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 openai

Basic 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.content

Node.js

Installation

npm install openai

Basic 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

FeatureSupported
Chat Completions✓
Streaming✓
Function Calling✓
Structured Output✓
Embeddings✓
Batch API✓
Responses API✓

Migration Checklist

Switching from OpenAI to LW AI:

  1. Change base_url / baseURL to https://api.atrouter.cn/v1
  2. Change api_key to your LW AI API key
  3. Change model to a LW AI model ID (e.g., deepseek/deepseek-v4-pro)
  4. Everything else stays the same