File and Image Inputs

A guide to sending PDFs and images via Base64 or URLs in API requests.

Overview

FastRouter allows you to pass images and PDFs as part of your messages, following the OpenAI-compatible API format. This unlocks powerful use cases such as document Q&A, image captioning, and multimodal chat—across supported providers and models.


Supported File Types

  • PDFs: Base64-encoded

  • Images: Base64-encoded or URLs (jpg, png, etc.)


Supported Models

  • PDFs: Select models that support "File" as an input modality from the FastRouter model catalog (e.g., models like openai/gpt-4.1-nano for document analysis).

  • Images: Select models that support "Image" as an input modality from the FastRouter model catalog (e.g., multimodal models like x-ai/grok-4 for vision tasks).


Sending PDFs as Input

You can attach PDFs as file inputs in your messages array, either by encoding the file in base64 or by providing a direct web URL.

Example: Sending a PDF using Base64 Encoding

Use this for local files. Example filename: "example.pdf".

import requests
import base64

def encode_pdf_to_base64(pdf_path):
    with open(pdf_path, "rb") as pdf_file:
        return base64.b64encode(pdf_file.read()).decode('utf-8')

url = "https://go.fastrouter.ai/api/v1/chat/completions"
headers = {
    "Authorization": "Bearer API-KEY",
    "Content-Type": "application/json"
}

pdf_path = "/path/to/your/example.pdf"  # Example filename
base64_pdf = encode_pdf_to_base64(pdf_path)
data_url = f"data:application/pdf;base64,{base64_pdf}"

messages = [
        {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What is in this pdf?"
            },
            {
                "type": "file",
                "file": {                 
                    "filename": "example.pdf",
                    "file_data": data_url
                }
            },
        ]
    }
]

payload = {
    "model": "openai/gpt-4.1-nano",
    "messages": messages,
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

Notes

  • The file_data field is a Data URL: data:application/pdf;base64,{base64_pdf}.

  • You can ask a question about the document in the "text" part.

  • FastRouter will handle the document upload transparently.


Sending Image Inputs

You may send images as part of your chat for multimodal models. Images are passed as entries of type "image_url", either as web URLs or as base64-encoded data URLs.

Example: Sending an Image via URL

Example image URL used.

Python Code Snippet:

import requests

url = "https://go.fastrouter.ai/api/v1/chat/completions"
headers = {
    "Authorization": "Bearer API-KEY",
    "Content-Type": "application/json"
}

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Where is this place located?"
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://upload.wikimedia.org/wikipedia/commons/d/da/Taj-Mahal.jpg"
                }
            }
        ]
    }
]

payload = {
    "model": "openai/gpt-4.1-nano",
    "messages": messages,
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

Example: Sending a Base64-Encoded Image

Use this for local images. Example filename: "image.jpg".

Python Code Snippet:

import requests
import base64

def encode_image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

url = "https://go.fastrouter.ai/api/v1/chat/completions"
headers = {
    "Authorization": "Bearer API-KEY",
    "Content-Type": "application/json"
}

image_path = "/path/to/your/image.jpg"  # Example filename
base64_image = encode_image_to_base64(image_path)
data_url = f"data:image/jpeg;base64,{base64_image}"

messages = [
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {"type": "image_url", "image_url": {"url": data_url}}
        ]
    }
]

payload = {
    "model": "openai/gpt-4.1-nano",
    "messages": messages
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

Tips & Best Practices

  • Order Content: Always send your text prompt first, followed by file/image content, for best results.

  • Multiple Files/Images: You can include more than one file/image by adding additional entries to the content array.

  • Provider/Model Support: The number and type of file/image inputs may vary by provider/model. See the FastRouter model documentation for specifics.

  • Large Image Files: For efficiency, always use direct URLs for large images when possible.

Last updated