# Text to Image

**What**：Generate an image from text.

**When**：Use for GPT-Image style results.

## Endpoints
- POST `/v1/images/generations`
- GET `/v1/images/generations/{id}`

> This page uses the unified API: generate with `POST /v1/images/generations` (synchronous by default: it returns once the image is ready; add `"async": true` to get a task back immediately and poll `GET /v1/images/generations/{id}`), authenticate with `Authorization: Bearer <key>`; errors use the OpenAI format. General notes are in the Images guide.

Requests on this page must carry `"async": true` (or the `Prefer: respond-async` header): the call answers 202 with the task object right away (`status: queued`, `Retry-After: 3`); poll `GET /v1/images/generations/{id}` until `status` is `completed` or `failed`. A gpt-image request without `async` goes to the synchronous OpenAI Images route; see "Synchronous call".

## Request params
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| model | string | Yes | gpt-image-1 / gpt-image-1.5 / gpt-image-2 / gpt-image-2.5-flare / gpt-image-2.5-sunburst |
| prompt | string | Yes | Image description (1–4000 chars) |
| size | string | — | Prefer official pixel sizes (default 1024x1024). Square 1024x1024 / 2048x2048(2K); landscape 1536x1024 / 1792x1024 / 1920x1080 / 2048x1152(2K) / 3840x2160(4K); portrait 1024x1536 / 1024x1792 / 1080x1920 / 2160x3840(4K). Ratio aliases 1:1 / 2:3 / 3:2 / 16:9 / 9:16 are legacy-only (not recommended); invalid/missing falls back to 1024x1024 |
| quality | string | — | `low` / `medium` / `high` / `auto`, passed to the GPT-Image channel as is. |
| async | boolean | Yes | Must be `true` on this page (or the `Prefer: respond-async` header): answer 202 with the task object right away, then poll with GET. A gpt-image request without `async` goes to the synchronous OpenAI Images route. |

`model`, `prompt`, `n`, `size`, `aspect_ratio`, `resolution`, `images`, `output_format`, `negative_prompt`, `prompt_extend` and `async` are the common fields; the server converts them to the channel fields. Other fields in the table are native to the channel: put them in the same body under their own names and they are passed through, and a native field wins over its unified synonym. `images` takes http(s) image URLs only (no base64). `response_format` can only be `url`.

## Request example
```bash
curl https://api.novapi.ai/v1/images/generations \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "a watercolor illustration of a quiet seaside town",
  "size": "1536x1024",
  "async": true
}'
```

```python
import os, requests

r = requests.post(
    "https://api.novapi.ai/v1/images/generations",
    headers={"Authorization": "Bearer " + os.environ["API_KEY"]},
    json={
        "model": "gpt-image-2",
        "prompt": "a watercolor illustration of a quiet seaside town",
        "size": "1536x1024",
        "async": True,
    },
)
print(r.json())  # poll GET /v1/images/generations/{id} until status is completed
```

```ts
const resp = await fetch("https://api.novapi.ai/v1/images/generations", {
  method: "POST",
  headers: {
    Authorization: "Bearer " + process.env.API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "model": "gpt-image-2",
    "prompt": "a watercolor illustration of a quiet seaside town",
    "size": "1536x1024",
    "async": true
  }),
});
console.log(await resp.json()); // poll GET /v1/images/generations/{id} until status is completed
```

## Response example
### Response example (async, 202)
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "gpt-image-2",
  "status": "queued",
  "created": <unix-seconds>,
  "completed_at": null,
  "error": null,
  "data": []
}
```

### Response example (polled, completed)
```json
{
  "id": "img_<task-id>",
  "object": "image.generation",
  "model": "gpt-image-2",
  "status": "completed",
  "created": <unix-seconds>,
  "completed_at": <unix-seconds>,
  "error": null,
  "data": [
    { "url": "https://..." }
  ]
}
```

```bash
curl https://api.novapi.ai/v1/images/generations/img_<task-id> \
  -H "Authorization: Bearer $API_KEY"

# status: queued | in_progress | completed | failed
# completed: data[].url holds the image URL(s); failed: error.code and error.message
```

## Response fields
| Field | Type | Description |
| --- | --- | --- |
| id | string | Task ID, starts with `img_`; used for polling. |
| object | string | Always `image.generation`. |
| model | string | The model from the request. |
| status | string | `queued`, `in_progress`, `completed` or `failed`. A synchronous 200 is always `completed`. |
| created | integer | Creation time (Unix seconds), as in OpenAI Images. |
| completed_at | integer \| null | Completion time; null until completed. |
| error | object \| null | On failure `{code, message}`, with `code` `image_generation_failed`. |
| data | array | When completed, `[{ "url": "https://..." }]`, one element per image (several when `n` is above 1, or for the 4 Midjourney images); an empty array until then. |
