> ## 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 photo editor

> Upload a photo, apply an AI edit, and show the result and edit history with Backapp.

Build a mobile photo editor that lets users choose a photo, pick a model, and describe an edit. The app shows the edited photo when the job completes and lets the user reopen past edits.

## 1. Set up the app and session

In the [management panel](https://docs.backapp.dev/app-settings.md), add `image_to_image` to your app's **Allowed operations**. Activate an OpenAI or Replicate credential for the models you want to offer. For a freemium app, set up a [free Plan](https://docs.backapp.dev/app-settings.md#give-unsubscribed-users-a-free-allowance) so unsubscribed users can try an edit.

Use a stable installation ID to [register the device](https://docs.backapp.dev/quickstart.md#register-the-device) and save the session. The examples use Bash, curl, and jq. Set `BACKAPP_APP_KEY`, `BACKAPP_APP_VERSION`, and `BACKAPP_ACCESS_TOKEN` and run the snippets in the same shell. Keep the same app and user session for every API call.

```bash theme={"dark"}
photo_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. Upload a photo

Let the user pick a photo from their library. Send JPEG, PNG, or WebP up to 10 MiB, 4,096 pixels per side, and 16 million pixels in total. Convert HEIC photos to a supported format before uploading for this workflow. The example assumes a local file named `photo.jpg`.

```bash theme={"dark"}
upload=$(photo_api https://api.backapp.dev/v2/files -F 'file=@photo.jpg') &&
  BACKAPP_FILE_ID=$(jq -er '.file_id' <<< "$upload")
```

HTTP 201 returns `file_id` and `expires_at`. Keep the source available until processing finishes. Reuse the same file ID to try another edit while the file is still available.

## 3. Choose a model and prompt

Populate the model picker from your app's catalog. Save the selected model's full `id`, including the provider prefix. If the catalog is empty, check the app's provider credentials.

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

Offer a custom prompt or prefill one for a style such as **Watercolor**. Ask for the desired change and specify what to preserve. The prompt must be nonblank and no longer than 2,000 characters.

```bash theme={"dark"}
# Use this ID if the model is present in your catalog.
BACKAPP_MODEL_ID='openai:gpt-image-2.6-flare'
BACKAPP_PROMPT='Turn this photo into a soft watercolor illustration. Preserve the subject, framing, and main colors.'
```

## 4. Start the edit

Generate and save a request ID for each new edit. If the submission response is lost, resend the same payload with the saved request ID. Backapp can return the existing job while it exists and submission checks pass. Use a fresh ID when the user explicitly starts another edit.

```bash theme={"dark"}
BACKAPP_REQUEST_ID=$(uuidgen)

job=$(jq -n --arg file "$BACKAPP_FILE_ID" --arg model "$BACKAPP_MODEL_ID" \
  --arg prompt "$BACKAPP_PROMPT" --arg id "$BACKAPP_REQUEST_ID" \
  '{file_id: $file, model: $model, prompt: $prompt, request_id: $id}' |
  photo_api https://api.backapp.dev/v2/images/transforms \
    -H 'Content-Type: application/json' --data-binary @-) &&
  BACKAPP_JOB_ID=$(jq -er '.job_id' <<< "$job")
```

HTTP 202 returns a job. Save its `job_id` to resume after an app restart. If the API returns `model_not_available`, refresh the model picker. For HTTP 402 `quota_exceeded`, explain that the user has reached their allowance and show upgrade options if available.

## 5. Show the edited photo

Poll the job with a delay while its status is `pending` or `processing`. Keep one status request in flight at a time and stop at `completed` or `failed`. Resume from the saved job ID when the user returns to the app.

```bash theme={"dark"}
result=$(photo_api "https://api.backapp.dev/v2/images/transforms/$BACKAPP_JOB_ID") &&
  jq '{status, result, error}' <<< "$result"
```

On completion, render `result.image.url` and use `result.image.width` and `result.image.height` to size the preview. This example downloads the result for local display or export.

```bash theme={"dark"}
image_url=$(jq -er 'select(.status == "completed") | .result.image.url' <<< "$result") &&
  curl --fail --silent --show-error --location "$image_url" -o edited-photo
```

Use `result.image.content_type` when choosing a file extension for export. On failure, display the job error and offer [retry](https://docs.backapp.dev/jobs.md#retry-or-delete-a-job) when `error.retryable` is true. See [Image to Image](https://docs.backapp.dev/ai/image-to-image.md) for complete response examples.

## 6. Show edit history

List completed image transforms for the current app and user. Show the edited image, prompt, and creation time in each row. Use `job_id` to reopen an edit through the same status endpoint.

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

Results are newest first. If `next_cursor` is a string, send it as the `cursor` query parameter with the same filters to load the next page. A null cursor marks the last page.

**Job retention days** controls how long the edit appears in history. **File retention hours** controls how long the source and edited photos are kept. An edited photo can expire while the job is still listed. Show an unavailable state if its URL returns HTTP 404 and let users export results before they expire. See [Files](https://docs.backapp.dev/files.md#app-retention-settings) and [Jobs](https://docs.backapp.dev/jobs.md).


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