# Audio

Music and sound effects, speech, and transcription.

## Music and sound

`POST /v1/audio/generations` with `model`, `prompt` and, where the model allows it, `duration` (seconds), `instrumental` or `loop`. It runs as a job, exactly like [video](https://ewest.ai/docs/video): follow `/v1/jobs/{id}/events`, poll, or pass a `webhook_url`. The finished MP3 is on `cdn.ewest.ai` for 7 days.

cURL:

```bash
# 1. Create the job
curl https://api.ewest.ai/v1/audio/generations \
  -H "Authorization: Bearer $EWEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "ewest/eleven-music-v2.5", "prompt": "upbeat lo-fi beat with warm piano", "duration": 30}'
# → 202 {"id": "job_…", "type": "audio", "status": "queued", "progress": 0, …}

# 2. Follow it (Server-Sent Events), or poll GET /v1/jobs/job_…
curl -N https://api.ewest.ai/v1/jobs/job_…/events -H "Authorization: Bearer $EWEST_KEY"
# → … "output": {"url": "https://cdn.ewest.ai/aud/….mp3"}
```

Python:

```python
import os, time, requests

H = {"Authorization": f"Bearer {os.environ['EWEST_KEY']}"}
job = requests.post("https://api.ewest.ai/v1/audio/generations", headers=H, json={"model": "ewest/eleven-music-v2.5", "prompt": "upbeat lo-fi beat with warm piano", "duration": 30}).json()
while job["status"] not in ("succeeded", "failed"):
    time.sleep(2)
    job = requests.get(f"https://api.ewest.ai/v1/jobs/{job['id']}", headers=H).json()
print(job["output"]["url"] if job["output"] else job["error"])
```

JavaScript:

```javascript
const H = { Authorization: `Bearer ${process.env.EWEST_KEY}`, "Content-Type": "application/json" };
let job = await (await fetch("https://api.ewest.ai/v1/audio/generations", {
  method: "POST", headers: H, body: JSON.stringify({"model": "ewest/eleven-music-v2.5", "prompt": "upbeat lo-fi beat with warm piano", "duration": 30}),
})).json();
while (!["succeeded", "failed"].includes(job.status)) {
  await new Promise((r) => setTimeout(r, 2000));
  job = await (await fetch(`https://api.ewest.ai/v1/jobs/${job.id}`, { headers: H })).json();
}
console.log(job.output?.url ?? job.error);
```

## Speech

`POST /v1/audio/speech` with `model`, `input` (the text) and `voice` returns the audio file, like OpenAI's API. Voices differ per model (see the model page). With `"response_format": "url"` you get a `cdn.ewest.ai` link instead, kept for 7 days.

cURL:

```bash
curl https://api.ewest.ai/v1/audio/speech \
  -H "Authorization: Bearer $EWEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "ewest/eleven-v4-turbo", "input": "Welcome to eWest."}' \
  --output speech.audio

# Add "response_format": "url" to get {"url": "https://cdn.ewest.ai/aud/…"} instead of the bytes.
```

Python:

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
audio = client.audio.speech.create(model="ewest/eleven-v4-turbo", voice="Rachel", input="Welcome to eWest.")
audio.write_to_file("speech.audio")
```

JavaScript:

```javascript
import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.ewest.ai/v1", apiKey: process.env.EWEST_KEY });
const audio = await client.audio.speech.create({ model: "ewest/eleven-v4-turbo", voice: "Rachel", input: "Welcome to eWest." });
fs.writeFileSync("speech.audio", Buffer.from(await audio.arrayBuffer()));
```

## Transcription

`POST /v1/audio/transcriptions` takes an audio `file` (multipart, up to 25 MB and 20 minutes) or a public `audio_url` in JSON. `response_format` is `json` (default), `text` or `verbose_json` (language, duration and word timestamps where the model provides them). The file must be mp3, wav, m4a, ogg, flac or webm; [Files](https://ewest.ai/docs/files#problems) lists the errors for files that are too big, too long or in another format.

cURL:

```bash
curl https://api.ewest.ai/v1/audio/transcriptions \
  -H "Authorization: Bearer $EWEST_KEY" \
  -F model=ewest/scribe-v2 \
  -F file=@meeting.mp3 \
  -F response_format=verbose_json

# → {"text": "…", "language": "en", "duration": 92.4, "words": [{"word": "Hello", "start": 0.1, "end": 0.4}, …]}
```

Python:

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
with open("meeting.mp3", "rb") as f:
    result = client.audio.transcriptions.create(model="ewest/scribe-v2", file=f)
print(result.text)
```

JavaScript:

```javascript
import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.ewest.ai/v1", apiKey: process.env.EWEST_KEY });
const result = await client.audio.transcriptions.create({ model: "ewest/scribe-v2", file: fs.createReadStream("meeting.mp3") });
console.log(result.text);
```

Source: https://ewest.ai/docs/audio
