Design request batching APIs to reduce network overhead and improve client performance.
You are a frontend-focused backend engineer optimizing APIs for client efficiency.
## CONTEXT
I need to implement request batching for my API.
**Requirements:**
- Client Types: {{CLIENTS}}
- Batch Scenarios: {{SCENARIOS}}
- Latency Goals: {{LATENCY}}
- Complexity: {{COMPLEXITY}}
## TASK
Design request batching API:
### 1. BATCH ENDPOINT
```json
POST /api/batch
{
"requests": [
{ "id": "1", "method": "GET", "path": "/users/123" },
{ "id": "2", "method": "GET", "path": "/orders?userId=123" },
{ "id": "3", "method": "POST", "path": "/analytics", "body": {...} }
]
}
```
### 2. BATCH RESPONSE
```json
{
"responses": [
{ "id": "1", "status": 200, "body": { "user": {...} } },
{ "id": "2", "status": 200, "body": { "orders": [...] } },
{ "id": "3", "status": 201, "body": { "tracked": true } }
]
}
```
### 3. IMPLEMENTATION
```typescript
async function handleBatch(requests) {
const results = await Promise.all(
requests.map(async (req) => {
try {
const response = await routeRequest(req);
return { id: req.id, ...response };
} catch (error) {
return { id: req.id, status: 500, error: error.message };
}
})
);
return { responses: results };
}
```
### 4. LIMITS AND VALIDATION
- Max requests per batch
- Request size limits
- Timeout handling
### 5. DEPENDENCY HANDLING
Sequential requests within batch.
### 6. PERFORMANCE
- Parallel execution
- Connection pooling
- Response streamingOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{CLIENTS][{SCENARIOS][{LATENCY][{COMPLEXITY]{
"requests": [
{ "id": "1", "method": "GET", "path": "/users/123" }{ "id": "2", "method": "GET", "path": "/orders?userId=123" }{ "id": "3", "method": "POST", "path": "/analytics", "body": {...}{
"responses": [
{ "id": "1", "status": 200, "body": { "user": {...}{ "id": "2", "status": 200, "body": { "orders": [...] }{ "id": "3", "status": 201, "body": { "tracked": true }{
const results = await Promise.all(
requests.map(async (req) => {
try {
const response = await routeRequest(req);
return { id: req.id, ...response }{
return { id: req.id, status: 500, error: error.message }{ responses: results }