GPT Image 2: Your First Image API Request
Send your first GPT Image 2 request through BetterToken, save the b64_json image, and troubleshoot key, model, and balance errors.
Contents
Generating your first image through BetterToken takes a single HTTP request. You need your own API Key and a POST request to https://www.bettertoken.ai/v1/images/generations. Set the model to gpt-image-2 and provide a text description in the request body. The response returns the image as b64_json, which you then decode and save to a file.
BetterToken provides an OpenAI-compatible Image API, but it is an independent service: your key, balance, and request history belong to your BetterToken account, not OpenAI. Before you begin, create a key in the BetterToken dashboard and check the current terms on the models and pricing page.
What you need
curlto send the request;- your own BetterToken API Key;
- the exact Base URL
https://www.bettertoken.ai/v1; - the
gpt-image-2model for this Image API; - a Base64 decoder or a short Python script.
Do not put a real key in source code, screenshots, or a command that will remain in your shell history. Store it in an environment variable:
export BETTERTOKEN_API_KEY="your_api_key_here"
The value above is a placeholder. Use your own key and never publish it.
Your first GPT Image 2 request
Ready to make a request with your own key? Create a BetterToken account, obtain an API Key, and keep it out of source code. Create a BetterToken account
Send a request to the image generation endpoint:
curl https://www.bettertoken.ai/v1/images/generations \
-H "Authorization: Bearer $BETTERTOKEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A clean editorial illustration of a coding desk at night, green and graphite palette",
"n": 1,
"size": "1024x1024",
"response_format": "b64_json",
"output_format": "png"
}' \
-o image-response.json
This is a synchronous contract: the shown first request needs no separate task ID, polling, or callback. Read data[0].b64_json, decode it, and save the PNG.
Saving the image
The following example reads the JSON, decodes the Base64 value, and creates bettertoken-image.png. It uses only Python’s standard library:
import base64
import json
with open("image-response.json", "r", encoding="utf-8") as source:
payload = json.load(source)
image_base64 = payload["data"][0]["b64_json"]
with open("bettertoken-image.png", "wb") as target:
target.write(base64.b64decode(image_base64))
print("Saved: bettertoken-image.png")
Open the file and check that it matches the prompt. An HTTP 200 confirms that the request worked, but it does not replace a visual review of the result.
Improving the second request
For the first test, use a short prompt with four clear parts:
- subject or scene;
- visual style;
- composition;
- palette or lighting.
For example:
Editorial illustration of a developer reviewing an API response,
clean geometric style, centered composition, dark graphite background
with restrained green accents, no text, no logos
Avoid starting with a long list of conflicting requirements. Check the basic composition first, then change one parameter at a time. This makes it easier to tell which wording affected the result.
Sizes, quality, and formats are current parameters: choose supported values in the current Image API Docs before a second request. This contract does not establish asynchronous tasks or callbacks, batch processing, seller templates, or guaranteed image quality.
Common errors
401: key rejected
Check that BETTERTOKEN_API_KEY is set in the current terminal and that the authorization header includes the Bearer prefix. Do not print the key with echo or send the full value to support.
402 or insufficient balance
Open the BetterToken dashboard and check the available balance. Paid BetterToken balance does not reset automatically at the end of the month, but every request still requires enough remaining balance.
404: incorrect path
Image generation uses this full path:
https://www.bettertoken.ai/v1/images/generations
Do not replace it with the Chat Completions endpoint or use api.openai.com: a BetterToken key works with the BetterToken endpoint.
400: incorrect model or parameters
Start with only the required model and prompt fields. This guide uses gpt-image-2. If the API reports that the model is unavailable, compare the current Model ID and parameters with the Image API documentation.
429 or 5xx
Do not start an infinite retry loop. Record the HTTP status, request time, and a safe portion of the response, then retry after a delay. In BetterToken Dashboard, match the request by time and inspect only the model, status, and actual charge.
Checks before application integration
Before moving the request into a backend or automation flow, confirm that:
- the key is stored in an environment variable or secrets manager;
- the request goes to BetterToken rather than another provider’s endpoint;
- the model and parameters come from the current documentation;
- the Base64 payload decodes without errors;
- the application limits retries and handles non-2xx responses;
- cost is checked against the current pricing page rather than an old review.
The short version
The first working flow has three steps: send a POST request to the BetterToken Image API, read data[0].b64_json, and decode it into a file. Check current parameters and prices before running the request.