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

# ChatIMG Image Generation API

> Generate and edit images from your own code: get a token, start a job, poll the result, check pricing

<Card title="ChatIMG API panel" icon="key" href="https://chatimg.ai/user/api">
  Sign in to get your token, check your credits balance and live pricing: [https://chatimg.ai/user/api](https://chatimg.ai/user/api)
</Card>

## 1. Get your API token

ChatIMG and BibiGPT share one account system, so **the API token is the same one**. Sign in to
ChatIMG and open [chatimg.ai/user/api](https://chatimg.ai/user/api) to get it (if you already use
the BibiGPT open API, that token works here as-is).

Every endpoint authenticates through the HTTP header:

```shell theme={null}
curl --header 'Authorization: Bearer <api_token>'
```

<Note>
  API calls draw from the **same credits balance** as the web app — no separate plan or top-up
  needed. Check the balance at [chatimg.ai/user/api](https://chatimg.ai/user/api) and top up at
  [chatimg.ai/pricing](https://chatimg.ai/pricing).
</Note>

## 2. Call flow

Image generation is **asynchronous**: start the job, then poll by the source image URL.

### Step 1 — Start generation [`POST /v1/generateImage`](https://docs.bibigpt.co/api-reference/open/generate-or-edit-an-image-with-ai-chatimgai)

```shell theme={null}
curl -X POST https://api.bibigpt.co/api/v1/generateImage \
  -H "Authorization: Bearer $CHATIMG_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "imageUrl": "https://example.com/photo.jpg",
    "prompt": "ghibli style",
    "model": "nanobanana-2-lite"
  }'
```

| Parameter  | Required | Description                                                                          |
| ---------- | -------- | ------------------------------------------------------------------------------------ |
| `imageUrl` | Yes      | Source image: an http(s) URL or base64 (with or without the `data:image/...` prefix) |
| `prompt`   | No       | What you want the result to look like. Defaults to `ghibli`                          |
| `model`    | No       | Model id, defaults to `nanobanana-2-lite`. See the pricing table below               |

The response carries `taskId` for tracking, `costCredits` for what this call charged, and
`balanceRemaining` for the balance after the charge.

<Note>
  Failed generations are **refunded automatically** — you don't need to reconcile them yourself.
</Note>

### Step 2 — Poll the result [`GET /v1/imageStatus`](https://docs.bibigpt.co/api-reference/open/get-the-status-of-an-image-generation-task)

Query with the **same `imageUrl`** you sent. Read-only and free of charge:

```shell theme={null}
curl "https://api.bibigpt.co/api/v1/imageStatus?imageUrl=https://example.com/photo.jpg" \
  -H "Authorization: Bearer $CHATIMG_API_TOKEN"
```

Once `status` becomes `completed`, `generatedImageUrl` holds the finished image.

Latency varies a lot by model (lightweight models take seconds, GPT Image 2 about 1–2 minutes), so
poll **every 3–5 seconds** with a sensible timeout.

### Step 3 — Check pricing [`GET /v1/imagePricing`](https://docs.bibigpt.co/api-reference/open/list-available-image-models-and-their-credits-pricing)

No authentication required. Returns the per-image credits price of each model plus top-up packs, so
your script can enforce a budget:

```shell theme={null}
curl https://api.bibigpt.co/api/v1/imagePricing
```

## 3. Models and pricing

Credits charged per generated image (`/v1/imagePricing` is the live source of truth):

| Model               | Credits | Model            | Credits |
| ------------------- | ------- | ---------------- | ------- |
| `z-image-turbo`     | 5       | `nanobanana-2`   | 20      |
| `qwen`              | 8       | `openai`         | 25      |
| `gemini`            | 10      | `flux-2-flex`    | 25      |
| `flux`              | 12      | `grok`           | 30      |
| `nanobanana-2-lite` | 12      | `nanobanana-pro` | 30      |
| `seedream`          | 15      | `gpt-image-2`    | 50      |

## 4. End-to-end example

Minimal script that starts a job and polls until it finishes:

```bash theme={null}
TOKEN="$CHATIMG_API_TOKEN"
SRC="https://example.com/photo.jpg"

curl -s -X POST https://api.bibigpt.co/api/v1/generateImage \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"imageUrl\":\"$SRC\",\"prompt\":\"ghibli style\",\"model\":\"nanobanana-2-lite\"}"

# Poll until completed
for i in $(seq 1 60); do
  sleep 3
  RESP=$(curl -s "https://api.bibigpt.co/api/v1/imageStatus?imageUrl=$SRC" \
    -H "Authorization: Bearer $TOKEN")
  echo "$RESP" | grep -q '"status":"completed"' && echo "$RESP" && break
done
```

## FAQ

<AccordionGroup>
  <Accordion title="Getting a 401?">
    The token is missing or no longer valid. Check that the header reads
    `Authorization: Bearer <token>`, and confirm your current token at
    [chatimg.ai/user/api](https://chatimg.ai/user/api). If you ever hit "reset", the old token stops
    working immediately and your scripts need the new one.
  </Accordion>

  <Accordion title="Getting a 400 invalid_image_url?">
    `imageUrl` must be a publicly reachable http(s) address or base64 data. A browser-local preview
    address (starting with `blob:`) can't be read by the server. These requests are not charged.
  </Accordion>

  <Accordion title="Out of credits?">
    Top up a credits pack at [chatimg.ai/pricing](https://chatimg.ai/pricing) (credits never expire),
    or subscribe for a monthly allowance. You can also switch to a cheaper model — `z-image-turbo`
    costs 5 credits per image.
  </Accordion>

  <Accordion title="Status stuck on unknown?">
    There's no generation record for that `imageUrl` — usually the polling URL doesn't exactly match
    the one you submitted. The two must be **character-for-character identical**, query string
    included.
  </Accordion>
</AccordionGroup>
