快速入门:第一次调用 MotoGP API
在 RapidAPI 订阅,发送两个请求头,并用 curl、JavaScript 或 Python 读取当前 MotoGP 锦标赛积分榜。
阅读约 5 分钟
本指南带你在约五分钟内从零完成一次可用的请求。你需要一个 RapidAPI 账号和一个终端。
1. 订阅并复制密钥
- 打开 RapidAPI 上的 MotoGP API 并选择一个套餐。
- 在 RapidAPI 的 playground 中复制 X-RapidAPI-Key 的值。该密钥用于标识你的订阅,RapidAPI 会按它统计你的配额。
请把密钥保存在环境变量中,而不是直接粘贴到代码里:
export RAPIDAPI_KEY="paste-your-key-here"
2. 发送第一个请求
每个请求都需要两个请求头:你的密钥和 API 主机。
curl -s "https://motogp-api-championship-data-statistics.p.rapidapi.com/standings/seasons/2026/riders?category=MOTOGP" \
-H "X-RapidAPI-Key: $RAPIDAPI_KEY" \
-H "X-RapidAPI-Host: motogp-api-championship-data-statistics.p.rapidapi.com"
响应是 2026 年 MotoGP 车手锦标赛,已按名次排好序:
{
"data": [
{
"position": 1,
"total_points": 365,
"wins": 6,
"podiums": 6,
"season_year": 2026,
"rider_name": "Marc Márquez",
"rider_number": 93,
"team_name": "Ducati Lenovo Team",
"category": "MOTOGP"
}
],
"meta": { "total": 32, "page": null, "size": null, "total_pages": null }
}
3. 不要写死赛季
GET /seasons/latest 返回最新赛季,因此新赛季开始时你的代码依然可用。把它与积分榜调用串联起来:
JavaScript (Node.js 18+)
const HOST = "motogp-api-championship-data-statistics.p.rapidapi.com";
const headers = {
"X-RapidAPI-Key": process.env.RAPIDAPI_KEY,
"X-RapidAPI-Host": HOST,
};
async function get(path) {
const res = await fetch(`https://${HOST}${path}`, { headers });
const body = await res.json();
if (!res.ok) throw new Error(`${res.status} ${body.message ?? res.statusText}`);
return body.data;
}
const season = await get("/seasons/latest");
const standings = await get(`/standings/seasons/${season.year}/riders?category=MOTOGP`);
for (const row of standings.slice(0, 5)) {
console.log(`${row.position}. ${row.rider_name} (#${row.rider_number}) – ${row.total_points} pts`);
}
Python
import os
import requests
HOST = "motogp-api-championship-data-statistics.p.rapidapi.com"
HEADERS = {"X-RapidAPI-Key": os.environ["RAPIDAPI_KEY"], "X-RapidAPI-Host": HOST}
def get(path: str, **params):
res = requests.get(f"https://{HOST}{path}", headers=HEADERS, params=params, timeout=10)
res.raise_for_status()
return res.json()["data"]
season = get("/seasons/latest")
standings = get(f"/standings/seasons/{season['year']}/riders", category="MOTOGP")
for row in standings[:5]:
print(f"{row['position']}. {row['rider_name']} (#{row['rider_number']}) – {row['total_points']} pts")
输出:
1. Marc Márquez (#93) – 365 pts
2. Jorge Martín (#89) – 355 pts
3. Marco Bezzecchi (#72) – 315 pts
...
下一步
- 阅读约定,了解组别、分页、错误和缓存。
- 为你的网站构建一个积分榜表格。
- 在 MotoGP API 参考中浏览所有端点。