|
| 1 | +# API Reference |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document details the specifications and usage of our RESTful API endpoints. |
| 5 | + |
| 6 | +## Authentication |
| 7 | + |
| 8 | +### API Key Authentication |
| 9 | +Include your API key in all requests via the Authorization header: |
| 10 | +```bash |
| 11 | +Authorization: Bearer YOUR_API_KEY |
| 12 | +``` |
| 13 | + |
| 14 | +### OAuth 2.0 Endpoints |
| 15 | + |
| 16 | +#### Authorization Request |
| 17 | +``` |
| 18 | +GET /oauth/authorize |
| 19 | +``` |
| 20 | +Parameters: |
| 21 | +- client_id (required) |
| 22 | +- redirect_uri (required) |
| 23 | +- scope (optional) |
| 24 | +- state (recommended) |
| 25 | + |
| 26 | +#### Token Exchange |
| 27 | +``` |
| 28 | +POST /oauth/token |
| 29 | +``` |
| 30 | +Parameters: |
| 31 | +- grant_type (required) |
| 32 | +- code (required) |
| 33 | +- client_id (required) |
| 34 | +- client_secret (required) |
| 35 | +- redirect_uri (required) |
| 36 | + |
| 37 | +## Request/Response Standards |
| 38 | + |
| 39 | +### Headers |
| 40 | +Required request headers: |
| 41 | +``` |
| 42 | +Content-Type: application/json |
| 43 | +Authorization: Bearer YOUR_API_KEY |
| 44 | +Accept: application/json |
| 45 | +``` |
| 46 | + |
| 47 | +### Response Format |
| 48 | +All responses follow this structure: |
| 49 | +```json |
| 50 | +{ |
| 51 | + "status": "success|error", |
| 52 | + "data": { |
| 53 | + // Response payload |
| 54 | + }, |
| 55 | + "meta": { |
| 56 | + "page": 1, |
| 57 | + "perPage": 25, |
| 58 | + "total": 100 |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Rate Limits |
| 64 | + |
| 65 | +### Default Limits |
| 66 | +- 1000 requests per minute per API key |
| 67 | +- 10000 requests per hour per API key |
| 68 | + |
| 69 | +### Headers |
| 70 | +Rate limit information is returned in headers: |
| 71 | +``` |
| 72 | +X-RateLimit-Limit: 1000 |
| 73 | +X-RateLimit-Remaining: 999 |
| 74 | +X-RateLimit-Reset: 1640995200 |
| 75 | +``` |
| 76 | + |
| 77 | +### Exceeded Limits |
| 78 | +When rate limited, receives 429 response with: |
| 79 | +```json |
| 80 | +{ |
| 81 | + "status": "error", |
| 82 | + "error": { |
| 83 | + "code": "RATE_LIMIT_EXCEEDED", |
| 84 | + "message": "Rate limit exceeded. Please try again in 60 seconds", |
| 85 | + "retryAfter": 60 |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Error Codes |
| 91 | + |
| 92 | +### HTTP Status Codes |
| 93 | +- 200: Success |
| 94 | +- 201: Created |
| 95 | +- 204: No Content |
| 96 | +- 400: Bad Request |
| 97 | +- 401: Unauthorized |
| 98 | +- 403: Forbidden |
| 99 | +- 404: Not Found |
| 100 | +- 429: Too Many Requests |
| 101 | +- 500: Internal Server Error |
| 102 | + |
| 103 | +### Error Response Format |
| 104 | +```json |
| 105 | +{ |
| 106 | + "status": "error", |
| 107 | + "error": { |
| 108 | + "code": "ERROR_CODE", |
| 109 | + "message": "Human readable message", |
| 110 | + "details": [ |
| 111 | + { |
| 112 | + "field": "email", |
| 113 | + "message": "Invalid email format" |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### Common Error Codes |
| 121 | +| Code | Description | |
| 122 | +|------|-------------| |
| 123 | +| VALIDATION_ERROR | Invalid input data | |
| 124 | +| AUTHENTICATION_ERROR | Invalid credentials | |
| 125 | +| AUTHORIZATION_ERROR | Insufficient permissions | |
| 126 | +| RESOURCE_NOT_FOUND | Requested resource doesn't exist | |
| 127 | +| RATE_LIMIT_EXCEEDED | Too many requests | |
| 128 | +| INTERNAL_ERROR | Server-side error | |
| 129 | + |
| 130 | +## API Endpoints |
| 131 | + |
| 132 | +### Users |
| 133 | + |
| 134 | +#### List Users |
| 135 | +``` |
| 136 | +GET /v1/users |
| 137 | +``` |
| 138 | +Query parameters: |
| 139 | +- page (default: 1) |
| 140 | +- perPage (default: 25) |
| 141 | +- sortBy (default: "createdAt") |
| 142 | +- sortOrder (default: "desc") |
| 143 | + |
| 144 | +Response: |
| 145 | +```json |
| 146 | +{ |
| 147 | + "status": "success", |
| 148 | + "data": [ |
| 149 | + { |
| 150 | + "id": "123", |
| 151 | + |
| 152 | + "name": "John Doe", |
| 153 | + "createdAt": "2023-01-01T00:00:00Z" |
| 154 | + } |
| 155 | + ], |
| 156 | + "meta": { |
| 157 | + "page": 1, |
| 158 | + "perPage": 25, |
| 159 | + "total": 100 |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +#### Create User |
| 165 | +``` |
| 166 | +POST /v1/users |
| 167 | +``` |
| 168 | +Request body: |
| 169 | +```json |
| 170 | +{ |
| 171 | + |
| 172 | + "name": "John Doe", |
| 173 | + "role": "user" |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +#### Get User |
| 178 | +``` |
| 179 | +GET /v1/users/{id} |
| 180 | +``` |
| 181 | + |
| 182 | +#### Update User |
| 183 | +``` |
| 184 | +PUT /v1/users/{id} |
| 185 | +``` |
| 186 | +Request body: |
| 187 | +```json |
| 188 | +{ |
| 189 | + "name": "John Smith", |
| 190 | + "role": "admin" |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +#### Delete User |
| 195 | +``` |
| 196 | +DELETE /v1/users/{id} |
| 197 | +``` |
| 198 | + |
| 199 | +## Usage Examples |
| 200 | + |
| 201 | +### Authentication |
| 202 | +```python |
| 203 | +import requests |
| 204 | + |
| 205 | +API_KEY = 'your_api_key' |
| 206 | +headers = { |
| 207 | + 'Authorization': f'Bearer {API_KEY}', |
| 208 | + 'Content-Type': 'application/json' |
| 209 | +} |
| 210 | + |
| 211 | +# List users |
| 212 | +response = requests.get( |
| 213 | + 'https://api.example.com/v1/users', |
| 214 | + headers=headers |
| 215 | +) |
| 216 | + |
| 217 | +# Create user |
| 218 | +user_data = { |
| 219 | + |
| 220 | + 'name': 'John Doe', |
| 221 | + 'role': 'user' |
| 222 | +} |
| 223 | +response = requests.post( |
| 224 | + 'https://api.example.com/v1/users', |
| 225 | + headers=headers, |
| 226 | + json=user_data |
| 227 | +) |
| 228 | +``` |
| 229 | + |
| 230 | +### Pagination |
| 231 | +```python |
| 232 | +def fetch_all_users(): |
| 233 | + users = [] |
| 234 | + page = 1 |
| 235 | + |
| 236 | + while True: |
| 237 | + response = requests.get( |
| 238 | + 'https://api.example.com/v1/users', |
| 239 | + params={'page': page}, |
| 240 | + headers=headers |
| 241 | + ) |
| 242 | + data = response.json() |
| 243 | + |
| 244 | + if not data['data']: |
| 245 | + break |
| 246 | + |
| 247 | + users.extend(data['data']) |
| 248 | + page += 1 |
| 249 | + |
| 250 | + return users |
| 251 | +``` |
| 252 | + |
| 253 | +### Error Handling |
| 254 | +```python |
| 255 | +try: |
| 256 | + response = requests.post( |
| 257 | + 'https://api.example.com/v1/users', |
| 258 | + headers=headers, |
| 259 | + json=user_data |
| 260 | + ) |
| 261 | + response.raise_for_status() |
| 262 | + user = response.json()['data'] |
| 263 | + |
| 264 | +except requests.exceptions.HTTPError as e: |
| 265 | + if e.response.status_code == 429: |
| 266 | + # Handle rate limiting |
| 267 | + retry_after = int(e.response.headers.get('Retry-After', 60)) |
| 268 | + time.sleep(retry_after) |
| 269 | + else: |
| 270 | + # Handle other errors |
| 271 | + error_data = e.response.json() |
| 272 | + print(f"API Error: {error_data['error']['message']}") |
| 273 | +``` |
0 commit comments