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

# GPT Image API quickstart: Generate images with CometAPI

> Generate a GPT image through CometAPI, decode b64_json, and save the result with curl, Python, or Node.js.

## What you will build

You will call `POST /v1/images/generations`, generate one image with a GPT image model, decode the returned `b64_json`, and save it as an image file.

## Prerequisites

* A CometAPI API key stored in `COMETAPI_KEY`
* Python 3.10+ or Node.js 18+ for file-saving examples
* A server-side environment. Do not call image generation from public frontend code with a secret key.

## API key, base URL, authentication

Use the OpenAI-compatible image endpoint:

```text theme={null}
https://api.cometapi.com/v1/images/generations
```

Authenticate with a Bearer token:

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/images/generations \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-image-2",
      "prompt": "A paper boat floating on calm water at sunrise.",
      "quality": "low",
      "size": "1024x1024",
      "output_format": "jpeg"
    }'
  ```

  ```python Python theme={null}
  import base64
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["COMETAPI_KEY"],
      base_url="https://api.cometapi.com/v1",
  )

  image = client.images.generate(
      model="gpt-image-2",
      prompt="A paper boat floating on calm water at sunrise.",
      quality="low",
      size="1024x1024",
      output_format="jpeg",
  )

  image_base64 = image.data[0].b64_json
  with open("cometapi-gpt-image.jpeg", "wb") as file:
      file.write(base64.b64decode(image_base64))
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs/promises";
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.COMETAPI_KEY,
    baseURL: "https://api.cometapi.com/v1",
  });

  const image = await client.images.generate({
    model: "gpt-image-2",
    prompt: "A paper boat floating on calm water at sunrise.",
    quality: "low",
    size: "1024x1024",
    output_format: "jpeg",
  });

  const imageBase64 = image.data[0].b64_json;
  await fs.writeFile("cometapi-gpt-image.jpeg", Buffer.from(imageBase64, "base64"));
  ```
</CodeGroup>

## Flow explanation

GPT image generation returns the completed response in one request. GPT image models return base64 image data in `data[0].b64_json`. Decode that value and write the bytes to a file that matches `output_format`.

If you need URL output, check whether the selected model supports URL responses. GPT image models use base64 output in the maintained reference example.

## Common parameters

| Parameter       | Use                                                                         |
| --------------- | --------------------------------------------------------------------------- |
| `model`         | GPT image model ID. The reference example uses `gpt-image-2`.               |
| `prompt`        | Text description of the image to generate.                                  |
| `size`          | Requested output size. Supported values depend on the selected model.       |
| `quality`       | Quality setting for models that support it.                                 |
| `output_format` | Encoded image type for GPT image results, such as `jpeg`, `png`, or `webp`. |

## Troubleshooting / FAQ

<AccordionGroup>
  <Accordion title="The saved file does not open">
    Confirm you decoded `b64_json` as base64 bytes and saved the file extension that matches `output_format`.
  </Accordion>

  <Accordion title="The model rejects a parameter">
    Image parameters vary by model. Start with the minimal reference example, then add one optional field at a time.
  </Accordion>

  <Accordion title="The response has no URL">
    Use `b64_json` for GPT image models. URL output is model-specific.
  </Accordion>
</AccordionGroup>

## Next steps

* Read the [Create image API reference](/api/image/openai/images).
* Find image model IDs in [Models](/overview/models).
* Estimate request cost with [Estimate request cost before calling a model](/guides/how-to-estimate-cost-before-calling-a-model).
* Handle failures with [Error codes and retry strategy](/guides/error-codes-and-retry-strategy).
