Image Inputs

How to send images to OpenRouter models

Requests with images, to multimodel models, are available via the /api/v1/chat/completions API with a multi-part messages parameter. The image_url can either be a URL or a base64-encoded image. Note that multiple images can be sent in separate content array entries. The number of images you can send in a single request varies per provider and per model. Due to how the content is parsed, we recommend sending the text prompt first, then the images. If the images must come first, we recommend putting it in the system prompt.

OpenRouter supports both direct URLs and base64-encoded data for images:

  • URLs: More efficient for publicly accessible images as they don’t require local encoding
  • Base64: Required for local files or private images that aren’t publicly accessible

Using Image URLs

Here’s how to send an image using a URL:

1import requests
2import json
3
4url = "https://openrouter.ai/api/v1/chat/completions"
5headers = {
6 "Authorization": f"Bearer {API_KEY_REF}",
7 "Content-Type": "application/json"
8}
9
10messages = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "text",
16 "text": "What's in this image?"
17 },
18 {
19 "type": "image_url",
20 "image_url": {
21 "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
22 }
23 }
24 ]
25 }
26]
27
28payload = {
29 "model": "{{MODEL}}",
30 "messages": messages
31}
32
33response = requests.post(url, headers=headers, json=payload)
34print(response.json())

Using Base64 Encoded Images

For locally stored images, you can send them using base64 encoding. Here’s how to do it:

1import requests
2import json
3import base64
4from pathlib import Path
5
6def encode_image_to_base64(image_path):
7 with open(image_path, "rb") as image_file:
8 return base64.b64encode(image_file.read()).decode('utf-8')
9
10url = "https://openrouter.ai/api/v1/chat/completions"
11headers = {
12 "Authorization": f"Bearer {API_KEY_REF}",
13 "Content-Type": "application/json"
14}
15
16# Read and encode the image
17image_path = "path/to/your/image.jpg"
18base64_image = encode_image_to_base64(image_path)
19data_url = f"data:image/jpeg;base64,{base64_image}"
20
21messages = [
22 {
23 "role": "user",
24 "content": [
25 {
26 "type": "text",
27 "text": "What's in this image?"
28 },
29 {
30 "type": "image_url",
31 "image_url": {
32 "url": data_url
33 }
34 }
35 ]
36 }
37]
38
39payload = {
40 "model": "{{MODEL}}",
41 "messages": messages
42}
43
44response = requests.post(url, headers=headers, json=payload)
45print(response.json())

Supported image content types are:

  • image/png
  • image/jpeg
  • image/webp
  • image/gif