Skip to main content
Create images from text, or upload an image and describe the changes. Both endpoints use the OpenAI Images response format. For Gemini-native image generation and editing with Nano Banana models, see the Nano Banana API overview. To compare image models and endpoints, see Choose an image API.

Set up your credentials

Create a model API key in Token management and make it available as the LAOZHANG_API_KEY environment variable. Check that its group has access to gpt-image-2-vip in Models and pricing. Set the key in your terminal, replacing the placeholder:
The examples below use:
  • API host: https://api.laozhang.ai
  • OpenAI SDK base URL: https://api.laozhang.ai/v1
  • Authentication: Authorization: Bearer $LAOZHANG_API_KEY
  • Model: gpt-image-2-vip
Install the Python packages used to call the API and save images. The cURL examples also use Python to decode the response and choose the correct file extension.

Save images from a response

Save this helper as save_images.py in the directory where you run the examples. It writes every image in data[] to disk, whether the entry holds b64_json or a url:
Run it as python save_images.py <response.json> <name> after a cURL request, or import save_images in Python.

Generate an image

Send a JSON body containing model and prompt to:
These examples create a product image and save it as generated.png, generated.jpg, or generated.webp, according to the returned file format.
After the request completes, save the image from generation-response.json:
Open the saved image to see the result. The .json file contains the API response; changing its extension to .png does not convert it into an image.

Edit an image

Place an image named input.png in the directory where you run the command. Send the file and your instructions as multipart form data to:
The examples below change the background and save the result as edited.png, edited.jpg, or edited.webp.
After the request completes, save the image from edit-response.json:
image[]=@input.png uploads the file contents. A plain path string in JSON does not upload a local file. When using cURL’s -F option, let cURL set the multipart content type and boundary; do not add Content-Type: application/json. Describe both the change you want and the details to preserve. Image editing is generative, so an instruction to keep the subject unchanged does not guarantee identical pixels outside the edited area.

Request options

The minimal examples use the model’s default output settings. To choose dimensions or quality, use the options listed for the exact model in the GPT Image 2 guide or GPT Image 2.5 guide. GPT Image and DALL·E use different options. Do not copy DALL·E’s quality="hd" or style into a GPT Image request. Masks, multiple reference images, transparent backgrounds, and output-format controls also require model-specific instructions; use the basic file upload above when editing a single local image.

Read image results

Each entry in data[] is one output image, returned in one of two forms:
  • b64_json holds the Base64-encoded image bytes. Some responses add a data:image/...;base64, prefix or drop the trailing = padding, so strip the prefix and restore the padding before decoding, as save_images.py does.
  • url is a download link. Fetch it promptly without your LaoZhang API key and keep your own copy; URL results may expire.
When requesting more than one image, process every entry in data[]. An image-generation task ID or a Gemini candidates response uses a different format and needs the handler for that API.

Errors and retries

  • 400: check the request fields and model options. For edits, confirm that the file was uploaded as multipart data.
  • 401 or 403: check the API key, token group, and model permissions.
  • 404 or an unavailable-model error: check the endpoint and exact model ID in Models and pricing.
  • 429: read the error body to distinguish rate or concurrency limits from account balance issues.
  • 5xx or timeout: inspect call logs before submitting the image request again.
A client timeout does not cancel server-side generation. The Python examples disable automatic SDK retries so that your application can decide whether to repeat a request. Current prices and billing units are shown in the console; service and refund terms are covered by the Terms.