Chat Completions
Generate text with any text model available on ImageRouter. This endpoint is OpenAI Chat Completions compatible — point the OpenAI SDK at ImageRouter by changing the base URL and you are done. You are billed on the true token cost of each request.
curl 'https://api.imagerouter.io/v1/openai/chat/completions' \-H 'Authorization: Bearer YOUR_API_KEY' \--json '{ "model": "openai/gpt-4o-mini", "messages": [ { "role": "user", "content": "Write a haiku about the ocean." } ]}'import OpenAI from 'openai'
const client = new OpenAI({apiKey: 'YOUR_API_KEY',baseURL: 'https://api.imagerouter.io/v1/openai',})
const completion = await client.chat.completions.create({model: 'openai/gpt-4o-mini',messages: [{ role: 'user', content: 'Write a haiku about the ocean.' }],})
console.log(completion.choices[0].message.content)from openai import OpenAI
client = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.imagerouter.io/v1/openai",)
completion = client.chat.completions.create( model="openai/gpt-4o-mini", messages=[{"role": "user", "content": "Write a haiku about the ocean."}],)
print(completion.choices[0].message.content)Streaming
Section titled “Streaming”Set stream: true to receive a Server-Sent Events stream. The stream is proxied unchanged, and the final chunk carries token usage and the true cost.
curl 'https://api.imagerouter.io/v1/openai/chat/completions' \-H 'Authorization: Bearer YOUR_API_KEY' \--json '{ "model": "openai/gpt-4o-mini", "messages": [{ "role": "user", "content": "Write a haiku about the ocean." }], "stream": true}'import OpenAI from 'openai'
const client = new OpenAI({apiKey: 'YOUR_API_KEY',baseURL: 'https://api.imagerouter.io/v1/openai',})
const stream = await client.chat.completions.create({model: 'openai/gpt-4o-mini',messages: [{ role: 'user', content: 'Write a haiku about the ocean.' }],stream: true,})
for await (const chunk of stream) {process.stdout.write(chunk.choices[0]?.delta?.content || '')}from openai import OpenAI
client = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.imagerouter.io/v1/openai",)
stream = client.chat.completions.create( model="openai/gpt-4o-mini", messages=[{"role": "user", "content": "Write a haiku about the ocean."}], stream=True,)
for chunk in stream: print(chunk.choices[0].delta.content or "", end="")Vision (image input)
Section titled “Vision (image input)”Models with vision capability can accept images alongside text. Add an image_url part to a user message’s content array — the URL may be a public link or a base64 data: URI. Sending an image to a model that does not support vision returns a 400 error.
curl 'https://api.imagerouter.io/v1/openai/chat/completions' \-H 'Authorization: Bearer YOUR_API_KEY' \--json '{ "model": "openai/gpt-4o-mini", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What is in this image?" }, { "type": "image_url", "image_url": { "url": "https://example.com/photo.jpg" } } ] } ]}'import OpenAI from 'openai'
const client = new OpenAI({apiKey: 'YOUR_API_KEY',baseURL: 'https://api.imagerouter.io/v1/openai',})
const completion = await client.chat.completions.create({model: 'openai/gpt-4o-mini',messages: [ { role: 'user', content: [ { type: 'text', text: 'What is in this image?' }, { type: 'image_url', image_url: { url: 'https://example.com/photo.jpg' }, }, ], },],})
console.log(completion.choices[0].message.content)from openai import OpenAI
client = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.imagerouter.io/v1/openai",)
completion = client.chat.completions.create( model="openai/gpt-4o-mini", messages=[ { "role": "user", "content": [ {"type": "text", "text": "What is in this image?"}, { "type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}, }, ], } ],)
print(completion.choices[0].message.content)To send a local image, base64-encode it and pass it as a data URI, e.g. "url": "data:image/jpeg;base64,<BASE64_BYTES>".
Parameters
Section titled “Parameters”-
modelrequired Text model to use for the completion. -
messagesrequired List of messages in the conversation, in OpenAI Chat Completions format. Each message has arole(system,user,assistant, ortool) andcontent. -
streamoptional Whentrue, partial deltas are streamed as Server-Sent Events. Defaults tofalse. -
temperature,max_tokens,top_p,tools,tool_choice,response_format,reasoning_effort, … optional All standard OpenAI Chat Completions parameters are passed through to the model unchanged.
Response
Section titled “Response”{ "id": "chatcmpl-...", "object": "chat.completion", "created": 1234567890, "model": "openai/gpt-4o-mini", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 12, "completion_tokens": 24, "total_tokens": 36, "cost": 0.0000123 }, "cost": 0.0001}usage.cost is the true upstream cost in USD. The top-level cost field is what was charged to your ImageRouter credits (the true cost, rounded up to the nearest 1/10,000 USD).