> ## Documentation Index
> Fetch the complete documentation index at: https://oddspapi-v4.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速开始：5 分钟完成第一次赔率 API 请求

> 获取免费的 OddsPapi API 密钥，三次请求即可拿到实时博彩赔率。提供适用于 v4 REST API 的 curl、Python 和 JavaScript 可复制示例。

本页带您用三次请求从零拿到真实的博彩公司价格。您只需要一个 API 密钥。

> [!NOTE]
>
> 这些步骤会使用您每月配额中的 **3 次请求**。免费套餐每月包含 250 次请求，覆盖所有体育项目和博彩公司。一次请求就是一次调用，无论它返回多少盘口和博彩公司。

## 1. 获取 API 密钥

[创建免费账户](https://oddspapi.io/zh/sign-up)，然后在[账户设置](https://oddspapi.io/zh/account)中复制您的密钥。

把它保存在环境变量中，避免写进代码：

```bash theme={null}
export ODDSPAPI_KEY="YOUR_API_KEY"
```

## 2. 找到一个体育项目

每个请求都需要 ID，所以先列出体育项目。

```bash curl theme={null}
curl "https://api.oddspapi.io/v4/sports?apiKey=$ODDSPAPI_KEY"
```

```python Python theme={null}
import os
import requests

BASE = "https://api.oddspapi.io/v4"
KEY = os.environ["ODDSPAPI_KEY"]

sports = requests.get(f"{BASE}/sports", params={"apiKey": KEY}).json()
print(sports[:2])
```

```javascript JavaScript theme={null}
const BASE = "https://api.oddspapi.io/v4";
const KEY = process.env.ODDSPAPI_KEY;

const sports = await fetch(`${BASE}/sports?apiKey=${KEY}`).then((r) => r.json());
console.log(sports.slice(0, 2));
```

```json theme={null}
[
  { "sportId": 10, "slug": "soccer", "sportName": "Soccer" },
  { "sportId": 11, "slug": "basketball", "sportName": "Basketball" }
]
```

足球的 `sportId` 是 10。下一步会用到它。

## 3. 选择一个联赛

```bash curl theme={null}
curl "https://api.oddspapi.io/v4/tournaments?sportId=10&apiKey=$ODDSPAPI_KEY"
```

```python Python theme={null}
tournaments = requests.get(
    f"{BASE}/tournaments", params={"sportId": 10, "apiKey": KEY}
).json()

for t in tournaments[:5]:
    print(t["tournamentId"], t["categoryName"], t["tournamentName"], t["upcomingFixtures"])
```

```javascript JavaScript theme={null}
const tournaments = await fetch(
  `${BASE}/tournaments?sportId=10&apiKey=${KEY}`
).then((r) => r.json());

for (const t of tournaments.slice(0, 5)) {
  console.log(t.tournamentId, t.categoryName, t.tournamentName, t.upcomingFixtures);
}
```

```json theme={null}
[
  {
    "tournamentId": 17,
    "tournamentSlug": "premier-league",
    "tournamentName": "Premier League",
    "categorySlug": "england",
    "categoryName": "England",
    "futureFixtures": 231,
    "upcomingFixtures": 0,
    "liveFixtures": 0
  }
]
```

英超的 `tournamentId` 是 17。三个计数器告诉您某个联赛当前是否有值得获取的赛事。

## 4. 获取赔率

一次调用返回一个联赛所有即将开始的赛事以及一家博彩公司的赔率。您最多可以传入 5 个联赛 ID，用逗号分隔。

> [!NOTE]
>
> 示例使用 `bookmaker=pinnacle%2B30`。请把它换成您套餐包含的博彩公司 slug：[`GET /v4/account`](https://oddspapi-v4.mintlify.site/zh/docs/get-account) 会列出它们。在自助套餐中，Pinnacle 的 slug 是 `pinnacle+30`，在 URL 中需编码为 `pinnacle%2B30`。

```bash curl theme={null}
curl "https://api.oddspapi.io/v4/odds-by-tournaments?bookmaker=pinnacle%2B30&tournamentIds=17&apiKey=$ODDSPAPI_KEY"
```

```python Python theme={null}
fixtures = requests.get(
    f"{BASE}/odds-by-tournaments",
    params={"bookmaker": "pinnacle+30", "tournamentIds": "17", "apiKey": KEY},
).json()

for fixture in fixtures:
    markets = fixture["bookmakerOdds"]["pinnacle+30"]["markets"]
    market = markets.get("101")  # 101 = soccer Full Time Result (1X2)
    if not market:
        continue
    prices = {
        outcome_id: outcome["players"]["0"]["price"]
        for outcome_id, outcome in market["outcomes"].items()
    }
    print(fixture["fixtureId"], fixture["startTime"], prices)
```

```javascript JavaScript theme={null}
const fixtures = await fetch(
  `${BASE}/odds-by-tournaments?bookmaker=pinnacle%2B30&tournamentIds=17&apiKey=${KEY}`
).then((r) => r.json());

for (const fixture of fixtures) {
  const market = fixture.bookmakerOdds["pinnacle+30"].markets["101"]; // 101 = soccer Full Time Result (1X2)
  if (!market) continue;
  const prices = Object.fromEntries(
    Object.entries(market.outcomes).map(([id, o]) => [id, o.players["0"].price])
  );
  console.log(fixture.fixtureId, fixture.startTime, prices);
}
```

## 5. 读取一个价格

赔率响应是嵌套结构，通往一个价格的路径始终相同：

```text theme={null}
bookmakerOdds → {bookmaker} → markets → {marketId} → outcomes → {outcomeId} → players → {playerId} → price
```

```json theme={null}
{
  "fixtureId": "id1000001764618978",
  "startTime": "2025-12-08T20:00:00.000Z",
  "bookmakerOdds": {
    "pinnacle+30": {
      "markets": {
        "101": {
          "outcomes": {
            "101": { "players": { "0": { "active": true, "price": 5.01, "limit": 5000 } } },
            "102": { "players": { "0": { "active": true, "price": 4.32, "limit": 5000 } } },
            "103": { "players": { "0": { "active": true, "price": 1.649, "limit": 7704 } } }
          }
        }
      }
    }
  }
}
```

| 部分 | 含义 |
| - | - |
| `marketId` | 投注的内容。`101` 是足球的全场胜平负（1X2）。通过 [`/v4/markets`](https://oddspapi-v4.mintlify.site/zh/docs/get-markets) 查询一次 ID 并缓存。 |
| `outcomeId` | 盘口中的选项。这里 `101` 是主胜（1），`102` 是平局（X），`103` 是客胜（2）。 |
| `playerId` | 普通盘口为 `0`。球员盘口中为球员的 ID。 |
| `price` | 小数赔率。 |
| `active` | `false` 表示博彩公司当前不提供该价格。请不要使用。 |
| `limit` | 博彩公司对该价格的投注限额（如果博彩公司公布）。 |

## 充分利用免费套餐

* **请求完整的赛事**。一次 [`/v4/odds`](https://oddspapi-v4.mintlify.site/zh/docs/get-odds) 调用会返回该赛事在您套餐内所有博彩公司的所有盘口。您不按盘口或博彩公司付费。
* **用历史数据做研究**。[`/v4/historical-odds`](https://oddspapi-v4.mintlify.site/zh/docs/get-historical-odds) 包含每一次价格变化，且永远不消耗配额，因此回测是免费的。
* **缓存参考数据**。体育项目、博彩公司、盘口和语言很少变化。获取一次并保存即可。请参阅[哪些算作一次请求](https://oddspapi-v4.mintlify.site/zh/docs/requests-and-quota.md)。
* **免费套餐不会被降速**。您获得的价格和速度与付费套餐相同。

## 接下来做什么

> [card] [比较博彩公司](https://oddspapi-v4.mintlify.site/zh/docs/get-odds.md)
>
> `/v4/odds` 一次返回一场赛事在多家博彩公司的赔率。

> [card] [免费的历史赔率](https://oddspapi-v4.mintlify.site/zh/docs/get-historical-odds.md)
>
> 一场赛事的每一次价格变化。不消耗配额。

> [card] [让 250 次请求更耐用](https://oddspapi-v4.mintlify.site/zh/docs/requests-and-quota.md)
>
> 哪些算作一次请求，以及如何少用请求。

> [card] [WebSocket (v5)](https://oddspapi-v4.mintlify.site/zh/docs/websocket-api.md)
>
> v4 是 REST。如何做好轮询，以及 WebSocket 在哪里。

## 第一天的常见问题

| 您看到 | 原因 | 解决方法 |
| - | - | - |
| `401 MISSING_API_KEY` 或 `INVALID_API_KEY` | 缺少 `apiKey` 参数或拼写有误 | 从账户设置中重新复制密钥 |
| `403 RESTRICTED_ACCESS` | 您的套餐不包含该体育项目或博彩公司 | 通过 [`/v4/account`](https://oddspapi-v4.mintlify.site/zh/docs/get-account) 查看您的密钥包含哪些内容 |
| `429 RATE_LIMITED` | 对同一端点的两次调用间隔太短 | 等待 `retryMs` 后重试 |
| `429 REQUEST_LIMIT_EXCEEDED` | 每月配额已用完 | 免费配额在每月 1 日重置。需要更多？[升级套餐](https://oddspapi.io/zh/pricing) |
| `bookmakerOdds` 为空 | 该博彩公司暂时没有这场赛事的赔率 | 换一家博彩公司，或用 `hasOdds=true` 筛选赛事 |

完整列表见[错误](https://oddspapi-v4.mintlify.site/zh/docs/errors.md)页面。


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