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

# API 参考

> 欢迎查看 MegaLLM API 参考文档。MegaLLM 通过兼容 OpenAI 和 Anthropic 的 REST API 提供对 70 多个 AI 模型的统一访问。

## 基础 URL

MegaLLM 支持两种 API 格式：

> [card] OpenAI 格式
>
> ```
> https://ai.megallm.io/v1
> ```
>
> 兼容 OpenAI SDK 和工具

> [card] Anthropic 格式
>
> ```
> https://ai.megallm.io
> ```
>
> 兼容 Anthropic Claude SDK

## 身份验证

所有 API 端点都需要使用以下方法之一进行身份验证：

### Bearer Token（推荐）

```http theme={null}
Authorization: Bearer YOUR_API_KEY
```

### API Key Header（Anthropic 格式）

```http theme={null}
x-api-key: YOUR_API_KEY
anthropic-version: 2023-06-01
```

> [!NOTE]
>
> 从 [MegaLLM Dashboard](https://megallm.io/dashboard/overview) 获取您的 API 密钥。

## 核心端点

> [card] [Chat Completions](https://docs.megallm.io/cn/api-reference/post-chat-completions)
>
> 兼容 OpenAI 的聊天 API，支持流式传输、函数调用和视觉功能

> [card] [Messages](https://docs.megallm.io/cn/api-reference/post-v1-messages)
>
> 兼容 Anthropic 的 API，支持扩展思考、工具和提示缓存

> [card] [Models](https://docs.megallm.io/cn/api-reference/get-models)
>
> 列出所有 70 多个 AI 模型及其功能、定价和上下文窗口

## 高级功能

> [card] [流式传输](https://docs.megallm.io/cn/api-reference/endpoint/streaming.md)
>
> 使用服务器发送事件进行实时流式响应

> [card] [函数调用](https://docs.megallm.io/cn/api-reference/endpoint/function-calling.md)
>
> 使 AI 能够与外部工具和 API 交互

> [card] [身份验证指南](https://docs.megallm.io/cn/dev-docs/getting-started/authentication.md)
>
> 全面的身份验证方法和安全最佳实践

## 快速开始

> [tab] Python
>
> ```python theme={null}
> from openai import OpenAI
>
> client = OpenAI(
>     base_url="https://ai.megallm.io/v1",
>     api_key="your-api-key"
> )
>
> response = client.chat.completions.create(
>     model="gpt-4",
>     messages=[{"role": "user", "content": "Hello!"}]
> )
> ```

> [tab] JavaScript
>
> ```javascript theme={null}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>   baseURL: 'https://ai.megallm.io/v1',
>   apiKey: process.env.MEGALLM_API_KEY,
> });
>
> const response = await openai.chat.completions.create({
>   model: 'gpt-4',
>   messages: [{ role: 'user', content: 'Hello!' }]
> });
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl https://ai.megallm.io/v1/chat/completions \
>   -H "Authorization: Bearer $MEGALLM_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "model": "gpt-4",
>     "messages": [{"role": "user", "content": "Hello!"}]
>   }'
> ```

## 速率限制

速率限制因套餐等级而异：

| 等级 | 请求/分钟 | Token/分钟 | 并发数 |
| - | - | - | - |
| Basic | 60 | 90K | 10 |
| Pro | 300 | 450K | 40 |
| Enterprise | 自定义 | 自定义 | 自定义 |

## SDK 支持

MegaLLM 兼容流行的 AI SDK：

* **Python**: `openai`, `anthropic`, `langchain`
* **JavaScript/TypeScript**: `openai`, `@anthropic-ai/sdk`
* **Go**: `go-openai`
* **Ruby**: `anthropic-rb`
* **Rust**: `async-openai`
* **Java**: `openai-java`
* **C#**: `OpenAI-DotNet`

## 需要帮助？

> [card] [开发者文档](https://docs.megallm.io/cn/dev-docs/overview)
>
> 全面的指南和教程

> [card] [模型目录](https://docs.megallm.io/cn/home/models.md)
>
> 浏览所有 70 多个可用模型


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.