TokenLab

Images

Edit Image

Edit an image given a prompt and source image

POST
/v1/images/edits

Overview

Creates an edited or extended image given an original image and a prompt.

This endpoint supports both:

  • the OpenAI-compatible multipart/form-data upload flow documented below
  • JSON requests that provide image_url, image_urls, or official images references for supported image-to-image families

gpt-image-2 is supported here. It accepts multipart image uploads, JSON image_url / image_urls, and official images[] references (image_url or file_id), with up to 16 source images. Create file_id values through /v1/files first. Set async: true to return a task first; official FLUX/BFL edit models also use the same task polling flow.

gpt-image-2 edits do not accept resolution; use size for output dimensions. background accepts auto or opaque; transparent is not supported. For multi-image or high-latency edits, prefer async: true and poll the returned task.

Nano Banana reference-image requests (nano-banana-edit, nano-banana-2, and nano-banana-pro) are exposed on /v1/images/generations with operation: "image-to-image" and image_urls, not on this /v1/images/edits endpoint.

xAI Grok Imagine image edit models (grok-imagine-image, grok-imagine-image-quality, and legacy grok-imagine-image-pro) accept at most 3 source images. Requests with more than 3 source images fail input validation with 400 too_many_images.

input_fidelity is not part of the current TokenLab supported fields for gpt-image-2; omit it or the request returns 400 unsupported_parameter.

Request Body

Synchronous request timeout: Some image requests return the final image inline and wait for generation to finish. High-resolution or high-quality requests can take close to a minute or longer, so set your HTTP client timeout to at least 120s. If the create response includes status: "pending", task_id, or poll_url, follow the returned poll_url instead.

Remote image URLs: when multipart input is needed, TokenLab fetches JSON image_url, image_urls, or images[].image_url and sends the bytes as multipart image parts. URLs must be public http/https, without embedded credentials or fragments, and must not resolve to localhost, private, or reserved IP ranges; each redirect is checked again. The fetched payload must be a real PNG, JPEG, or WebP image. Limits are 50 MiB per image, 200 MiB total for URL-fetched images in one request, 30s fetch timeout, and up to 3 redirects.

For JSON requests, provide exactly one of image_url, image_urls, or images. Each images[] object must contain exactly one of image_url or file_id. The 200 MiB aggregate limit includes all source images and the mask.

imagefile

Multipart source images. Repeat image to provide multiple GPT Image sources. Files must be PNG, JPEG, or WebP, up to 16 source images and 50 MiB each. xAI Grok Imagine edit models use the same input fields but cap source images at 3.

promptstringrequired

A text description of the desired edit.

maskfile | object

An additional image whose fully transparent areas indicate where the image should be edited. Must be a valid PNG file, less than 50 MiB, and have the same dimensions as image.

For JSON requests, mask may also be an object with exactly one of image_url or file_id; file_id values must come from /v1/files and remain bound to the same image-edit configuration.

modelstringrequired

The model to use for image edits. Use gpt-image-2 for GPT Image edits, or another current image-edit model returned by GET /v1/models?recommended_for=image.

nintegerdefault: 1

Number of images to generate (1-10, model dependent).

sizestring

The size of the generated image. For gpt-image-2, use auto or WIDTHxHEIGHT; dimensions must be multiples of 16, longest edge at most 3840px, long/short ratio at most 3:1, and total pixels between 655,360 and 8,294,400.

response_formatstringdefault: url

The format in which generated images are returned. Must be url or b64_json; the default is url.

url returns each image in data[].url; b64_json returns Base64 image data in data[].b64_json.

asyncbooleandefault: false

Set to true with gpt-image-2 or official FLUX/BFL edit models to return a task before the final image is ready. Completed async edits return URLs regardless of the requested response_format; use synchronous requests when you need b64_json.

userstring

A unique identifier representing your end-user for abuse monitoring.

Response

createdinteger

Unix timestamp of when the images were created.

dataarray

Array of generated images.

Each object contains:

  • url (string): URL of the edited image (if response_format is url)
  • b64_json (string): Base64-encoded image (if response_format is b64_json)

Async Task Response

Set async: true with gpt-image-2 or official FLUX/BFL edit models to create a task instead of waiting for the edited image in the request. The response includes status: "pending", task_id, and poll_url. Poll /v1/tasks/{task_id} until the task reaches completed or failed.

Async edit tasks return final image URLs only. If you need raw b64_json image data, use a synchronous request.

Billing may reserve the estimated amount when the task is created. Completed tasks are billed by actual usage, and failed or timed-out tasks are released or refunded.

Request

curl -X POST "https://api.tokenlab.sh/v1/images/edits" \
  -H "Authorization: Bearer sk-your-api-key" \
  -F "model=gpt-image-2" \
  -F "image=@sunlit_lounge.png" \
  -F "mask=@mask.png" \
  -F "prompt=A sunlit indoor lounge area with a pool" \
  -F "n=1" \
  -F "size=1024x1024"

Response

Response
{
  "created": 1706000000,
  "data": [
    {
      "url": "https://..."
    }
  ]
}

Notes

Remote image fetch failures are returned as input errors before generation begins. Unreachable URLs, timeouts, 403/404 responses, private/internal hosts, credentials or fragments in the URL, non-image content, unsupported formats, and size-limit violations return 400 or 413 and identify the image_url / image_urls[n] input. For private or header-protected assets, upload multipart image files directly or create /v1/files references.

Hy Image 3.5 Preview creates 1024-pixel square images from text and edits reference images using written instructions. It is useful for drafting visuals and refining a composition.

{
  "model": "hy-image-v3.5-preview",
  "prompt": "Change the table to pale blue",
  "image_url": "https://example.com/reference.png",
  "size": "1024x1024",
  "n": 1,
  "response_format": "url"
}

Authorization

BearerAuth
AuthorizationBearer <token>

API Key authentication. Create or manage API keys in Dashboard > API > API Keys.

In: header

Headers

X-TokenLab-Delivery-Policy?string

Per-request Delivery policy. Overrides the API key and Workspace defaults. Auto tries TokenLab Verified first and may switch once to Official only before output, request acceptance, or persistent resource creation.

Value in

  • "auto"
  • "verified"
  • "official"

Request Body

Response

application/json

application/json

application/json

application/json

application/json

application/json