# 速率限制

MoreLogin API 强制执行速率限制，以确保公平使用和系统稳定性。

## 限制

| API类型 | 速率限制 | 时间窗口 |
|  --- | --- | --- |
| **本地API** | 120 个请求 | 每分钟 |
| **开放API** | 120 个请求 | 每分钟 |


## 速率限制响应

当您超过速率限制时，API 将返回：

**HTTP 状态**：`429 Too Many Requests`

```json
{
  "code": 429,
  "msg": "Rate limit exceeded",
  "data": null,
  "requestId": "..."
}
```

## 最佳实践

### 1. 实施带退避的重试

```python
import time
import requests

def api_request_with_retry(url, payload, max_retries=3):
    for attempt in range(max_retries):
        response = requests.post(url, json=payload)
        if response.status_code == 429:
            wait_time = 2 ** attempt  # 1s, 2s, 4s
            print(f"Rate limited. Retrying in {wait_time}s...")
            time.sleep(wait_time)
            continue
        return response
    raise Exception("Max retries exceeded")
```

### 2. 批量操作

不要进行单独的 API 调用，而是在可用时使用批处理端点：

```python
# Bad: 100 individual calls
for env_id in env_ids:
    requests.post(f"{BASE}/api/env/start", json={"envId": env_id})

# Good: Use batch endpoints
requests.post(f"{BASE}/api/env/updateGroup/batch", json={
    "envIds": env_ids,
    "groupId": "target-group"
})
```

### 3. 缓存响应

对于不经常更改的数据（例如时区列表、平台列表），在本地缓存响应：

```python
# Cache timezone/language data — it rarely changes
timezone_data = requests.post(f"{BASE}/api/env/advanced/timezone").json()
# Reuse timezone_data for subsequent profile creations
```

## 监控您的使用情况

跟踪您的 API 调用频率。如果您始终接近极限，请考虑：

- 在每分钟内均匀分布请求
- 使用网络钩子（即将推出）而不是轮询
- 将多个操作组合成批量调用