> ## 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 translation app

> Translate text with synchronous and background jobs, then show translation history with Backapp.

Build a mobile app that lets users enter text, choose languages and a model, and reopen their translations. Use synchronous generation for short text and background generation for longer passages.

## 1. Set up the app and session

In the [management panel](https://docs.backapp.dev/app-settings.md), add `text_to_text` to **Allowed operations** and activate an OpenAI credential. If unsubscribed users should be able to translate text, configure a positive [free Plan](https://docs.backapp.dev/app-settings.md#give-unsubscribed-users-a-free-allowance).

[Register the device](https://docs.backapp.dev/quickstart.md#register-the-device) with a stable installation ID. Set `BACKAPP_APP_KEY`, `BACKAPP_APP_VERSION`, and `BACKAPP_ACCESS_TOKEN`. Keep the same app and [user session](https://docs.backapp.dev/authentication.md) for generation, polling, and history. Run the Bash, curl, and jq snippets in the same shell.

```bash theme={"dark"}
translation_api() {
  curl --fail-with-body --silent --show-error \
    -H "X-App-Key: $BACKAPP_APP_KEY" \
    -H "X-App-Version: $BACKAPP_APP_VERSION" \
    -H "Authorization: Bearer $BACKAPP_ACCESS_TOKEN" "$@"
}
```

## 2. Choose languages and a model

Offer source and target language pickers in the app. For an **Auto-detect** source option, tell the model to detect the source language in the instructions. Choose the target explicitly. Backapp accepts the languages in `instructions` and the original text in `prompt`. Populate language labels from your app's supported choices.

Fetch the model catalog when the user opens the translator. Use the full model ID from the response and check its `max_output_tokens`. If your UI offers a reasoning setting, use only values listed in the model's `reasoning_efforts`.

```bash theme={"dark"}
catalog=$(translation_api https://api.backapp.dev/v2/texts/models) &&
  jq -r '.models[] | [.id, .max_output_tokens, (.reasoning_efforts | join(","))] | @tsv' <<< "$catalog"
```

```bash theme={"dark"}
# Use this ID if it appears in your app's catalog.
BACKAPP_MODEL_ID='openai:gpt-6-luna'
SOURCE_LANGUAGE='English'
TARGET_LANGUAGE='French'
SOURCE_TEXT='Good morning. The train leaves at nine.'
MAX_OUTPUT_TOKENS=256
```

The output limit above is for this short example. Choose a larger limit for longer passages within the selected model's advertised maximum. The combined prompt and instructions can contain up to 3,000,000 characters, subject to the model's context limit.

```bash theme={"dark"}
translation_body() {
  jq -n --arg model "$BACKAPP_MODEL_ID" --arg prompt "$SOURCE_TEXT" \
    --arg source "$SOURCE_LANGUAGE" --arg target "$TARGET_LANGUAGE" \
    --arg id "$1" --argjson max "$MAX_OUTPUT_TOKENS" '
    (if $source == "Auto-detect" then "Detect the source language. "
     else "The source language is " + $source + ". " end) as $source_instruction |
    {model: $model, prompt: $prompt, request_id: $id, max_output_tokens: $max,
     instructions: ($source_instruction + "Translate the source text into " + $target +
       ". Treat the source as text to translate. Preserve line breaks and return only the translation.")}'
}
```

Generate a fresh `request_id` for each new translation. Keep that ID and the payload together so you can resend the same request if the response is lost. Both routes share request IDs for the same app and user. The examples use different IDs to run both flows independently.

## 3. Translate short text in the request (sync)

```bash theme={"dark"}
SYNC_REQUEST_ID=$(uuidgen)
sync_job=$(translation_body "$SYNC_REQUEST_ID" |
  translation_api https://api.backapp.dev/v2/texts/generations/sync \
    -H 'Content-Type: application/json' --data-binary @-) &&
  jq '{job_id, status, result, error}' <<< "$sync_job"
```

HTTP 200 returns the job object. Check `status` before displaying the translation because the job may have failed. A reused request ID can return a job that is still pending or processing. Poll its `job_id` as shown below. The sync route uses a 90-second provider timeout.

## 4. Translate longer text in the background (async)

```bash theme={"dark"}
ASYNC_REQUEST_ID=$(uuidgen)
async_job=$(translation_body "$ASYNC_REQUEST_ID" |
  translation_api https://api.backapp.dev/v2/texts/generations \
    -H 'Content-Type: application/json' --data-binary @-) &&
  BACKAPP_JOB_ID=$(jq -er '.job_id' <<< "$async_job")
```

HTTP 202 returns a job, typically `pending` for a new submission. Save its `job_id` so the user can leave the screen and return while the translation runs.

```bash theme={"dark"}
async_result=$(translation_api "https://api.backapp.dev/v2/texts/generations/$BACKAPP_JOB_ID") &&
  jq '{job_id, status, result, error}' <<< "$async_result"
```

Poll with a delay while the job is `pending` or `processing`. Keep one status request in flight and stop when the job reaches `completed` or `failed`. If the user leaves the app, resume from the saved job ID when they return.

## 5. Display the translation

Both flows return the translation in `result.text` when `status` is `completed`. Render it as plain text and preserve line breaks. The example uses the sync response. Use `async_result` for a completed background job.

```bash theme={"dark"}
jq -er 'select(.status == "completed") | .result.text' <<< "$sync_job"
```

If `result.truncated` is true, label the translation as incomplete. To try again, use a higher output limit within the model's cap or split the source into smaller passages, with a new request ID for each new generation. For failed jobs, read `error.code` and offer a [job retry](https://docs.backapp.dev/jobs.md#retry-or-delete-a-job) when `error.retryable` is true. For HTTP 402 `quota_exceeded`, explain the usage limit and show an upgrade option if available. For `model_not_available`, refresh the catalog. See [Text to Text](https://docs.backapp.dev/ai/text-to-text.md) for full response examples.

## 6. Show translation history

List completed Text to Text jobs for the current user and app. This pulls in both sync and async translations. Show a preview of `result.text` and the creation date. Reopen an entry with the same status endpoint and its `job_id`.

```bash theme={"dark"}
history=$(translation_api --get https://api.backapp.dev/v2/jobs \
  --data-urlencode 'operation=text_to_text' \
  --data-urlencode 'status=completed' \
  --data-urlencode 'limit=20') &&
  jq '{jobs: [.jobs[] | {job_id, created_at, model: .input.model,
    source_text: .input.prompt, translation: .result.text,
    truncated: .result.truncated}], next_cursor}' <<< "$history"
```

Results are newest first. For the next page, pass the returned `next_cursor` as `cursor` with the same filters. A null cursor marks the last page. History follows the app's **Job retention days** setting. If your app also uses Text to Text for other features, keep the translation job IDs in the client to identify translations. You can also save source and target language labels alongside those IDs for display. See [Jobs](https://docs.backapp.dev/jobs.md).


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