# Get a sound from the API

Send one request to the Mirli server and save the sound it returns as a WAV
file. The request waits while the model renders, so there is no job to follow.

## Before you begin

- Mirli is running on this machine. The desktop app starts the server; from a
  clone, [Run Mirli from source](https://mirli.app/docs/install/index.md) describes how to start it.
- The **Stable Audio 3.0 SFX** model is installed. In the app, go to the
  **Models** page to install it.
- For the Python example, Python 3. For the JavaScript example, Node.js 18 or
  later.

## Render a sound

1. Check that the server answers on its default address:

   ```bash
   curl http://127.0.0.1:17494/api/health
   ```

   The response is JSON with `"status": "ok"`. If the connection is refused,
   the server is not running or listens on another port.

2. Send a render request for a two-second door slam. For Python, save the code
   as `render.py` and run `python render.py`; for JavaScript, save it as
   `render.mjs` and run `node render.mjs`:

   ```bash title="curl"
   curl -X POST http://127.0.0.1:17494/api/render \
     -H 'Content-Type: application/json' \
     -d '{
       "model": "audiocpp-stable-audio-3-sfx",
       "prompt": "heavy wooden door slam, dry room",
       "duration_s": 2
     }' \
     -o door.wav
   ```

   ```python title="Python"
   import json
   import urllib.request

   body = json.dumps({
     "model": "audiocpp-stable-audio-3-sfx",
     "prompt": "heavy wooden door slam, dry room",
     "duration_s": 2,
   }).encode()
   request = urllib.request.Request(
     "http://127.0.0.1:17494/api/render",
     data=body,
     headers={"Content-Type": "application/json"},
   )
   with urllib.request.urlopen(request) as response:
     with open("door.wav", "wb") as out:
       out.write(response.read())
   ```

   ```js title="JavaScript"
   import { writeFile } from "node:fs/promises";

   const response = await fetch("http://127.0.0.1:17494/api/render", {
     method: "POST",
     headers: { "Content-Type": "application/json" },
     body: JSON.stringify({
       model: "audiocpp-stable-audio-3-sfx",
       prompt: "heavy wooden door slam, dry room",
       duration_s: 2,
     }),
   });
   if (!response.ok) throw new Error(await response.text());
   const audio = Buffer.from(await response.arrayBuffer());
   await writeFile("door.wav", audio);
   ```

   The request returns when the render is done, which takes seconds to a
   minute, and `door.wav` appears in the current folder.

3. Open `door.wav` in an audio player to hear the result. The sound is also in
   the app's library, because the server keeps every render by default.

If a step returns an error instead, its `detail` says why.
[Errors](https://mirli.app/docs/api-errors/index.md) lists every status and what causes it.

## What's next

- [Generation endpoints](https://mirli.app/docs/api-generate/index.md) describe every field of a render,
  and the job-based endpoints for long renders.
- [Speech endpoints](https://mirli.app/docs/api-speech/index.md) describe reading text aloud with a saved
  voice.
- [OpenAI-compatible endpoints](https://mirli.app/docs/api-openai/index.md) describe using an OpenAI
  client library with Mirli.
