LMU AI Docs
Open API

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.

LMU AI provides Grok image generation and image editing through the OpenAI Images-compatible path.

Base URL

OpenAI-compatible SDK:

https://api.lmuai.com/v1

Full HTTP endpoints:

POST https://api.lmuai.com/v1/images/generations
POST https://api.lmuai.com/v1/images/edits

1. API overview

MethodPathDescription
GET/v1/modelsQuery the models available to your current Grok group
POST/v1/images/generationsGrok text-to-image, synchronous response
POST/v1/images/editsGrok image editing / image-to-image, synchronous response

There is currently no publicly available Grok async job or multi-item batch API.

ScenarioRecommended model
Standard text-to-imagegrok-imagine-image
Quality-first text-to-imagegrok-imagine-image-quality
Image editing / image-to-imagegrok-imagine-image-quality

First query the models with your current API key:

curl https://api.lmuai.com/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY"

Use the quality model for image editing

The image editing examples use grok-imagine-image-quality. We do not recommend grok-imagine-edit as your default model: this compatibility name may appear in some model lists, but some upstream channels return 404 when it is called.

3. Authentication

Authorization: Bearer YOUR_API_KEY

Your API key must belong to a Grok group that has image generation enabled.

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": "grok-imagine-image",
    "prompt": "A blue ceramic mug, centered on a light-gray studio background, soft side lighting, no text",
    "n": 1,
    "size": "1024x1024"
  }'

JavaScript:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.LMU_API_KEY,
  baseURL: "https://api.lmuai.com/v1",
});

const result = await client.images.generate({
  model: "grok-imagine-image",
  prompt: "A blue ceramic mug, light-gray studio background, soft side lighting, no text",
  n: 1,
  size: "1024x1024",
});

const url = result.data?.[0]?.url;
if (!url) throw new Error("No image URL in the response");
console.log(url);

Python:

from openai import OpenAI
import requests

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.lmuai.com/v1",
)

result = client.images.generate(
    model="grok-imagine-image",
    prompt="A blue ceramic mug, light-gray studio background, soft side lighting, no text",
    n=1,
    size="1024x1024",
)

url = result.data[0].url
if not url:
    raise RuntimeError("No image URL in the response")

image = requests.get(url, timeout=60)
image.raise_for_status()
with open("grok-output.jpg", "wb") as f:
    f.write(image.content)

5. Text-to-image parameters

FieldTypeRequiredDescription
modelstringYesRecommended: grok-imagine-image or grok-imagine-image-quality
promptstringYesImage description
nintegerNoNumber of images; we recommend starting your tests with 1
sizestringNoOpenAI-compatible size parameter; the actual output size is determined by Grok upstream capabilities
response_formatstringNoResponse-format compatibility parameter; Grok usually returns a URL

Do not rely on size to force fixed output pixels

Grok channels may accept size as a compatibility or billing parameter, but the final image's pixels and aspect ratio are determined by the upstream generation result. When you need fixed pixels, download the image and crop or resize it yourself.

6. Image editing / image-to-image

POST /v1/images/edits

Grok image editing is best done with JSON; pass one of the following in image.url:

  • A publicly accessible HTTPS image URL; or
  • A data:image/...;base64,... Data URL.

Using an image URL

curl https://api.lmuai.com/v1/images/edits \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-imagine-image-quality",
    "prompt": "Keep the mug's shape, composition, and lighting; change the mug from blue to yellow; no text",
    "image": {
      "url": "https://example.com/input.jpg",
      "type": "image_url"
    },
    "response_format": "url"
  }'

Converting a local image to a Data URL

Python:

import base64
import mimetypes
import requests

api_key = "YOUR_API_KEY"
image_path = "input.jpg"
mime_type = mimetypes.guess_type(image_path)[0] or "image/jpeg"

with open(image_path, "rb") as f:
    data_url = f"data:{mime_type};base64,{base64.b64encode(f.read()).decode()}"

payload = {
    "model": "grok-imagine-image-quality",
    "prompt": "Keep the subject and composition; change the background to a seaside at dusk; no text",
    "image": {
        "url": data_url,
        "type": "image_url",
    },
    "response_format": "url",
}

response = requests.post(
    "https://api.lmuai.com/v1/images/edits",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    },
    json=payload,
    timeout=300,
)
response.raise_for_status()
result = response.json()
print(result["data"][0]["url"])

Data URLs enlarge the request body

Base64 increases the request body by about one third. For large images, compress them first, or upload them to your own HTTPS object storage and pass the URL. Do not use addresses that require cookies, a login session, or temporary hotlink protection.

7. Response format

A typical Grok image response:

{
  "data": [
    {
      "url": "https://image-host.example/generated.jpg"
    }
  ],
  "usage": {
    "cost_in_usd_ticks": 200000000
  }
}

Clients should:

  1. Check the HTTP status code;
  2. Check whether data is a non-empty array;
  3. Check whether data[0].url is non-empty;
  4. Download the image immediately and save it to your own storage;
  5. Do not treat the temporary URL as a permanent resource address.

The usage field is returned by the upstream channel, and its structure may differ from the GPT image API. The final cost follows your LMU AI bill and Usage details; do not treat any single upstream field as the amount charged to your account.

8. Common errors

HTTPCommon causeRecommended action
400Missing model / prompt, or an invalid image Data URLCheck the JSON and image encoding
401Invalid API keyCheck the Bearer authentication
403Image generation not enabled for the groupContact the admin to check the Grok group permissions
404A channel-incompatible model alias was used, or the upstream path is unavailableFor editing, switch to grok-imagine-image-quality first and save the request ID
429Concurrency, RPM, or upstream quota limitsLower concurrency and retry with exponential backoff
5xxUpstream generation temporarily failedRetry a limited number of times and provide the request ID to the admin

9. Differences from other image APIs

NeedRecommended doc
Gemini native text-to-image and image-to-imageGemini Image API
GPT text-to-image and editingGPT Image API
Grok text-to-image and editingThis page
Async processing of multiple Gemini promptsGemini Batch Image API

Last updated:

On this page