> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs-staging.joytokens.ai/llms.txt.

# 快速开始

> 用一条请求接入 JoyToken

选择你的应用使用的协议。只需要配置该协议对应的 Base URL。

## 准备信息

| 项目      | 值                          |
| ------- | -------------------------- |
| API Key | `JOY_TOKEN_API_KEY=sk-xxx` |
| 模型      | `auto`（不可配置）               |

## 选择协议接口

#### OpenAI

Base URL：`https://api-staging.joytokens.ai/openai/v1`

```bash
export JOY_TOKEN_API_KEY="sk-xxx"
export JOY_TOKEN_OPENAI_BASE_URL="https://api-staging.joytokens.ai/openai/v1"
```

### 使用 cURL 调用 Responses API

```bash
curl -sS -X POST "$JOY_TOKEN_OPENAI_BASE_URL/responses" \
  -H "Authorization: Bearer $JOY_TOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: quickstart-responses-001" \
  -d '{
    "model": "auto",
    "input": "只回复：pong",
    "max_output_tokens": 1024,
    "stream": false
  }'
```

### 使用 OpenAI SDK 调用 Responses API

```ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.JOY_TOKEN_API_KEY,
  baseURL: process.env.JOY_TOKEN_OPENAI_BASE_URL,
});

const response = await client.responses.create({
  model: "auto",
  input: "只回复：pong",
  max_output_tokens: 1024,
});

console.log(response.output_text);
```

### 使用 cURL 调用 Chat Completions

```bash
curl -sS -X POST "$JOY_TOKEN_OPENAI_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $JOY_TOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: quickstart-001" \
  -d '{
    "model": "auto",
    "messages": [
      { "role": "user", "content": "只回复：pong" }
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": false
  }'
```

### 使用 OpenAI SDK 调用 Chat Completions

```ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.JOY_TOKEN_API_KEY,
  baseURL: process.env.JOY_TOKEN_OPENAI_BASE_URL,
});

const response = await client.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", content: "只回复：pong" }],
  max_tokens: 1024,
});

console.log(response.choices[0]?.message?.content);
```

#### Anthropic

Base URL：`https://api-staging.joytokens.ai/anthropic/v1`

```bash
export JOY_TOKEN_API_KEY="sk-xxx"
export JOY_TOKEN_ANTHROPIC_BASE_URL="https://api-staging.joytokens.ai/anthropic/v1"
```

### cURL

```bash
curl -sS -X POST "$JOY_TOKEN_ANTHROPIC_BASE_URL/messages" \
  -H "x-api-key: $JOY_TOKEN_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "auto",
    "messages": [
      { "role": "user", "content": "只回复：pong" }
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": false
  }'
```

### Anthropic SDK

官方 SDK 会自动追加 `/v1/messages`，传入 JoyToken Base URL 时需去掉末尾的 `/v1`。

```ts
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.JOY_TOKEN_API_KEY,
  baseURL: process.env.JOY_TOKEN_ANTHROPIC_BASE_URL?.replace(/\/v1$/, ""),
});

const message = await client.messages.create({
  model: "auto",
  max_tokens: 1024,
  messages: [{ role: "user", content: "只回复：pong" }],
});

console.log(message.content);
```

## 检查接入结果

| 位置               | 应看到                                   |
| ---------------- | ------------------------------------- |
| 程序输出             | 响应包含 `pong`                           |
| 响应 metadata      | `metadata.model` 和 `metadata.billing` |
| JoyToken Console | Logs 和 Usage 归因到当前 API Key            |

API Key 应保存在服务端环境变量、密钥管理服务或 CI/CD Secrets 中，不要放进浏览器、移动端、公开仓库或日志。