Skip to main content

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:
https://api.cometapi.com/v1/images/generations
Authenticate with a Bearer token:
Authorization: Bearer $COMETAPI_KEY

Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.
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"
  }'

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

ParameterUse
modelGPT image model ID. The reference example uses gpt-image-2.
promptText description of the image to generate.
sizeRequested output size. Supported values depend on the selected model.
qualityQuality setting for models that support it.
output_formatEncoded image type for GPT image results, such as jpeg, png, or webp.

Troubleshooting / FAQ

Confirm you decoded b64_json as base64 bytes and saved the file extension that matches output_format.
Image parameters vary by model. Start with the minimal reference example, then add one optional field at a time.
Use b64_json for GPT image models. URL output is model-specific.

Next steps