GPT Image API is OpenAI's official endpoint for generating images from text. To send your first request, you need an API key with model access, Python or curl, and the gpt-image-2 model. The response contains base64 data that you decode and write to a file. This guide covers the shortest working path from a key to a verified result.
What you need for the first request
Before you start, prepare three things:
- API Key — create one at platform.openai.com. The model page marks the free tier as unsupported; check your API dashboard for the tier and limits available to your account.
- Python or curl with the
jqandbase64utilities. - The Python
openaipackage:python -m pip install --upgrade openai.
The gpt-image-2 model uses the Image API endpoint POST https://api.openai.com/v1/images/generations. Check the Image generation guide and the gpt-image-2 model page for current parameters.
How to pass the API key safely
Do not paste an API key directly into source code. If that file reaches a repository or another machine, revoke the key immediately.
Use an environment variable instead:
export OPENAI_API_KEY="your_key_here"
The Python SDK reads this variable when it initializes the client. curl substitutes it as $OPENAI_API_KEY. If your application loads a .env file, add that file to .gitignore; the SDK itself does not export .env values into the current shell.
For developers who need alternative API access, BetterToken.ai is one example of a service that provides an OpenAI-compatible API key and Base URL with pay-as-you-go billing and no fixed monthly subscription. Interface compatibility alone does not prove support for
gpt-image-2: this guide sends requests to OpenAI's official endpoint, while BetterToken's available models should be checked on its pricing page.
Minimal GPT Image API request
Python
import base64
from pathlib import Path
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
result = client.images.generate(
model="gpt-image-2",
prompt="A red apple on a white background, photorealistic",
size="1024x1024",
)
image_bytes = base64.b64decode(result.data[0].b64_json)
Path("output.png").write_bytes(image_bytes)
print("Saved: output.png")
curl
curl https://api.openai.com/v1/images/generations \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A red apple on a white background, photorealistic",
"size": "1024x1024"
}' \
| jq -r '.data[0].b64_json' \
| base64 --decode > output.png
On macOS, the built-in base64 utility may require -D instead of --decode. If jq is not installed, install it with the package manager appropriate for your system.
Available size values and constraints depend on the model, so verify them in the current documentation.
How to save and verify the image
After the command completes, the current directory should contain output.png.
Step 1. Open the file in an image viewer. If it opens, the request reached the API and base64 decoding succeeded.
Step 2. Check the dimensions with Python after installing Pillow with python -m pip install Pillow:
from PIL import Image
img = Image.open("output.png")
print(img.size) # expect (1024, 1024)
Or use ImageMagick if it is installed:
identify output.png
# output.png PNG 1024x1024 ...
Or use the standard file command:
file output.png
# output.png: PNG image data, 1024 x 1024, 8-bit/color RGB...
Step 3. If the dimensions match the requested size, the request completed successfully.
If the file is empty or will not open: the API probably returned an error JSON that was saved instead of image bytes. Remove the decoding pipe (| base64 --decode > output.png) and inspect the raw response for an error field.
Fixing authentication, model, and parameter errors
401 Unauthorized
The request failed at authentication. Common causes include:
OPENAI_API_KEYis unset or was set in another shell session. Check presence without printing the key:
if test -n "${OPENAI_API_KEY:-}"; then
printf 'set\n'
else
printf 'not set\n'
fi
If it prints not set, define the variable in the current session and restart the application that sends the request.
- A space or newline was copied into the key.
- The key was deleted or deactivated at platform.openai.com/api-keys.
Incorrect model name
The model field requires an exact string. This example uses gpt-image-2; verify the current name and availability on the model page.
400 Bad Request (parameters)
- The
sizevalue does not meet the selected model's constraints. Check supported dimensions in the Image generation guide. gpt-image-2currently does not supportbackground: "transparent". Remove this parameter or use a supported value.
The file contains JSON instead of an image
This happens when a curl command writes the API response to a file before decoding it. First print the response without redirection, confirm that .data[0].b64_json exists, and only then add the pipe.
Current request parameters, response formats, and supported sizes are documented in the official Image generation guide and GPT Image 2 model page.