欢迎使用 Ruanm API!本文档将帮助您了解如何使用我们的 API 来构建功能丰富的应用程序。
所有 API 请求都应发送到 http://localhost:3000/api
http://localhost:3000/api
大多数 API 端点都需要认证。我们使用 JWT (JSON Web Tokens) 进行认证。
要获取访问令牌,您需要先注册开发者账号并获得 API 密钥。
POST /api/auth/token
Content-Type: application/json
{
"api_key": "your_api_key_here"
}
{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 3600
}
获取令牌后,在后续请求的 Authorization 头中包含它:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
获取指定用户的公开信息。
GET /api/users/{userId}
Authorization: Bearer YOUR_ACCESS_TOKEN
{
"success": true,
"data": {
"id": "123456",
"username": "example_user",
"email": "user@example.com",
"avatar": "https://example.com/avatar.jpg",
"created_at": "2025-01-01T00:00:00Z"
}
}
更新当前认证用户的个人信息。
PUT /api/users/{userId}
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json
{
"username": "new_username",
"email": "new_email@example.com"
}
{
"success": true,
"data": {
"id": "123456",
"username": "new_username",
"email": "new_email@example.com",
"avatar": "https://example.com/avatar.jpg",
"created_at": "2025-01-01T00:00:00Z",
"updated_at": "2025-01-02T00:00:00Z"
}
}
获取所有可用游戏的列表。
GET /api/games
Authorization: Bearer YOUR_ACCESS_TOKEN
{
"success": true,
"data": [
{
"id": "game1",
"name": "超级马里奥",
"description": "经典平台跳跃游戏",
"thumbnail": "https://example.com/mario.jpg",
"category": "platformer"
},
{
"id": "game2",
"name": "俄罗斯方块",
"description": "经典的益智游戏",
"thumbnail": "https://example.com/tetris.jpg",
"category": "puzzle"
}
]
}
获取所有可用产品的列表。
GET /api/products
Authorization: Bearer YOUR_ACCESS_TOKEN
{
"success": true,
"data": [
{
"id": "product1",
"name": "Ruanm 屏保程序",
"description": "个性化您的Windows体验",
"thumbnail": "https://example.com/screensaver.jpg",
"price": 0
},
{
"id": "product2",
"name": "Ruanm Studio",
"description": "强大的软件开发工具包",
"thumbnail": "https://example.com/studio.jpg",
"price": 0
}
]
}
当 API 请求出现问题时,我们会返回标准的错误响应格式。
{
"success": false,
"error": "错误信息描述",
"code": "ERROR_CODE"
}
| 错误代码 | 描述 | HTTP 状态码 |
|---|---|---|
| UNAUTHORIZED | 未提供有效的认证信息 | 401 |
| FORBIDDEN | 权限不足 | 403 |
| NOT_FOUND | 请求的资源不存在 | 404 |
| VALIDATION_ERROR | 请求参数验证失败 | 400 |
| INTERNAL_ERROR | 服务器内部错误 | 500 |
为了保证服务质量,API 对请求频率有所限制。
默认情况下,每个 API 密钥每小时最多可以发送 1000 个请求。
每个响应都包含速率限制相关信息的头部:
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 999
X-RateLimit-Reset: 1609459200
如果超过速率限制,API 将返回 429 状态码和相应的错误信息。
以下是一些使用 Ruanm API 的实用代码示例。
使用 JavaScript 获取用户信息:
// 获取访问令牌
async function getAccessToken(apiKey) {
const response = await fetch('http://localhost:3000/api/auth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ api_key: apiKey })
});
const data = await response.json();
return data.token;
}
// 获取用户信息
async function getUserInfo(userId, token) {
const response = await fetch(`http://localhost:3000/api/users/${userId}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
return data;
}
// 使用示例
(async () => {
try {
const token = await getAccessToken('your_api_key_here');
const userInfo = await getUserInfo('123456', token);
console.log(userInfo);
} catch (error) {
console.error('Error:', error);
}
})();
使用 Python 获取游戏列表:
import requests
def get_access_token(api_key):
url = "http://localhost:3000/api/auth/token"
payload = {"api_key": api_key}
response = requests.post(url, json=payload)
return response.json()["token"]
def get_games(token):
url = "http://localhost:3000/api/games"
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(url, headers=headers)
return response.json()
# 使用示例
try:
token = get_access_token("your_api_key_here")
games = get_games(token)
print(games)
except Exception as e:
print(f"Error: {e}")
遵循以下最佳实践可以帮助您更好地使用 Ruanm API。
始终将访问令牌存储在安全的地方,不要将其硬编码在客户端代码中。对于Web应用,考虑使用HTTP-only cookies存储令牌。
始终检查API响应的状态码和错误信息,适当处理各种错误情况,提供友好的用户体验。
对于不经常变化的数据,使用适当的缓存策略可以减少API调用次数,提高应用性能。
监控您的API使用情况,确保不超过速率限制。在接近限制时实施适当的节流机制。
提示:定期查看API文档更新,了解新增功能和改进。