> ## Documentation Index
> Fetch the complete documentation index at: https://docs.backapp.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Build an AI voiceover app

> Choose a voice, turn text into speech, and play or download the generated MP3.

Build a mobile app that lets users enter a script, select a voice, and listen to or save the generated audio.

This example requires an existing integration with access to `/api/text_to_speech` on its assigned legacy API origin. A public v2 Text to Speech endpoint is planned. Ask the Backapp team about access if you are setting up a new app.

## 1. Set up the session

Use the assigned API origin without a trailing slash or `/api`. Obtain a legacy access token through [Authentication](https://docs.backapp.dev/authentication.md#legacy-api) and use the same user's session to submit and read the task. Run the Bash, curl, and jq snippets in the same shell.

```bash theme={"dark"}
LEGACY_API_ORIGIN='https://your-assigned-legacy-origin'
# Set LEGACY_ACCESS_TOKEN from your legacy session before continuing.

voiceover_api() {
  curl --fail-with-body --silent --show-error \
    -H "Authorization: Bearer $LEGACY_ACCESS_TOKEN" "$@"
}
```

## 2. Choose the script and voice

Give the user a text field for the script and a voice picker with these IDs: `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer`. The legacy endpoint defaults to `alloy` when the voice is omitted. Trim the script and require nonblank text before enabling **Generate**.

```bash theme={"dark"}
VOICEOVER_TEXT='Welcome to the city tour. Our first stop is the old train station.'
VOICEOVER_VOICE='nova'
```

## 3. Start generation

Send the text and selected voice as JSON. HTTP 202 returns a `task_id`. Save it so the user can return to the screen while the audio is being generated.

```bash theme={"dark"}
task=$(jq -n --arg text "$VOICEOVER_TEXT" --arg voice "$VOICEOVER_VOICE" \
  '{text: $text, voice: $voice}' |
  voiceover_api "$LEGACY_API_ORIGIN/api/text_to_speech" \
    -H 'Content-Type: application/json' --data-binary @-) &&
  VOICEOVER_TASK_ID=$(jq -er '.task_id' <<< "$task")
```

Disable duplicate submissions while the request is in flight. If the connection drops before you receive a task ID, another POST can create a second task.

## 4. Wait for the MP3 and play it

Read the task status with the saved ID. Repeat with a delay while `status` is `pending` or `processing`. Keep one status request in flight and stop on `completed` or `failed`.

```bash theme={"dark"}
voiceover=$(voiceover_api --get "$LEGACY_API_ORIGIN/api/text_to_speech" \
  --data-urlencode "task_id=$VOICEOVER_TASK_ID") &&
  jq '{task_id, status, url, error_message}' <<< "$voiceover"
```

On completion, pass the nonempty `url` to your app's audio player as the MP3 source. Enable **Play** and **Download** once the URL is available. Older completed tasks can have an empty URL. On failure, read `error_message` and show the user a short message. See [Text to Speech response examples](https://docs.backapp.dev/legacy-ai.md#convert-text-to-speech).

## 5. Download the audio

Use the exact URL from the task response. Fetch it separately from the API calls without the legacy bearer token. Treat it as a shareable link and keep it out of logs.

```bash theme={"dark"}
audio_url=$(jq -er 'select(.status == "completed") | .url | strings | select(length > 0)' <<< "$voiceover") &&
  curl --fail --silent --show-error --location "$audio_url" -o voiceover.mp3
```

Open `voiceover.mp3` in a local player to preview it or offer it through your app's export action. Download it while the result is available. If a saved URL later returns HTTP 404, show that the audio is unavailable and offer to generate a new copy.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.