The official Python SDK for HyperRouter — a unified API gateway for all major AI models.
Project description
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
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 hyperrouter_ai-0.2.0.tar.gz.
File metadata
- Download URL: hyperrouter_ai-0.2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd558e867c8c9d9782250434a2aeb9a7cfc3e9dfb812aa04c8dff6e733ad172b
|
|
| MD5 |
648f227afbf3181f9e4ca009ffb78326
|
|
| BLAKE2b-256 |
ba4d58b1518a48196b2c870e3f9fd3505eff9696602c1ac4e30d5655fc848dd4
|
File details
Details for the file hyperrouter_ai-0.2.0-py3-none-any.whl.
File metadata
- Download URL: hyperrouter_ai-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fe866da3604f3fe0c37720d80f65333ccb592f60739d77bb5c1b3d3bc015db1
|
|
| MD5 |
2c0d748b850540280259582e7183d4cc
|
|
| BLAKE2b-256 |
562e97f09002ffab7b50c871c1e568c9bd00e3301026cad4eb14cac617c56da3
|