---
title: "API 文档"
description: "通过我们的 REST API 获取实时货币汇率。获取 API 密钥，查看文档，将实时外汇数据集成到您的应用程序中。"
canonical: https://allratestoday.com/zh/developers/
source: AllRatesToday (https://allratestoday.com/) — Markdown mirror of the page above; index of all pages at https://allratestoday.com/llms.txt
generated: 2026-12-01T04:08:08.095Z
---

# 开发者 API

通过我们的 REST API 获取实时和历史货币汇率。简单集成，数据可靠。

  [获取API密钥](https://allratestoday.com/zh/login)  [GitHub](https://github.com/AllRates-Today/exchange-rates-api) [下载用户手册](https://allratestoday.com/docs/api-documentation.pdf)

## 身份验证

所有 API 请求都需要在 Authorization 头中使用 Bearer 令牌进行身份验证：

```
Authorization: Bearer YOUR_API_KEY
```

#### 请求限制

API 请求限制为每个 IP 地址每分钟 100 次。所有响应都包含速率限制头信息：

- `X-RateLimit-Limit` - 每分钟最大请求数
- `X-RateLimit-Remaining` - 当前时间窗口内剩余请求数
- `X-RateLimit-Reset` - 限制重置的 Unix 时间戳

## 接口端点

   GET /api/v1/rates

获取汇率。不带参数时返回所有汇率，或使用 source/target 获取特定货币对。

#### 查询参数

| 参数 | 类型 | 描述 |
| --- | --- | --- |
| source可选 | string | 源货币代码（例如 USD、EUR、CNY） |
| target可选 | string | 目标货币代码 |
| time可选 | ISO 8601 | 获取特定时间点的汇率 |
| from可选 | YYYY-MM-DD | 历史数据范围的开始日期 |
| to可选 | YYYY-MM-DD | 历史数据范围的结束日期 |
| group可选 | string | 分组方式：day、hour、minute |

#### 响应示例

```
[
  {
    "rate": 1.0856,
    "source": "USD",
    "target": "EUR",
    "time": "2026-01-09T12:00:00Z"
  }
]
```

## 错误响应

| 状态码 | 描述 |
| --- | --- |
| `401` | 缺少或无效的 API 密钥 |
| `429` | 超出请求限制。请检查 Retry-After 头。 |
| `500` | 服务器内部错误 |

## 开始使用

### 1. 获取 API 密钥

注册或登录以申请您的 API 密钥。

 [申请 API 密钥](https://allratestoday.com/zh/login)

### 2. 发送您的第一个请求

```
curl -X GET "https://allratestoday.com/api/v1/rates?source=USD&target=EUR" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

### 3. 解析响应

```
[
  {
    "rate": 1.0856,
    "source": "USD",
    "target": "EUR",
    "time": "2026-01-09T12:00:00Z"
  }
]
```

### 准备好开始了吗？

立即申请 API 密钥，开始集成实时汇率数据。

 [申请 API 密钥](https://allratestoday.com/zh/login)

## JavaScript / Node.js

```
// 服务器端（Node.js 18+）：请勿将 API 密钥放在浏览器代码中
const response = await fetch(
  'https://allratestoday.com/api/v1/rates?source=USD&target=EUR',
  {
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY'
    }
  }
);

const rates = await response.json();
console.log(`1 USD = ${rates[0].rate} EUR`);
```

## Python

```
import requests

response = requests.get(
    'https://allratestoday.com/api/v1/rates',
    params={'source': 'USD', 'target': 'EUR'},
    headers={'Authorization': 'Bearer YOUR_API_KEY'}
)

rates = response.json()
print(f"1 USD = {rates[0]['rate']} EUR")
```

## PHP

```
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
    'https://allratestoday.com/api/v1/rates?source=USD&target=EUR');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$rates = json_decode($response, true);
echo "1 USD = " . $rates[0]['rate'] . " EUR";
```

## 历史数据示例

```
// 获取过去一周的 USD/EUR 汇率历史
const response = await fetch(
  'https://allratestoday.com/api/v1/rates?' + new URLSearchParams({
    source: 'USD',
    target: 'EUR',
    from: '2026-01-02',
    to: '2026-01-09',
    group: 'day'
  }),
  {
    headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
  }
);

const history = await response.json();
// 返回每日汇率数组
```
