What you will build
You will callPOST /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:Code examples
Use the tabs below for copyable examples in cURL, Python, and Node.js.Flow explanation
GPT image generation returns the completed response in one request. GPT image models return base64 image data indata[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
The saved file does not open
The saved file does not open
Confirm you decoded
b64_json as base64 bytes and saved the file extension that matches output_format.The model rejects a parameter
The model rejects a parameter
Image parameters vary by model. Start with the minimal reference example, then add one optional field at a time.
The response has no URL
The response has no URL
Use
b64_json for GPT image models. URL output is model-specific.Next steps
- Read the Create image API reference.
- Find image model IDs in Models.
- Estimate request cost with Estimate request cost before calling a model.
- Handle failures with Error codes and retry strategy.