> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arouter.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速開始

> 2 分鐘內開始使用 ARouter。無需修改程式碼——只需替換 base_url 和 api_key。

ARouter 為主流 LLM 提供商提供統一的 API key 和單一端點。如果你已經在使用 OpenAI 相容的客戶端，遷移通常只需更改 `base_url`、`api_key`，以及可選的應用歸因請求標頭。

## 1. 取得 API Key

在 [ARouter 控制台](https://api.arouter.ai) 註冊並建立 API key。
你的 key 格式類似 `lr_live_xxxxxxxxxxxx`。

## 2. 安裝 ARouter SDK

官方 `@arouter/sdk` 適用於任何 Node.js 或 TypeScript 專案，支援 npm、yarn 和 pnpm。

<Tabs>
  <Tab title="npm">
    ```bash theme={null}
    npm install @arouter/sdk
    ```
  </Tab>

  <Tab title="yarn">
    ```bash theme={null}
    yarn add @arouter/sdk
    ```
  </Tab>

  <Tab title="pnpm">
    ```bash theme={null}
    pnpm add @arouter/sdk
    ```
  </Tab>
</Tabs>

```typescript theme={null}
import { ARouter } from "@arouter/sdk";

const client = new ARouter({
  apiKey: "lr_live_xxxx",
  baseURL: "https://api.arouter.ai",
});

const response = await client.chatCompletion({
  model: "openai/gpt-5.4",
  messages: [{ role: "user", content: "你好！" }],
});
console.log(response.choices[0].message.content);
```

查看 [Node.js / TypeScript SDK 指南](/zh-Hant/sdks/node)，了解串流輸出、key 管理和 x402 付款範例。

## 3. 使用現有 SDK

已在使用 OpenAI、Anthropic 或 Go？只需更改 `base_url` 和 `api_key` 即可。

<Tabs>
  <Tab title="Python (OpenAI)">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.arouter.ai/v1",
        api_key="lr_live_xxxx",
        default_headers={
            "HTTP-Referer": "https://myapp.com",  # 可選
            "X-Title": "My AI App",               # 可選
        },
    )

    response = client.chat.completions.create(
        model="openai/gpt-5.4",
        messages=[{"role": "user", "content": "你好！"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="Node.js (OpenAI)">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: "https://api.arouter.ai/v1",
      apiKey: "lr_live_xxxx",
      defaultHeaders: {
        "HTTP-Referer": "https://myapp.com", // 可選
        "X-Title": "My AI App",              // 可選
      },
    });

    const response = await client.chat.completions.create({
      model: "openai/gpt-5.4",
      messages: [{ role: "user", content: "你好！" }],
    });
    console.log(response.choices[0].message.content);
    ```
  </Tab>

  <Tab title="Python (Anthropic)">
    ```python theme={null}
    import anthropic

    client = anthropic.Anthropic(
        base_url="https://api.arouter.ai",
        api_key="lr_live_xxxx",
    )

    message = client.messages.create(
        model="claude-sonnet-4.6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "你好！"}],
    )
    print(message.content[0].text)
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    package main

    import (
        "context"
        "fmt"
        "log"

        "github.com/arouter-ai/arouter-go"
    )

    func main() {
        client := arouter.NewClient("lr_live_xxxx",
            arouter.WithBaseURL("https://api.arouter.ai/v1"),
            arouter.WithHeader("HTTP-Referer", "https://myapp.com"),
            arouter.WithHeader("X-Title", "My AI App"),
        )

        resp, err := client.CreateChatCompletion(context.Background(), arouter.ChatCompletionRequest{
            Model: "openai/gpt-5.4",
            Messages: []arouter.Message{
                {Role: "user", Content: "你好！"},
            },
        })
        if err != nil {
            log.Fatal(err)
        }

        fmt.Println(resp.Choices[0].Message.Content)
    }
    ```
  </Tab>

  <Tab title="cURL">
    ```bash theme={null}
    curl https://api.arouter.ai/v1/chat/completions \
      -H "Authorization: Bearer lr_live_xxxx" \
      -H "HTTP-Referer: https://myapp.com" \
      -H "X-Title: My AI App" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "openai/gpt-5.4",
        "messages": [{"role": "user", "content": "你好！"}]
      }'
    ```
  </Tab>

  <Tab title="fetch">
    ```typescript theme={null}
    const response = await fetch('https://api.arouter.ai/v1/chat/completions', {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer lr_live_xxxx',
        'Content-Type': 'application/json',
        'HTTP-Referer': 'https://myapp.com', // 可選：來源追蹤
        'X-Title': 'My AI App',              // 可選：顯示名稱
      },
      body: JSON.stringify({
        model: 'openai/gpt-5.4',
        messages: [{ role: 'user', content: '你好！' }],
      }),
    });

    const data = await response.json();
    console.log(data.choices[0].message.content);
    ```
  </Tab>
</Tabs>

<Note>
  `HTTP-Referer` 和 `X-Title` 為可選項。如果你希望 ARouter 控制台分析將請求歸因到特定應用或工作流程，請包含這兩個請求標頭。
</Note>

## 4. 直接呼叫 API

如果不想安裝 SDK，可以使用任意 HTTP 客戶端呼叫 ARouter：

```python theme={null}
import json
import requests

response = requests.post(
    "https://api.arouter.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer lr_live_xxxx",
        "HTTP-Referer": "https://myapp.com",  # 可選
        "X-Title": "My AI App",               # 可選
        "Content-Type": "application/json",
    },
    data=json.dumps({
        "model": "openai/gpt-5.4",
        "messages": [{"role": "user", "content": "你好！"}],
    }),
)

print(response.json()["choices"][0]["message"]["content"])
```

## 5. 切換不同提供商

使用 ARouter，切換提供商只需更改 `model` 字串：

```python theme={null}
# OpenAI
response = client.chat.completions.create(model="openai/gpt-5.4", ...)

# Anthropic（透過 OpenAI SDK！）
response = client.chat.completions.create(model="anthropic/claude-sonnet-4.6", ...)

# Google Gemini
response = client.chat.completions.create(model="google/gemini-2.5-flash", ...)

# DeepSeek
response = client.chat.completions.create(model="deepseek/deepseek-v3.2", ...)
```

<Tip>
  如果省略提供商前綴（例如只寫 `"gpt-5.4"`），ARouter 預設使用 OpenAI。
</Tip>

## 下一步

<CardGroup cols={2}>
  <Card title="身份驗證" icon="lock" href="/zh-Hant/authentication">
    了解三種驗證方式
  </Card>

  <Card title="模型路由" icon="shuffle" href="/zh-Hant/model-routing">
    理解 provider/model 格式與多模型路由
  </Card>

  <Card title="帳單與積分" icon="credit-card" href="/zh-Hant/guides/billing-and-credits">
    查看定價、餘額和積分規則
  </Card>

  <Card title="請求歸因" icon="globe" href="/zh-Hant/guides/request-attribution">
    在控制台分析中將流量歸因到你的應用
  </Card>

  <Card title="串流輸出" icon="wave-sine" href="/zh-Hant/guides/streaming">
    啟用即時串流回應
  </Card>

  <Card title="工具呼叫" icon="wrench" href="/zh-Hant/guides/features/tool-calling">
    讓模型存取你的函式
  </Card>

  <Card title="結構化輸出" icon="brackets-curly" href="/zh-Hant/guides/features/structured-outputs">
    強制模型回傳有效的 JSON schema
  </Card>

  <Card title="提示詞快取" icon="bolt" href="/zh-Hant/guides/features/prompt-caching">
    降低重複提示的成本和延遲
  </Card>

  <Card title="Key 管理" icon="key" href="/zh-Hant/guides/key-management">
    為團隊建立受限 key
  </Card>

  <Card title="常見問題" icon="circle-question" href="/zh-Hant/faq">
    常見問題解答
  </Card>
</CardGroup>
