integrations
OpenAI SDK
Use the official OpenAI SDK with LW AI API by simply changing the base URL.
LW AI API is fully compatible with the OpenAI SDK. Just change the base URL — no code refactoring needed.
Python
Installation
pip install openaiBasic Usage
from openai import OpenAI
client = OpenAI(
base_url="https://api.atrouter.cn/v1",
api_key="YOUR_API_KEY"
)
response = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)Streaming
stream = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
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="", flush=True)Function Calling
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"]
}
}
}]
response = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Weather in Beijing?"}],
tools=tools
)Async
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://api.atrouter.cn/v1",
api_key="YOUR_API_KEY"
)
async def chat():
response = await client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Hello!"}]
)
return response.choices[0].message.contentNode.js
Installation
npm install openaiBasic Usage
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.atrouter.cn/v1',
apiKey: 'YOUR_API_KEY',
});
const response = await client.chat.completions.create({
model: 'deepseek/deepseek-v4-pro',
messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(response.choices[0].message.content);Streaming
const stream = await client.chat.completions.create({
model: 'deepseek/deepseek-v4-pro',
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}Environment Variables
Set your API key via environment variable instead of hardcoding:
export LINKWO_API_KEY="YOUR_API_KEY"import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.atrouter.cn/v1",
api_key=os.environ.get("LINKWO_API_KEY")
)Compatibility
| Feature | Supported |
|---|---|
| Chat Completions | ✓ |
| Streaming | ✓ |
| Function Calling | ✓ |
| Structured Output | ✓ |
| Embeddings | ✓ |
| Batch API | ✓ |
| Responses API | ✓ |
Migration Checklist
Switching from OpenAI to LW AI:
- Change
base_url/baseURLtohttps://api.atrouter.cn/v1 - Change
api_keyto your LW AI API key - Change
modelto a LW AI model ID (e.g.,deepseek/deepseek-v4-pro) - Everything else stays the same
Related
- SDK Integration — Other language SDKs
- Chat Completions — Full API reference
- Authentication — API key setup