Guides
SDK Integration
Use UniterRouter with popular AI SDKs and libraries.
SDK Integration
UniterRouter is fully compatible with OpenAI's API format, allowing you to use existing SDKs with minimal configuration changes.
OpenAI SDK
Python
pip install openaifrom openai import OpenAI
client = OpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key="your_uniterouter_api_key",
)
# Chat Completions
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
# Streaming
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
# Embeddings
embeddings = client.embeddings.create(
model="text-embedding-3-small",
input="Hello world"
)
# Images
image = client.images.generate(
model="dall-e-3",
prompt="A sunset over mountains"
)
# Audio Transcription
with open("audio.mp3", "rb") as f:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=f
)TypeScript / Node.js
npm install openaiimport OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.uniterouter.ai/v1",
apiKey: process.env.UNITEROUTER_API_KEY,
});
// Chat Completions
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
// Streaming
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Tell me a story" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}Anthropic SDK
Use the Anthropic SDK with UniterRouter's Messages endpoint:
Python
pip install anthropicfrom anthropic import Anthropic
client = Anthropic(
base_url="https://api.uniterouter.ai",
api_key="your_uniterouter_api_key",
)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude!"}]
)
print(response.content[0].text)TypeScript
npm install @anthropic-ai/sdkimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.uniterouter.ai",
apiKey: process.env.UNITEROUTER_API_KEY,
});
const response = await client.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, Claude!" }],
});Vercel AI SDK
The Vercel AI SDK provides React hooks and streaming utilities.
npm install ai @ai-sdk/openaiProvider Setup
// lib/ai.ts
import { createOpenAI } from "@ai-sdk/openai";
export const uniterouter = createOpenAI({
baseURL: "https://api.uniterouter.ai/v1",
apiKey: process.env.UNITEROUTER_API_KEY!,
});API Route (Next.js)
// app/api/chat/route.ts
import { uniterouter } from "@/lib/ai";
import { streamText } from "ai";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: uniterouter("gpt-4o"),
messages,
});
return result.toDataStreamResponse();
}React Component
"use client";
import { useChat } from "ai/react";
export function Chat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
return (
<div className="flex flex-col h-screen">
<div className="flex-1 overflow-auto p-4">
{messages.map((m) => (
<div key={m.id} className="mb-4">
<strong>{m.role === "user" ? "You" : "AI"}:</strong>
<p>{m.content}</p>
</div>
))}
</div>
<form onSubmit={handleSubmit} className="p-4 border-t">
<input
value={input}
onChange={handleInputChange}
placeholder="Type a message..."
className="w-full p-2 border rounded"
disabled={isLoading}
/>
</form>
</div>
);
}LangChain
Python
pip install langchain-openaifrom langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key="your_uniterouter_api_key",
model="gpt-4o",
)
response = llm.invoke("What is the capital of France?")
print(response.content)TypeScript
npm install @langchain/openaiimport { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
configuration: {
baseURL: "https://api.uniterouter.ai/v1",
},
openAIApiKey: process.env.UNITEROUTER_API_KEY,
modelName: "gpt-4o",
});
const response = await llm.invoke("What is the capital of France?");
console.log(response.content);LlamaIndex
pip install llama-index-llms-openaifrom llama_index.llms.openai import OpenAI
llm = OpenAI(
api_base="https://api.uniterouter.ai/v1",
api_key="your_uniterouter_api_key",
model="gpt-4o",
)
response = llm.complete("What is machine learning?")
print(response.text)Go
go get github.com/sashabaranov/go-openaipackage main
import (
"context"
"fmt"
openai "github.com/sashabaranov/go-openai"
)
func main() {
config := openai.DefaultConfig("your_uniterouter_api_key")
config.BaseURL = "https://api.uniterouter.ai/v1"
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "gpt-4o",
Messages: []openai.ChatCompletionMessage{
{Role: "user", Content: "Hello!"},
},
},
)
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}Environment Configuration
Best Practice: Store your API key in environment variables, never in code.
# .env
UNITEROUTER_API_KEY=sk_your_api_key_hereimport os
from openai import OpenAI
client = OpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key=os.environ["UNITEROUTER_API_KEY"],
)Switching Models
With UniterRouter, you can switch between providers by just changing the model name:
# OpenAI
response = client.chat.completions.create(model="gpt-4o", messages=messages)
# Anthropic (via OpenAI format)
response = client.chat.completions.create(model="claude-3-5-sonnet-20241022", messages=messages)
# Google
response = client.chat.completions.create(model="gemini-2.0-flash", messages=messages)
# DeepSeek
response = client.chat.completions.create(model="deepseek-chat", messages=messages)Next Steps
- View all Available Models
- Learn about Streaming
- Explore Function Calling
How is this guide?