OpenRouter API client library
Project description
OpenRouter Client
A Python client library for the OpenRouter API using FastAPI.
Currently supported APIs:
- Chat Completion -
/chat/completions(streaming / non-streaming)
Most parameters that I could figure out from the cryptic API docs are defined and usable. I have not tested every parameter.
More API support coming soon!
Requirements
- Python 3.8 or higher
Installation
From PyPI
To install into your project directly from PyPI via pip:
pip install openrouter-client
If you're using uv:
uv add openrouter-client
Build from source
To build the project from source, clone the repo and then run (Requires uv to be installed - instructions here):
uv sync
Configuration
Requires the following environment variable to be set:
export OPENROUTER_API_KEY="<your_api_key>"
Optional env variables (with their current defaults):
export OPENROUTER_API_URL="https://openrouter.ai/api/v1"
export OPENROUTER_API_PREFIX="/ai/openrouter"
export OPENROUTER_INTEGRATION_TESTS=False
export OPENROUTER_API_TEST_MODEL=meta-llama/llama-3.2-1b-instruct:free
export OPENROUTER_HEADER_TITLE=OpenRouter-PyClient
export OPENROUTER_HEADER_REFERER="https://github.com/mrcodepanda/openrouter-client"
Preferably, put these in an .env or .env.dev file in project root.
The last two are added to the request headers automatically. These values are for app rankings on OpenRouter site. Free to change to your app and site, if you like.
Usage
For using the api, here are various ways to setup (almost all parameters in OpenRouter REST API are available)
Option 1: Use as a standalone FastAPI app
from openrouter import create_app
app = create_app()
# Run with uvicorn (assuming main.py is the name of the file)
# uvicorn main:app --reload
Option 2: Add to an existing FastAPI app
from fastapi import FastAPI
from openrouter import api_router
app = FastAPI()
# You can add dependencies and other options available for FastAPI Routers as well. For eg.
api_router.dependencies = [Depends(some_dependency)]
app.include_router(api_router)
# Now the OpenRouter endpoints are available at /ai/openrouter/*
# Optionally, you can customize the API prefix with OPENROUTER_API_PREFIX env variable
Option 3: Use as a client library
from openrouter.client import create_chat_completion
from openrouter.models.request import ChatCompletionRequest
# Example usage
request = ChatCompletionRequest(
messages=[{"role": "user", "content": "Hello, how are you?"}],
model="<model_name>"
)
response = await create_chat_completion(request)
Testing
Read integration testing section before running tests
To run the tests, install test dependencies:
# Using requirements file
pip install -r requirements-dev.txt
pytest
# Or using uv
uv sync --extra dev
pytest
Integration Testing
The package includes both unit tests (with mocks) and integration tests (with real API calls).
To run integration tests:
- Set your OpenRouter API key in the environment or .env file
- Enable integration tests with an environment variable:
# Enable integration tests
export OPENROUTER_INTEGRATION_TESTS=True
- [OPTIONAL] Set the OpenRouter API test model with an environment variable:
# Set the OpenRouter API test model
export OPENROUTER_API_TEST_MODEL="meta-llama/llama-3.2-1b-instruct:free"
- Run tests
# Run all tests including integration tests
pytest
# Run only mock tests (no calls directly to OpenRouter API)
pytest tests/test_openrouter.py
# Run only integration tests
# Strict max tokens set to 5 to be safe
pytest tests/test_integration.py
Integration tests make actual API calls to OpenRouter API. By default, the api uses meta-llama/llama-3.2-1b-instruct:free for faster tests with max 30 tokens for output. This model can be changed by setting the OPENROUTER_API_TEST_MODEL environment variable. Current free API call limits can be checked here.
Issues & Improvements
If you find an issue, submit a PR or open an issue in the issues tab here and I will get back to you as soon as I can.
Contribute
If you'd like to contribute, simply fork the repository, commit your changes to master branch (or branch off of it), and send a pull request. Make sure you add yourself to AUTHORS in the pull request.
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Copyright (C) 2025 Sudhanshu Aggarwal (@mrcodepanda)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openrouter_client-0.1.0.tar.gz.
File metadata
- Download URL: openrouter_client-0.1.0.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f024abba038bb3a307d4e4e82794b9572166b017544e21e7d3d60332ef2e493b
|
|
| MD5 |
dd3e12be21ec78ffa8faa0451f91d10b
|
|
| BLAKE2b-256 |
db3fcf4b7ffcd09fa49ae2d4dfc6a793710b05ce53e81b3f735ca7a55bb92797
|
File details
Details for the file openrouter_client-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openrouter_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5219fc2944defd4e771ae19afd9bbb3b3d1ddd247e3bf886b5146c9c9ff4bae
|
|
| MD5 |
21bfcf7c22b9fff9e3acd0bb948a0108
|
|
| BLAKE2b-256 |
048ce92cdfb33f0cb7291ef23ffdcc4d2683becdc7aa1acc5efdd8a9b44b49a6
|