# OpenRouter APIs — OpenAI: GPT Image 2 (openai/gpt-image-2)

Guide for calling every confirmed OpenRouter API that serves `openai/gpt-image-2`.

Model page: https://openrouter.ai/openai/gpt-image-2
Create an API key: https://openrouter.ai/settings/keys

## Authentication

Send this header with every request:

- Authorization: Bearer $OPENROUTER_API_KEY

## Text / Chat Completions API

Generate text with `openai/gpt-image-2` through OpenRouter's Chat Completions API.

Docs: https://openrouter.ai/docs/api/api-reference/chat/create-a-chat-completion

### Endpoint

POST https://openrouter.ai/api/v1/chat/completions

Headers:
- Content-Type: application/json

### Request fields (openai/gpt-image-2)

- model: string (required) — `"openai/gpt-image-2"`
- messages: array (required) — ordered conversation messages with `role` and `content`
- stream: boolean (optional) — return Server-Sent Events as tokens are generated
- frequency_penalty: optional — accepted by this model; see the API reference for its value shape
- logit_bias: optional — accepted by this model; see the API reference for its value shape
- logprobs: optional — accepted by this model; see the API reference for its value shape
- max_tokens: optional — accepted by this model; see the API reference for its value shape
- presence_penalty: optional — accepted by this model; see the API reference for its value shape
- response_format: optional — accepted by this model; see the API reference for its value shape
- seed: optional — accepted by this model; see the API reference for its value shape
- stop: optional — accepted by this model; see the API reference for its value shape
- temperature: optional — accepted by this model; see the API reference for its value shape
- top_logprobs: optional — accepted by this model; see the API reference for its value shape
- top_p: optional — accepted by this model; see the API reference for its value shape

The model-specific optional fields above come from this model's advertised capabilities.
`stream` and `provider` routing preferences are API controls accepted independently of that
model capability list.

### Response

Without `stream`, the response is a Chat Completions JSON object:

```json
{
  "id": "gen-abc123",
  "choices": [{ "message": { "role": "assistant", "content": "..." } }],
  "usage": { "prompt_tokens": 12, "completion_tokens": 24, "total_tokens": 36, "cost": 0.001 }
}
```

With `stream: true`, the response is `text/event-stream`: read each `data:` JSON chunk until
`data: [DONE]`. The final usage chunk carries token counts and cost when usage is requested.

### Examples

#### Chat completion

```bash
curl -X POST https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "openai/gpt-image-2",
  "messages": [
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}'
```

### Error differences

- 400 — malformed messages or a parameter this model does not support
- 502 — the upstream text generation failed

## Image API

Generate images with `openai/gpt-image-2` through OpenRouter's Image API.

Docs: https://openrouter.ai/docs/guides/overview/multimodal/image-generation
Model discovery API: https://openrouter.ai/api/v1/images/models
Endpoints (openai/gpt-image-2): https://openrouter.ai/api/v1/images/models/openai/gpt-image-2/endpoints

### Endpoint

POST https://openrouter.ai/api/v1/images

Headers:
- Content-Type: application/json

### Request fields (openai/gpt-image-2)

- model: string (required) — `"openai/gpt-image-2"`
- prompt: string (required) — text description of the desired image
- aspect_ratio: "1:1" | "3:2" | "2:3" | "4:3" | "3:4" | "16:9" | "9:16" | "21:9" | "auto" (optional) — aspect ratio of the generated image
- quality: "auto" | "low" | "medium" | "high" (optional) — rendering quality
- background: "auto" | "opaque" (optional) — background treatment; transparent requires png or webp output
- n: integer 1-10 (optional) — upper bound on the number of images to generate; providers may return fewer, and single-image providers reject n > 1
- input_references: array of up to 16 image references (optional) — reference images for image-to-image, as `{ "type": "image_url", "image_url": { "url": "…" } }` entries; the url is an https URL or a base64 data URL
- output_compression: integer 0-100 (optional) — compression level for webp/jpeg output; ignored for png
- stream: boolean (optional) — stream partial images as SSE events

These are the generation parameters this model accepts between its providers; an
unlisted value is rejected, and a listed one can still be refused by whichever provider
serves the call. `provider` (routing preferences) is accepted on every request.

### Response

```json
{
  "created": 1748372400,
  "data": [{ "b64_json": "<base64 image bytes>", "media_type": "image/png" }],
  "usage": { "prompt_tokens": 0, "completion_tokens": 4175, "total_tokens": 4175, "cost": 0.04 }
}
```

Base64-decode `data[i].b64_json` and write the bytes to a file; `media_type` gives the extension.
`usage.cost` is the USD charge for the call.

### Examples

#### Text to Image

```bash
curl -X POST https://openrouter.ai/api/v1/images \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "openai/gpt-image-2",
  "prompt": "Editorial architectural photograph of a contemporary neighborhood storefront at blue hour, glowing warmly against the dusk. Above the entrance, a warm-white neon sign in flowing hand-bent cursive reads exactly \"OpenRouter\", the only text in the scene. Large windows reveal a cozy, lived-in interior: wood shelves styled with books and ceramics, lush trailing plants, and soft pendant lighting. Potted plants and a bicycle rest by the entrance, and golden light spills across the wet pavement in gentle reflections. Straight-on composition, realistic materials, quiet street with no people.",
  "n": 1,
  "quality": "high",
  "aspect_ratio": "16:9"
}'
```

#### Edit Image

```bash
curl -X POST https://openrouter.ai/api/v1/images \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "openai/gpt-image-2",
  "prompt": "Change the neon sign to a bright electric blue neon, and let its cool glow tint the facade and the reflections on the pavement below, while keeping the interior'\''s warm lamp lighting unchanged so the windows still glow cozy and golden. Add gentle falling snow: a light dusting on the awning ledge, the potted plants, the bicycle seat, and the sidewalk, with fine snowflakes visible in the air against the dusk sky. Keep the composition and everything else exactly the same. The sign must still read exactly '\''OpenRouter'\''.",
  "n": 1,
  "quality": "high",
  "aspect_ratio": "16:9",
  "input_references": [
    {
      "type": "image_url",
      "image_url": {
        "url": "https://model-assets.openrouter.ai/model-examples/openai/gpt-image-2/d69724de-dc82-4bd9-b41b-d7a85f9a4fce/input-0.png"
      }
    }
  ]
}'
```

#### Combine Images

```bash
curl -X POST https://openrouter.ai/api/v1/images \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "openai/gpt-image-2",
  "prompt": "A close-up portrait of the dog from photo 2 at dusk, framed from the chest up, sitting on the wet sidewalk directly in front of the cafe from photo 1, right next to the parked bicycle, waiting for its owner and looking slightly past the camera. Camera at the dog'\''s eye level, low to the ground, so the sidewalk is visible beneath it. Behind the dog, softly out of focus: the bicycle at its side and the glowing cafe windows. Warm window light catches its fur and eyes, with a faint cool blue tone from the evening sky. Keep the dog'\''s exact face, fur coloring, and collar.",
  "n": 1,
  "quality": "high",
  "aspect_ratio": "16:9",
  "input_references": [
    {
      "type": "image_url",
      "image_url": {
        "url": "https://model-assets.openrouter.ai/model-examples/openai/gpt-image-2/4f02b87a-ca3c-4da0-9300-312c347ad55c/input-0.png"
      }
    },
    {
      "type": "image_url",
      "image_url": {
        "url": "https://model-assets.openrouter.ai/model-examples/openai/gpt-image-2/4f02b87a-ca3c-4da0-9300-312c347ad55c/input-1.png"
      }
    }
  ]
}'
```

### Error differences

- 413 — request body too large

## Errors

Failures return `{"error": {"code": <number>, "message": <string>}}` with the HTTP status:

- 400 — malformed request or an unsupported parameter
- 401 — missing or invalid API key
- 402 — insufficient credits
- 403 — spend limit reached, key disabled, or access blocked
- 404 — unknown model or no provider can serve the request
- 429 — rate limited; retry with backoff
- 502 — the operation failed upstream; failed generations are not billed

---

Canonical version of this document: https://openrouter.ai/openai/gpt-image-2/llms.txt
