API Integration Patterns
Connect to any REST API, handle authentication, pagination, and rate limits.
Connecting to External APIs
n8n's HTTP Request node connects to any REST API. Configure: method, URL, headers, body, authentication, and response parsing.
Authentication Patterns
API Key — pass in a header (Authorization: Bearer {key}) or query parameter. Store keys in n8n credentials, never hardcode.
OAuth 2.0 — n8n handles the OAuth flow for supported integrations. For custom OAuth, use the OAuth2 credential type.
HMAC — generate a signature from request contents. Used by Stripe, GitHub webhooks. Implement in a Function node.
Pagination
Most APIs paginate large result sets. Common patterns:
Offset pagination — ?page=2&limit=50. Loop until results < page size.
Cursor pagination — ?cursor=abc123. Loop until cursor is null.
Link header — response includes a Link: <url>; rel="next" header. Follow until no "next" link.
Build the pagination loop in a Function node with recursive calls or use n8n's built-in Split In Batches node.
Rate Limit Handling
APIs throttle requests. Handle rate limits:
- Check for 429 status code
- Read the
Retry-Afterheader - Wait for the specified duration
- Retry the request
Some APIs use soft limits (headers warning you're approaching limits) — monitor these proactively.
The Resilient API Client Pattern
async function callWithRetry(url, options, maxRetries = 3) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
const response = await fetch(url, options);
if (response.ok) return response.json();
if (response.status === 429) {
const wait = parseInt(response.headers.get('Retry-After') || '60');
await sleep(wait * 1000);
continue;
}
if (attempt === maxRetries) throw new Error(`Failed: ${response.status}`);
}
}