getting started

First Request

Make your first API request to LW AI and understand the response format.

After setting up authentication, you're ready to make your first API request. This guide walks you through each part of a request and explains the response.

Basic Request

The most common endpoint is Chat Completions, which generates a response based on a list of messages:

curl https://api.atrouter.cn/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek/deepseek-v4-pro",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is the capital of France?"}
    ],
    "temperature": 0.7
  }'

Understanding the Response

A successful response returns JSON like this:

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1719000000,
  "model": "deepseek/deepseek-v4-pro",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 8,
    "total_tokens": 32
  }
}

Key Fields

FieldDescription
idUnique identifier for the completion
choicesArray of completion choices (usually one)
choices[].messageThe assistant's reply
choices[].finish_reasonWhy the model stopped generating (stop, length, content_filter)
usageToken counts for billing and monitoring

Message Roles

Each message has a role that tells the model how to interpret it:

RoleDescription
systemSets the assistant's behavior and personality
userThe end-user's input or instruction
assistantThe model's previous replies (for multi-turn conversations)

Multi-Turn Conversation

Include previous messages to maintain context:

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": "system", "content": "You are a travel guide."},
        {"role": "user", "content": "What's the best time to visit Japan?"},
        {"role": "assistant", "content": "The best time to visit Japan is during spring (March-May) for cherry blossoms or autumn (October-November) for fall foliage."},
        {"role": "user", "content": "What about budget-friendly options?"}
    ]
)

print(response.choices[0].message.content)

Common Parameters

ParameterTypeDefaultDescription
temperaturefloat1.0Controls randomness (0 = deterministic, 2 = very random)
max_tokensintmodel maxMaximum tokens to generate in the response
top_pfloat1.0Nucleus sampling threshold
streamboolfalseStream partial results as they're generated

Error Handling

If something goes wrong, the API returns an error object:

{
  "error": {
    "message": "Invalid API key provided",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}

Common HTTP status codes:

StatusMeaning
400Bad request — check your parameters
401Unauthorized — invalid or missing API key
429Rate limit exceeded — slow down or upgrade
500Server error — retry after a brief wait

See Error Codes for the complete list.

Next Steps