Set up your account

  1. API Key: You need to login at https://app.requesty.ai/sign-up
  2. Set up an API-key at https://app.requesty.ai/router
  3. Once you have your API-key you will need to change the base_url of your Openai Client and change it to https://router.requesty.ai/v1

Using our OpenAI SDK

You can use the standard OpenAI Python client by simply changing openai.api_base and using our API key header:

import os
import openai
from dotenv import load_dotenv

# Load API key from environment variables
load_dotenv()
ROUTER_API_KEY = os.getenv("ROUTER_API_KEY")

if ROUTER_API_KEY is None:
    raise ValueError("ROUTER_API_KEY not found. Please check your .env file.")

try:
    # Initialize OpenAI client
    client = openai.OpenAI(
        api_key=ROUTER_API_KEY,
        base_url="https://router.requesty.ai/v1",
        default_headers={"Authorization": f"Bearer {ROUTER_API_KEY}"}
    )

    # Example request
    response = client.chat.completions.create(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello, who are you?"}]
    )

    # Check if the response is successful
    if not response.choices:
        raise Exception("No response choices found.")

    # Print the result
    print(response.choices[0].message.content)

except openai.OpenAIError as e:
    print(f"OpenAI API error: {e}")

except Exception as e:
    print(f"An unexpected error occurred: {e}")

This makes a request to the local router which proxies it to the appropriate model provider, returning a completion response in OpenAI format. Additionally you can add additional information to the headers you can specify (like HTTP-Referer and X-Title) which can help with analytics and app discoverability.