Image to image (image editing)
Generate or edit images from reference images with GPT Image 2 through BetterToken’s OpenAI-compatible API. Includes setup steps, examples, and common fixes.
POST /v1/images/edits
The image-to-image endpoint uses a multipart/form-data request body. Upload one or more reference images and submit prompt and parameter fields as form fields.
https://bettertoken.ai/v1 as the Base URL. Pass your BetterToken API Key through Authorization: Bearer YOUR_API_KEY.Recommended values
Send these fields explicitly in every request:image for a single reference image. Use repeated image[] fields for multiple reference images.
Single reference image
Multiple reference images
Edit a selected area with a mask
Add the optionalmask file field when you want to guide editing to a selected area. The current BetterToken endpoint reads one mask file. Do not send repeated mask[] fields.
- The original image and mask must use the same file format and pixel dimensions. Each file must be smaller than 50 MB. For maximum compatibility, use PNG with an Alpha channel for both files.
- The mask must contain an Alpha channel. Fully transparent pixels (
Alpha 0) mark the area the model may redraw. Fully opaque pixels (Alpha 255) guide the model to preserve that area. - A regular RGB black-and-white image is not a valid mask unless it actually contains an Alpha channel.
- If you send multiple
image[]fields, the mask applies to the first input image. - The mask guides the model. It does not guarantee that pixels outside the transparent area remain absolutely identical.
Masked edit with Python
Open both files in onewith statement so Python closes them after the request:
Check the files before upload
This minimal Pillow check verifies the required format, dimensions, and Alpha channel. For a typical local mask, it also warns when the Alpha channel does not include both fully transparent and fully opaque areas:Python example
Recommended sizes
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:data[0].b64_json and save it as base64 image content. Always set output_format: "png" so you can save the decoded image as .png.
Response flow
This endpoint is synchronous. After sendingPOST /images/edits, 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.
Related docs
Authorizations
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
Body
Use the GPT Image 2 model ID.
gpt-image-2 "gpt-image-2"
Instructions for editing the image or generating from reference images.
"Use this reference image to create a more polished square product hero image while preserving the main style."
The reference image field for a single input image.
Repeat this field for each input when sending multiple reference images.
An optional single mask file. The mask and original image must have the same format and dimensions, each file must be smaller than 50 MB, and the mask must contain an Alpha channel. For maximum compatibility, use PNG with an Alpha channel for both files. With multiple image[] inputs, the mask applies to the first image.
Use 1. Send separate requests when you need multiple output images.
"1"
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.
auto, 1024x1024, 1536x1024, 1024x1536, 1536x1152, 1152x1536, 2048x2048, 2048x1152, 3840x2160, 2160x3840 "1024x1024"
Use b64_json.
b64_json "b64_json"
Use png. Do not rely on jpeg or webp being returned directly in the selected format.
png "png"

