HyperRouter
The official Python SDK for HyperRouter — a unified API gateway for all major AI models. One API key, one SDK, any model.
To learn more about how to use the HyperRouter SDK, check out our API Reference and Documentation.
SDK Installation
Note: The SDK requires a valid API key.
The SDK can be installed with pip, uv, or other package managers.
pip
pip install hyperrouter-ai
uv
uv is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools.
uv add hyperrouter-ai
Poetry
poetry add hyperrouter-ai
Shell and script usage
You can use this SDK in a Python shell with uv and the --with command-line option:
# Launch a Python shell
uv run --with hyperrouter-ai python
# Run a script directly
uv run --with hyperrouter-ai python my_script.py
Requirements
This SDK requires Python 3.8 or higher.
IDE Support
PyCharm
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration by installing an additional plugin.
SDK Usage
Synchronous
from hyperrouter import HyperRouter
client = HyperRouter(api_key="hr-your-key-here")
# Or set HYPERROUTER_API_KEY env var and call HyperRouter()
response = client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is HyperRouter?"},
],
)
print(response.choices[0].message.content)
Async
The SDK client can also be used to make asynchronous requests with asyncio:
from hyperrouter import AsyncHyperRouter
import asyncio
client = AsyncHyperRouter() # Uses HYPERROUTER_API_KEY env var
async def main():
response = await client.chat.completions.create(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
Streaming
Stream responses token-by-token for real-time output:
from hyperrouter import HyperRouter
client = HyperRouter()
stream = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Write a haiku about AI."}],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()
Async Streaming
from hyperrouter import AsyncHyperRouter
import asyncio
client = AsyncHyperRouter()
async def main():
stream = await client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Explain quantum computing."}],
stream=True,
)
async for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()
asyncio.run(main())
Using Different Models
HyperRouter gives you access to 300+ models from 50+ providers through a single API. Just change the model parameter:
from hyperrouter import HyperRouter
client = HyperRouter()
# Anthropic
response = client.chat.completions.create(
model="anthropic/claude-opus-4.6",
messages=[{"role": "user", "content": "Hello from Claude!"}],
)
# OpenAI
response = client.chat.completions.create(
model="openai/gpt-4.1",
messages=[{"role": "user", "content": "Hello from GPT!"}],
)
# DeepSeek
response = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Hello from DeepSeek!"}],
)
# Google
response = client.chat.completions.create(
model="google/gemma-4-31b-it",
messages=[{"role": "user", "content": "Hello from Gemma!"}],
)
# Meta
response = client.chat.completions.create(
model="meta-llama/llama-4-scout",
messages=[{"role": "user", "content": "Hello from Llama!"}],
)
# Auto Router — let HyperRouter pick the best model
response = client.chat.completions.create(
model="hyperrouter/auto",
messages=[{"role": "user", "content": "Pick the best model for me!"}],
)
Browse all available models at hyperrouter.ai/models.
OpenAI Compatibility
HyperRouter is fully compatible with the OpenAI SDK. If you already use openai, you can switch by changing the base URL:
from openai import OpenAI
client = OpenAI(
api_key="hr-your-key-here",
base_url="https://api.hyperrouter.ai/v1",
)
response = client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Hello!"}],
)
Resource Management
The SDK implements the standard context manager protocol, so you can use it with with statements to ensure connections are properly closed:
from hyperrouter import HyperRouter
with HyperRouter() as client:
response = client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
# Connection is automatically closed
For async:
from hyperrouter import AsyncHyperRouter
import asyncio
async def main():
async with AsyncHyperRouter() as client:
response = await client.chat.completions.create(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
Environment Variables
| Variable | Description |
|---|---|
HYPERROUTER_API_KEY |
Your HyperRouter API key (hr-...). Used when no api_key is passed to the client. |
Debugging
You can turn on debug logging to see raw HTTP requests and responses:
from hyperrouter import HyperRouter
import logging
logging.basicConfig(level=logging.DEBUG)
client = HyperRouter()
Links
Metadata
Release files for hyperrouter-ai 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hyperrouter_ai-0.2.0.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hyperrouter_ai-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.1 kB
Release files / hyperrouter_ai-0.2.0.tar.gz
| Download URL | hyperrouter_ai-0.2.0.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd558e867c8c9d9782250434a2aeb9a7cfc3e9dfb812aa04c8dff6e733ad172b
|
|
BLAKE2b-256 checksum How to use checksums |
ba4d58b1518a48196b2c870e3f9fd3505eff9696602c1ac4e30d5655fc848dd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / hyperrouter_ai-0.2.0-py3-none-any.whl
| Download URL | hyperrouter_ai-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fe866da3604f3fe0c37720d80f65333ccb592f60739d77bb5c1b3d3bc015db1
|
|
BLAKE2b-256 checksum How to use checksums |
562e97f09002ffab7b50c871c1e568c9bd00e3301026cad4eb14cac617c56da3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|