GPT Image 2: Your First Image API Request
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
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"
}' \
-o image-response.json
If the request succeeds, image-response.json contains a data object. The image is in data[0].b64_json. This response format works well in server-side flows: you can store the payload in object storage, pass it to the next processing step, or decode it directly into a local file.
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.
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 the BetterToken Dashboard, you can match the request by time and inspect the model, status, input/output/cache Token usage, and charge without viewing the full prompt.
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.
You can then move the same contract into your application's SDK or HTTP client. Begin with the minimal request, save one result, and only then add dimensions, quality settings, batch processing, and your own storage layer.
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. If you need your own key and pay-as-you-go billing, create a BetterToken account and check the current parameters and prices before running the request.
Originally published on the BetterToken blog.
BetterToken provides pay-as-you-go access to AI model APIs through
OpenAI-compatible and Anthropic-compatible endpoints — useful if you are wiring
Claude Code, Codex, or your own tooling to a custom base URL.
See the docs to get started.
Top comments (0)