Skip to main content
POST
cURL
POST /v1/images/generations The text-to-image endpoint uses an application/json request body. Submit a prompt, keep the HTTP request open, and read the generated image from data[0].b64_json in the same response.
Use https://bettertoken.ai/v1 as the Base URL. Pass your BetterToken API Key through Authorization: Bearer YOUR_API_KEY.
You can enter Authorization and the request body in the Playground on the right side of the page, then send the request directly to https://bettertoken.ai/v1/images/generations.
Do not put API keys in frontend browser code, Git repositories, tickets, screenshots, or logs. For server-side proxy calls, store API keys only in server environment variables or a secret manager.

Choose the right workflow

  • Generate an image from text and save the result: stay on this page. Start with the curl example, then use Save the image to decode data[0].b64_json.
  • Edit an existing image: use Image to image, which sends one or more source files as multipart/form-data.
  • Choose a model, review parameters, and estimate budget: check current Model IDs and prices in the model plaza, then use Recommended values below for request parameters. The GPT Image 2 first-request article provides the broader request-and-response context.

Quick start with curl

Use https://bettertoken.ai/v1 as the Base URL and send a request to https://bettertoken.ai/v1/images/generations:
Send these fields explicitly in every request:
Generate multiple images by sending multiple independent requests. Do not rely on a single request with n > 1. size represents the expected aspect ratio and size tier. The actual returned pixels may be mapped or adjusted by the server. Use the decoded image dimensions instead of forcibly cropping the output to the requested value.

Save the image

A successful response follows the OpenAI-compatible image response shape:
Read data[0].b64_json and save it as base64 image content. The response may include extra fields such as revised_prompt; allow these fields in your client. Always set output_format: "png". Then save the decoded image as .png without inspecting file headers.

JavaScript example (Node.js)

Do not rely on output_format: "jpeg" or output_format: "webp" to directly receive JPEG or WebP files. The current endpoint may still return PNG image content. If your product needs JPEG or WebP, receive PNG first and convert it in your own code.

Response flow

This endpoint is synchronous. After sending POST /images/generations, keep the current HTTP request open until the server responds. When generation succeeds, the image content is returned in data[0].b64_json. The endpoint does not return a task_id, and there is no separate status query or result download endpoint.

Timeouts and retries

  • Set HTTP client timeouts to several minutes.
  • Retry transport errors, 408, 409, 425, 429, and 5xx.
  • Do not retry 400, 401, missing parameters, or malformed requests automatically.
  • Use exponential backoff such as 3s, 8s, and 15s.
  • If duplicate images are unacceptable, record your own request ID before retrying.

Error handling

Errors usually return JSON. When showing an error, read error.message first, then message, then the HTTP status text.

Integration checklist

  • The Base URL is https://bettertoken.ai/v1.
  • The header contains Authorization: Bearer YOUR_API_KEY.
  • The request uses application/json and POST /images/generations.
  • model is gpt-image-2, response_format is b64_json, output_format is png, and n is 1.
  • size is one of the recommended values, such as 1024x1024.
  • Your HTTP client allows several minutes for generation.

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json
model
enum<string>
default:gpt-image-2
required

Use the GPT Image 2 model ID.

Available options:
gpt-image-2
Example:

"gpt-image-2"

prompt
string
required

A detailed description of the image to generate.

Example:

"A futuristic AI product poster on a light background, with glass textures, a clean composition, and a premium technology aesthetic."

n
default:1

Use 1. Send separate requests when you need multiple output images.

Example:

1

size
enum<string>
default:1024x1024

The requested image size or aspect-ratio tier. The server may map or adjust the final pixel dimensions, so inspect the decoded image for its actual size.

Available options:
auto,
1024x1024,
1536x1024,
1024x1536,
1536x1152,
1152x1536,
2048x2048,
2048x1152,
3840x2160,
2160x3840
Example:

"1024x1024"

response_format
enum<string>
default:b64_json

Use b64_json.

Available options:
b64_json
Example:

"b64_json"

output_format
enum<string>
default:png

Use png. Do not rely on jpeg or webp being returned directly in the selected format.

Available options:
png
Example:

"png"

Response

Image generation result.