GPT Image 2: Your First Image API Request

Your first image through BetterToken takes one 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 prompt. The response contains the image in b64_json, which you decode and save as 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 start, create a key in the BetterToken dashboard and check current terms on the models and pricing page.

What you need

  • curl to send the request;
  • your own BetterToken API key;
  • the exact base URL https://www.bettertoken.ai/v1;
  • the gpt-image-2 model 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 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 workflows: you can save the payload to object storage, pass it to another processing step, or decode it immediately.

How to save the image

The following example reads the JSON, decodes Base64, 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 confirm that it matches your prompt. An HTTP 200 response proves the request completed, but it does not replace a visual review of the result.

How to improve the second request

For an initial test, use a short prompt with four clear parts:

  1. the subject or scene;
  2. the visual style;
  3. the composition;
  4. the 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

Do not start with a long list of conflicting requirements. Confirm the basic composition first, then change one parameter at a time. This makes it easier to see which instruction changed the output.

Common errors

401: the key was rejected

Confirm 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 complete value to support.

402 or insufficient balance

Open your BetterToken account and check the available balance. Paid BetterToken balance does not reset automatically at the end of the month, but each request still requires enough balance.

404: incorrect path

Image generation uses this full path:

https://www.bettertoken.ai/v1/images/generations

Do not replace it with a Chat Completions endpoint or api.openai.com: a BetterToken key works with the BetterToken endpoint.

400: invalid model or parameters

First reduce the body to the required model and prompt fields. This guide uses gpt-image-2. If the API reports that the model is unavailable, check the current model ID and parameters in the Image API documentation.

429 or 5xx

Do not start an unlimited retry loop. Record the HTTP status, request time, and a safe portion of the response, then retry with a delay. The BetterToken Dashboard lets you match the request by time and inspect the model, status, input/output/cache tokens, and cost without exposing the full prompt.

Checklist before application integration

Before moving the request into a backend or automation, confirm that:

  • the key is stored in an environment variable or secrets manager;
  • the request goes to BetterToken rather than another provider;
  • the model and parameters come from current documentation;
  • Base64 decoding completes without errors;
  • the application limits retries and handles non-2xx responses;
  • pricing was checked on the current pricing page, not copied from an old review.

You can then move the same contract into your application's SDK or HTTP client. Start with one minimal request and one saved result before adding sizes, quality options, batch processing, or your own storage layer.

Short answer

The first working loop 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 current parameters and prices before running the request.

Related articles