GuideImage Generation

gpt-image-2 Model Tutorial

Tutorial for calling gpt-image-2 on AnyRouter: text-to-image and image edit endpoints, request parameters, cURL and Python examples, and response format.

Quickly get started with the text-to-image and image editing endpoints of the gpt-image-2 model via NewAPI

Prerequisites

Before making any calls, make sure you have the following credentials:

link

Base URL

The API endpoint, e.g.: https://anyrouter.win

key

API Key

An API key generated in the AnyRouter console, e.g.: sk-xxxxxxxxxxxx


1. Text to Image

This endpoint generates a brand-new image from scratch based on your text prompt.

Request

/v1/images/generations/required

Standard OpenAI-compatible image generation path.

Request Format

application/json

Request Parameters

stringrequired

Always set to gpt-image-2.

stringrequired

Describe the image you want to generate. Both Chinese and English are supported.

integer

Number of images to generate.

string

Image resolution, e.g. 1024x1024 or 512x512.

string

Quality of the generated image.

Code Examples

curl [https://anyrouter.win/v1/images/generations/](https://anyrouter.win/v1/images/generations) \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxxxxxxxxx" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A futuristic city with flying cars at sunset, cyberpunk style, highly detailed",
    "n": 1,
    "size": "1024x1024"
  }'
import requests

url = "[https://anyrouter.win/v1/images/generations/](https://anyrouter.win/v1/images/generations)"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-xxxxxxxxxxxx"
}
data = {
    "model": "gpt-image-2",
    "prompt": "A cute cat wearing sunglasses on a beach vacation",
    "n": 1,
    "size": "1024x1024"
}

response = requests.post(url, json=data, headers=headers)
print(response.json())

2. Image Edits

This endpoint modifies an existing image. You need to provide an original image and a mask (the transparent areas of the mask indicate the regions to be modified/replaced).

Request

/v1/images/edits/required

Standard OpenAI-compatible image editing path.

Request Format

multipart/form-data

Request Parameters

stringrequired

Always set to gpt-image-2.

stringrequired

Describe the image you want to generate. Both Chinese and English are supported.

integer

Number of images to generate.

string

Image resolution, e.g. 1024x1024 or 512x512.

string

Format in which the generated image is returned. Must be url or b64_json.

file

The image to edit. Must be a valid PNG file, less than 4MB, and square. If no mask is provided, the image must have transparency, which will be used as the mask.

Code Examples

curl -X POST "https://anyrouter.win/v1/images/edits/" \
  -H "Authorization: Bearer " \
  -F image="cmMtdXBsb2FkLTE2ODc4MzMzNDc3NTEtMjA=/31225951_59371037e9_small.png" \
  -F prompt="A cute baby sea otter wearing a beret."
import requests

url = "https://anyrouter.win/v1/images/edits/"
body = {
  "image": "cmMtdXBsb2FkLTE2ODc4MzMzNDc3NTEtMjA=/31225951_59371037e9_small.png",
  "prompt": "A cute baby sea otter wearing a beret."
}
response = requests.request("POST", url, data = body, headers = {
  "Content-Type": "multipart/form-data",
  "Authorization": "Bearer "
})

print(response.text)

3. Response Structure

On success, the endpoint returns a JSON object containing the image URL.

Successful Response Example

{
  "created": 1718112345,
  "data": [
    {
      // depends on the response_format field
      // "url": "[https://cdn.your-domain.com/images/abc123xyz.png](https://cdn.your-domain.com/images/abc123xyz.png)",
      "b64_json": "xxx"
    }
  ]
}

On this page