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

# Train a LoRA with the API

> Build a dataset, upload and caption images, add a character's photos or synthetic images, then train LoRAs and download every checkpoint with the API.

Everything the [cloud trainer](https://docs.omnichar.org/cloud/lora-training/overview.md) does is in the API: build a dataset, caption it, train one LoRA per base model, and download the final LoRA and its checkpoints. Agents get the same through the [MCP server](https://docs.omnichar.org/cloud/mcp.md#lora-training).

**Script LoRA training against the API.** Paste into Claude Code, Cursor or VS Code in your project, and your agent writes the dataset, caption and training calls for you.

Write a script that trains a LoRA with the Omnichar API from a folder of images.

Read `https://docs.omnichar.org/api/lora-training.md`, `https://docs.omnichar.org/api/uploads.md` and `https://docs.omnichar.org/api/authentication.md` first.

1. Ask me for the language, the image folder, the LoRA's name and trigger word, and whether it is for one of my characters (then ask for the character id).
2. Read the key from the `OMNICHAR_API_KEY` environment variable. It needs the `training:read` and `training:write` scopes and must be a production `sk_live_` key.
3. Create a dataset, upload each JPG, PNG or WebP image with `POST /v1/uploads` and a `PUT`, and add the `upload_key`s to the dataset. Use a matching `.txt` file as an image's caption when there is one.
4. If any image has no caption, start auto-caption and poll the job until `completed`.
5. Call `POST /v1/training/estimate`, print the credits and minutes, and stop for my yes before `POST /v1/training`. Send an `Idempotency-Key`.
6. Poll each run until it settles, then download every output from `/v1/runs/{id}/outputs`, naming checkpoints by their `step`.

Hard rules:

* Never print, log or commit the API key.
* Don't start training, auto-caption or synthetic images without my yes in this chat.

Done when: the script runs end to end against a dataset of mine and saves the `.safetensors` files.

## Before you start

Create a **production** key (`sk_live_`) with these scopes. New keys don't have them, so tick them when you create the key. See [Authentication](https://docs.omnichar.org/api/authentication.md).

| Scope | Lets the key |
| - | - |
| `training:write` | create and edit datasets, upload images, auto-caption, add a character's photos or synthetic images, start, retry and test training |
| `training:read` | read datasets, caption jobs, estimates, training runs and their LoRA files |

A sandbox key (`sk_test_`) can build and edit datasets. Training, auto-caption, synthetic images and LoRA tests run on GPUs, so a sandbox key gets `422` with `sandbox_unsupported`.

## 1. Create a dataset

`POST /v1/datasets` · scope `training:write`

```bash theme={null}
curl -s -X POST https://api.omnichar.org/v1/datasets \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"mara-skin","triggerWord":"mara"}'
# -> {"id":"5b1d...","name":"mara-skin","triggerWord":"mara","itemCount":0,...}
```

The trigger word is added to the front of every caption when training starts, so don't repeat it in captions. `GET /v1/datasets` lists your datasets, newest first; reuse one rather than building it again.

## 2. Add images

Upload each JPG, PNG or WebP image the way [Uploads](https://docs.omnichar.org/api/uploads.md) describes (`POST /v1/uploads` with `"kind":"image"`, then `PUT` the bytes), then add the `upload_key`s:

`POST /v1/datasets/{id}/items` · scope `training:write`

```bash theme={null}
curl -s -X POST https://api.omnichar.org/v1/datasets/$DATASET_ID/items \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"assetIds":["inputs/9b2e.../4c1d.../mara-01.jpg","inputs/9b2e.../8e2a.../mara-02.jpg"]}'
```

The answer is the dataset with every image and its `id`. Up to 200 images per call; 10 to 50 trains well. To set or change a caption:

`PATCH /v1/datasets/{id}/items/{itemId}` · scope `training:write`

```bash theme={null}
curl -s -X PATCH https://api.omnichar.org/v1/datasets/$DATASET_ID/items/$ITEM_ID \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"caption":"close-up, soft window light, smiling"}'
```

To set every image and caption in one call, `PUT /v1/datasets/{id}/items` with `{"items":[{"assetId":"<upload_key>","caption":"..."}]}`; it replaces the dataset's images with that list. `DELETE /v1/datasets/{id}/items/{itemId}` removes one image.

### From a character

For a character built in Omnichar Cloud, copy the photos it was built from:

`POST /v1/datasets/{id}/import-character` · scope `training:write` · body `{"characterId":"..."}`

With only a few photos, generate 12, 24 or 48 more of the character in varied angles, light and expressions. They are captioned and added for you:

`POST /v1/datasets/{id}/synthetic` · scope `training:write` · body `{"characterId":"...","size":12}`

The answer has a `planId` and the `credits` it holds. Poll `GET /v1/builds/{planId}` until `stage` is `done`; `synthetic.done` and `synthetic.total` count the images. Each image is priced like a 1K character image, and one that fails is not charged. See [Pricing](https://docs.omnichar.org/cloud/pricing.md#lora-training).

## 3. Caption it

`POST /v1/datasets/{id}/caption` · scope `training:write`

```bash theme={null}
curl -s -X POST https://api.omnichar.org/v1/datasets/$DATASET_ID/caption \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"florence-2-large","style":"sentence","overwrite":false}'
# -> {"jobId":"a7c0...","targets":12,"credits":12}
```

| Field | Default | Notes |
| - | - | - |
| `model` | `florence-2-large` | Or `florence-2-base`, `blip-large`, `blip-base`. |
| `style` | `sentence` | `sentence` for a plain description, `tags` for comma-separated tags. |
| `overwrite` | `false` | `true` recaptions images that already have a caption. |

`credits` is the most it can cost. You are charged per caption written, so an image it can't caption costs nothing. Poll `GET /v1/datasets/{id}/caption/{jobId}` until `state` is `completed`; the captions are then on the dataset. `GET /v1/datasets/{id}` also returns `captionCreditsPerImage`, the price per image.

## 4. Price and start training

The model slugs are `train-lora-flux-2-klein-4b`, `train-lora-krea-2`, `train-lora-krea-2-turbo`, `train-lora-z-image`, `train-lora-moss-image`, `train-lora-minimax-h3` and `train-lora-ltx-2-5`; [Base models](https://docs.omnichar.org/cloud/lora-training/overview.md#base-models) says what each is for.

`POST /v1/training/estimate` · scope `training:read`

```bash theme={null}
curl -s -X POST https://api.omnichar.org/v1/training/estimate \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"models":["train-lora-flux-2-klein-4b"],"preset":"fast"}'
# -> {"etaMin":73,"credits":<price>,"lines":[...]}
```

`POST /v1/training` · scope `training:write`

```bash theme={null}
curl -s -X POST https://api.omnichar.org/v1/training \
  -H "Authorization: Bearer $OMNICHAR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: train-mara-0001" \
  -d '{
    "datasetId": "5b1d...",
    "models": ["train-lora-flux-2-klein-4b"],
    "preset": "fast",
    "characterId": "8d2a...",
    "settings": { "steps": 800, "saveEvery": 200 },
    "exportCheckpoints": true,
    "notifyEmail": false
  }'
# -> {"batchId":"...","runs":[{"runId":"fc4c...","model":"train-lora-flux-2-klein-4b"}],"refused":[],"estimate":{...}}
```

| Field | Required | Notes |
| - | - | - |
| `datasetId` | yes | Every image needs a caption. |
| `models` | yes | One to four slugs. Each trains on its own GPU at the same time, so you get one LoRA per model. |
| `preset` | no | `fast` (1000 steps), `balanced` (1500, default) or `quality` (2500). |
| `characterId` | no | A character LoRA. Leave it out and send `name` for a standalone one. |
| `settings` | no | `steps`, `resolution`, `rank`, `alpha`, `learningRate`, `batchSize`, `saveEvery`, `captionDropout`, `flipAugment`, `loraScope`. See [Settings](https://docs.omnichar.org/cloud/lora-training/settings.md). |
| `exportCheckpoints` | no | `true` keeps a LoRA at every `saveEvery` step as well as the final one. |
| `notifyEmail` | no | Email the person who created the key when each LoRA finishes. Default `true`. |

The estimate is held when the run starts. You pay for the GPU time used, the rest comes back, and a failed run costs nothing. A model with no free GPU comes back in `refused` while the others start.

## 5. Get the LoRA

Poll `GET /v1/training` (filter with `characterId` or `status`) or `GET /v1/runs/{runId}` until the run completes. Then:

`GET /v1/runs/{runId}/outputs` · scope `training:read` or `runs:read`

```json theme={null}
{
  "runId": "fc4c...",
  "status": "succeeded",
  "outputs": [
    { "mediaKind": "lora", "label": "mara", "checkpoint": false, "step": null, "url": "https://...", "bytes": 74200000 },
    { "mediaKind": "lora", "label": "mara @ 200", "checkpoint": true, "step": 200, "url": "https://...", "bytes": 74200000 }
  ]
}
```

The output with `checkpoint: false` is the final LoRA; each checkpoint carries its `step`. Links expire: download right away, or ask for the outputs again. To render a test image with the LoRA, `POST /v1/training/{runId}/test` with `{"prompt":"...","step":200}`, and read the result as a normal run. `POST /v1/training/{runId}/retry` trains a failed run again on the same settings.

## Errors

| Status | When |
| - | - |
| `403` | The key is missing `training:read` or `training:write`. |
| `402` | The balance doesn't cover the estimate. |
| `422` | The dataset has no images, an image has no caption, a model isn't available in the cloud, or the key is a sandbox key. |
| `503` | No GPU was free for any model. Start it again in a few minutes. |

See [Errors](https://docs.omnichar.org/api/errors.md) for the response body.


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