# Optimize audio

Re-record a voice clip or music track in a living room, then download its M4A with an optimization score.

## Start an optimization

**Optimize audio by URL**

```bash title="cURL"
curl -X POST "https://realpng.com/api/v1/optimize" \
  -H "X-API-Key: $REALPNG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "audio_url": "https://audio.example.com/voice-note.mp3",
    "seconds": 18
  }'
```

```javascript title="JavaScript"
const res = await fetch("https://realpng.com/api/v1/optimize", {
  method: "POST",
  headers: {
    "X-API-Key": process.env.REALPNG_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "audio_url": "https://audio.example.com/voice-note.mp3",
    "seconds": 18
  }),
});
const data = await res.json();
```

```python title="Python"
import os

import requests

res = requests.post(
    "https://realpng.com/api/v1/optimize",
    headers={"X-API-Key": os.environ["REALPNG_API_KEY"]},
    json={
        "audio_url": "https://audio.example.com/voice-note.mp3",
        "seconds": 18,
    },
)
data = res.json()
```

Response `202 Accepted`

```json
{
  "id": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
  "status": "queued",
  "media": "audio"
}
```

`POST /optimize` answers 202 with a job id. Send exactly one of `audio_url` or `audio_upload_id`. An audio file on disk goes through [an upload](/docs/guides/upload-a-local-file).

The optimizer accepts MP3, WAV and M4A files. It treats music and voice clips the same way, and scores the encoded output with the voice detector.

## Poll and download

**Get the audio optimization**

```bash title="cURL"
curl "https://realpng.com/api/v1/optimize/7c9e6679-7425-40de-944b-e07fc1f90ae7" \
  -H "X-API-Key: $REALPNG_API_KEY"
```

```javascript title="JavaScript"
const res = await fetch("https://realpng.com/api/v1/optimize/7c9e6679-7425-40de-944b-e07fc1f90ae7", {
  headers: {
    "X-API-Key": process.env.REALPNG_API_KEY,
  },
});
const data = await res.json();
```

```python title="Python"
import os

import requests

res = requests.get(
    "https://realpng.com/api/v1/optimize/7c9e6679-7425-40de-944b-e07fc1f90ae7",
    headers={"X-API-Key": os.environ["REALPNG_API_KEY"]},
)
data = res.json()
```

Response `200 OK`

```json
{
  "id": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
  "status": "succeeded",
  "media": "audio",
  "file_url": "https://realpng.com/api/v1/optimize/7c9e6679-7425-40de-944b-e07fc1f90ae7/file",
  "expires_at": "2026-10-15T12:00:00+00:00",
  "optimization_score": {
    "score": 86,
    "zone": "good"
  }
}
```

Poll [`GET /optimize/{id}`](/docs/api-reference/get-optimization) until the job succeeds or fails. A succeeded job has `file_url`, an M4A download link, and `optimization_score`. The link works until `expires_at`.

The output is AAC at 192 kb/s and 48 kHz. It keeps the input length and channel count. Its tags and encoder line are removed.

## From an agent

The MCP tool `optimize_audio` takes the same fields and waits for the job. `get_processing_result` resumes a job that is still running. See [MCP tools](/docs/mcp/tools).

- [Credits and limits](/docs/credits-and-limits#video-limits): Audio size, length and credit limits.

- [Upload a local file](/docs/guides/upload-a-local-file): Send audio from your disk by its upload id.
