GPT Image API
Call gpt-image-2 through the LMU AI OpenAI Images-compatible API for text-to-image, image editing, parameters, Base64 saving, model lookup, and error fixes.
LMU AI provides an OpenAI Images-compatible API for text-to-image and image editing / image-to-image using gpt-image-2.
Base URL
OpenAI SDK:
https://api.lmuai.com/v1When writing HTTP requests by hand, use the full endpoints:
POST https://api.lmuai.com/v1/images/generations
POST https://api.lmuai.com/v1/images/edits1. API overview
| Method | Path | Content-Type | Description |
|---|---|---|---|
GET | /v1/models | — | Query the models available to your current API key |
POST | /v1/images/generations | application/json | GPT text-to-image, synchronous response |
POST | /v1/images/edits | multipart/form-data | GPT image editing / image-to-image, synchronous response |
No GPT multi-item batch API yet
/v1/images/batches currently supports only Gemini and cannot accept gpt-image-2. To generate multiple GPT images, have the client send requests one at a time and manage concurrency and RPM yourself.
The production environment currently has no async image job endpoint enabled either, so rely on the two synchronous APIs on this page.
2. Authentication
Authorization: Bearer YOUR_API_KEYDo not put your API key in browser front-end code, public repositories, URL query strings, or logs. We recommend calling from your own server.
3. Query models
curl https://api.lmuai.com/v1/models \
-H "Authorization: Bearer YOUR_API_KEY"Look for the image model in the response's data[].id, for example:
{
"object": "list",
"data": [
{"id": "gpt-image-2", "object": "model"}
]
}The model list is determined by the group your API key belongs to. Different API keys on the same site may return different models.
4. Text-to-image
POST /v1/images/generations
curl https://api.lmuai.com/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A red ceramic mug, centered on a light-gray studio background, soft side lighting, no text",
"n": 1,
"size": "1024x1024",
"quality": "low",
"output_format": "png"
}'Python SDK
from openai import OpenAI
import base64
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.lmuai.com/v1",
)
result = client.images.generate(
model="gpt-image-2",
prompt="A red ceramic mug, light-gray studio background, soft side lighting, no text",
size="1024x1024",
quality="low",
)
item = result.data[0]
if item.b64_json:
with open("gpt-output.png", "wb") as f:
f.write(base64.b64decode(item.b64_json))
elif item.url:
print(item.url)
else:
raise RuntimeError("No valid image in the response")JavaScript
import OpenAI from "openai";
import fs from "node:fs";
const client = new OpenAI({
apiKey: process.env.LMU_API_KEY,
baseURL: "https://api.lmuai.com/v1",
});
const result = await client.images.generate({
model: "gpt-image-2",
prompt: "A red ceramic mug, light-gray studio background, soft side lighting, no text",
size: "1024x1024",
quality: "low",
output_format: "png",
});
const item = result.data?.[0];
if (item?.b64_json) {
fs.writeFileSync("gpt-output.png", Buffer.from(item.b64_json, "base64"));
} else if (item?.url) {
console.log(item.url);
} else {
throw new Error("No valid image in the response");
}5. Text-to-image parameters
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Recommended | Currently gpt-image-2 |
prompt | string | Yes | Image description |
n | integer | No | Number of images; the available range is determined by the model and upstream channel |
size | string | No | Requested size, e.g. 1024x1024; the actual pixel dimensions follow the returned image |
quality | string | No | Quality tier, e.g. low, medium, high; subject to model capabilities |
background | string | No | Background setting, e.g. transparent background; subject to model capabilities |
output_format | string | No | png, jpeg, webp, etc.; subject to model capabilities |
output_compression | integer | No | Compression quality for formats such as JPEG / WebP |
response_format | string | No | Response-format compatibility parameter; clients should still check both b64_json and url |
moderation | string | No | Content-moderation parameter; subject to model capabilities |
stream | boolean | No | Toggle for streaming image responses; for normal server-side calls we recommend non-streaming |
partial_images | integer | No | Number of partial images in streaming scenarios; subject to model capabilities |
size is not a guaranteed crop
Different upstream accounts and image backends may map or normalize size to their capabilities. Even if you request 1024x1024, the actual image pixels may differ. When you need a fixed ratio or pixel size, read the output file dimensions and crop or resize on your side.
6. Image editing / image-to-image
POST /v1/images/edits
GPT image editing uses multipart/form-data:
curl https://api.lmuai.com/v1/images/edits \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=Keep the mug's shape, composition, and lighting; change the mug from red to green; no text" \
-F "image=@./input.png" \
-F "size=1024x1024" \
-F "quality=low" \
-F "output_format=png"Python SDK:
from openai import OpenAI
import base64
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.lmuai.com/v1",
)
with open("input.png", "rb") as image_file:
result = client.images.edit(
model="gpt-image-2",
image=image_file,
prompt="Keep the subject and composition; change the background to a neon street on a rainy night",
size="1024x1024",
quality="low",
)
item = result.data[0]
if item.b64_json:
with open("gpt-edited.png", "wb") as f:
f.write(base64.b64decode(item.b64_json))If the model and channel support mask editing, you can add:
-F "mask=@./mask.png"The edits endpoint also accepts parameters such as input_fidelity, background, output_format, and output_compression; the exact effect depends on model capabilities.
7. Response format
A typical GPT image response:
{
"created": 1760000000,
"data": [
{
"b64_json": "iVBORw0KGgoAAA..."
}
],
"background": "opaque",
"output_format": "png",
"quality": "low",
"size": "1024x1024",
"model": "gpt-image-2",
"usage": {
"input_tokens": 48,
"output_tokens": 186,
"total_tokens": 234
}
}Clients should perform three levels of validation:
- Whether the HTTP status code is
2xx; - Whether
datais a non-empty array; - Whether a non-empty
b64_jsonorurlexists indata[].
When you get HTTP 200 but no valid image field, treat it as a business-level failure.
8. Common errors
| HTTP | Common cause | Recommended action |
|---|---|---|
400 | Bad request body, image format, parameter, or model | Check the JSON / multipart, field names, and model ID |
401 | Invalid API key | Check the Bearer header and do not add spaces around the key |
403 | The group your API key belongs to does not have image generation enabled | Contact the admin to check the group's image permissions |
404 | Wrong path, or the image API is not supported for the current group | Confirm you are using /v1/images/generations or /v1/images/edits |
429 | Concurrency, RPM, or upstream quota limits | Use exponential backoff and lower concurrency and RPM |
5xx | The upstream or API relay is temporarily unavailable | Log the request ID and retry a limited number of times |
When troubleshooting, save the request ID from the response headers and provide it to the admin; do not send your full API key.
9. Differences from other image APIs
| Need | Recommended doc |
|---|---|
| Gemini native text-to-image, image-to-image, 1K / 2K / 4K | Gemini Image API |
| GPT text-to-image and editing | This page |
| Grok text-to-image and editing | Grok Image API |
| Submit multiple Gemini prompts at once | Gemini Batch Image API |
Last updated:
Get an LMU AI key and start using Claude, Codex and more
Free sign-up, flexible plans, one key across Claude Code, Codex CLI, Cursor, VS Code, OpenCode, Cherry Studio and other AI tools.
Sign upGemini Image API
Use LMU AI's native Gemini v1beta endpoint for text-to-image, image editing, and image-to-image, with 1K / 2K / 4K, common aspect ratios, and Base64 parsing.
Grok Image API
Call Grok image models via the LMU AI OpenAI Images-compatible API: text-to-image, editing, URL and Base64 input, downloads, model choice, and error fixes.