# qmoon REST API quickstart

Call each operation with a `POST` to `https://api.qmoon.ai/api/v1/<operation>`, with its input as JSON and an API key as the bearer token. Each returns the same response as its MCP tool. The [OpenAPI reference](https://api.qmoon.ai/api/v1/openapi.json) lists every operation with its input and responses.

## 1. Create a key

Create a key on the [Connections](https://qmoon.ai/en/studio?settings=connections) page in Settings. Name it after where it runs, and choose its access and spend limit in coins. The key is shown once. Store it in `QMOON_API_KEY`, and never print or commit it.

## 2. Check the key

```
curl https://api.qmoon.ai/api/v1/identity.connection.get -H "Authorization: Bearer $QMOON_API_KEY" -H "Content-Type: application/json" -d '{}'
```

A `200` with `"status": "done"` shows the workspace, the key’s access, its spend limit (the coins remaining and when it resets) and the coins it can spend.

## 3. Generate an image

1. List the image models with their ids and the starting price of an image, in coins.

   ```
   curl https://api.qmoon.ai/api/v1/models.model.list \
     -H "Authorization: Bearer $QMOON_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"capability":"image.generate","limit":5}'
   ```

2. Get an estimate before generating. The response shows which model runs the generation and how many coins it costs. An estimate is free and generates nothing.

   ```
   curl https://api.qmoon.ai/api/v1/generation.generation.estimate \
     -H "Authorization: Bearer $QMOON_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"capability":"image.generate","intent":{"aspect":"1:1","prompt":{"en":"A paper lantern over a quiet canal at dusk, anime"}},"count":1,"quality":"fast","sources":[]}'
   ```

3. Generate the image. A repeat call with the same `requestId` returns the first result instead of generating another. Use a new id for each new image.

   ```
   curl https://api.qmoon.ai/api/v1/generation.generation.create \
     -H "Authorization: Bearer $QMOON_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"capability":"image.generate","intent":{"aspect":"1:1","prompt":{"en":"A paper lantern over a quiet canal at dusk, anime"}},"count":1,"quality":"fast","sources":[],"requestId":"lantern-0001"}'
   ```

4. Wait for the image. `Prefer: wait=n` keeps the request open until the generation is settled, for up to 25 seconds. While `settled` is false, call it again.

   ```
   curl https://api.qmoon.ai/api/v1/generation.generation.get \
     -H "Authorization: Bearer $QMOON_API_KEY" \
     -H "Content-Type: application/json" \
     -H "Prefer: wait=20" \
     -d '{"generationId":"<generationId>"}'
   ```

5. Download the image. Each URL works for 5 minutes and only for this asset. Fetch it right away, and request new URLs later.

   ```
   curl https://api.qmoon.ai/api/v1/media.asset.files \
     -H "Authorization: Bearer $QMOON_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"assetId":"<assetId>"}'
   ```

## Responses

| `status` | HTTP | Contents |
| --- | --- | --- |
| `done` | 200 | `output`, the operation’s result. A read that waits also has `settled`. |
| `failed` | 4xx, 5xx | `error`, with its `area`, `code`, details, `retryable` and `help`, a sentence that says what happened and what to do |

The HTTP status shows the kind of error:

- 400: The input does not match the operation’s schema. `issue` says where.
- 401: The bearer token does not belong to an active connection.
- 402: The call is over a spend limit or out of coins, and `code` says which. For a spend limit, `limit` names the limit, and `remaining` and `needed` give the coins remaining and the coins the request needs. `resetsAt` is when the limit resets. It is null, with `retryable` false, when waiting will not help and only coins, a plan or a higher limit will. No Retry-After header is sent.
- 403: The connection’s scope or its permissions do not allow the call, or the operation is not in the public API.
- 404: The target or the operation does not exist, or the connection is not allowed to see it.
- 409: The request id was used for another request, or a call with it is still running.
- 413: The request body is larger than the API accepts.
- 422: The operation’s rules do not allow the call.
- 429: Too many calls from this connection. Retry-After and `retryAfterMs` say when to try again.
- 500: The call failed. `errorId` identifies the failure in qmoon’s logs. The response contains no other details of the failure.
- 501: A service the operation needs is not set up on this deployment. Trying again does not help.
- 503: A service the operation needs is set up but not answering now (`down`). Try again later.
