OpenRouter Image Gen API Bearer Auth
A thin Hono service over OpenRouter. /generate wraps image generation on google/gemini-3.1-flash-image-preview; /v1/chat/completions is a transparent proxy to OpenRouter's chat completions API (any model).
Authentication
Every request to /generate or /v1/chat/completions must include an Authorization header:
Authorization: Bearer <API_TOKEN>
The token is configured server-side via the API_TOKEN environment variable. Requests without a valid token receive 401 Unauthorized.
POST /generate
Generate one or more images from a text prompt.
Request body (JSON)
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
prompt | string | yes | — | Description of the image to generate. |
aspect_ratio | enum | no | upstream default (1:1) | One of: 1:1 2:3 3:2 3:4 4:3 4:5 5:4 9:16 16:9 21:9 1:4 4:1 1:8 8:1 |
image_size | enum | no | upstream default (1K) | One of: 0.5K 1K 2K 4K |
format | enum | no | json | json returns base64 data URLs; image returns raw image bytes. |
Example — JSON response
curl -X POST https://your-host/generate \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"prompt": "a serene mountain lake at sunrise, photorealistic",
"aspect_ratio": "16:9",
"image_size": "2K"
}'
Response:
{
"model": "google/gemini-3.1-flash-image-preview",
"text": "Here is your image.",
"images": ["data:image/png;base64,iVBORw0KGgo..."]
}
Example — raw image bytes
Set format: "image" to get the raw image (handy for <img src> proxies or saving to disk):
curl -X POST https://your-host/generate \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{"prompt":"a red panda astronaut","format":"image"}' \
--output panda.png
POST /v1/chat/completions
Transparent proxy to OpenRouter's chat completions API. The request body is forwarded as-is (including model, stream, tools, images, etc.), and the response (including SSE streams) is piped back unchanged. Use the same Authorization: Bearer <API_TOKEN> header as /generate; the upstream OPENROUTER_API_KEY is injected server-side.
curl -X POST https://your-host/v1/chat/completions \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o-mini",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Errors
| Status | Meaning |
|---|---|
400 | Validation failed (missing prompt, unsupported enum value, …). |
401 | Missing or invalid bearer token. |
500 | OPENROUTER_API_KEY not configured on the server. |
502 | Upstream (OpenRouter) returned an error or no images. |