HTTP Request: Call Any API
Ch06 HTTP Request: Call Any API
The HTTP Request node is n8n's most-used node by a wide margin. It lets you call virtually any REST API โ from OpenAI to Stripe, from internal microservices to government open data platforms โ without writing a single line of networking code. This chapter covers every configuration option in depth, with complete examples for the most common API call patterns.
Node Configuration Reference
Basic Settings
Method: HTTP method โ GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS.
URL: Target API endpoint; supports n8n expressions for dynamic values, e.g., https://api.github.com/repos/{{ $json.owner }}/{{ $json.repo }}/issues.
Authentication: Built-in options include:
Predefined Credential Type: Select from saved Credentials (recommended)Generic Credential Type: Custom Header Auth, Basic Auth, Bearer TokenNone: Public APIs that require no auth
Headers
Most REST APIs need Content-Type and Authorization headers:
{
"Content-Type": "application/json",
"Authorization": "Bearer {{ $vars.API_KEY }}",
"X-Custom-Header": "custom-value"
}
Body
POST/PUT/PATCH requests typically need a body:
- JSON: Most common, pairs with
Content-Type: application/json - Form Data:
application/x-www-form-urlencoded - Multipart Form Data: File uploads
- Raw/Binary: Raw byte streams
Query Parameters
GET parameters go in Query Parameters โ n8n handles URL encoding automatically:
page=1&limit=20&status=active&search={{ $json.keyword }}
Response Handling
Response Format:
Automatic(default): Auto-detects; JSON is parsed, others become textJSON: Force JSON parsingText: Return as a plain stringFile: Return as binary data (for file downloads)
Practical Examples
Example 1: Call the OpenAI Chat API
{
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{ $vars.OPENAI_API_KEY }}",
"Content-Type": "application/json"
},
"body": {
"model": "gpt-4o",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "{{ $json.userMessage }}" }
],
"temperature": 0.7,
"max_tokens": 1000
}
}
After execution, $json.choices[0].message.content contains the AI response.
Example 2: Create a GitHub Issue
{
"method": "POST",
"url": "https://api.github.com/repos/{{ $json.owner }}/{{ $json.repo }}/issues",
"headers": {
"Authorization": "token {{ $vars.GITHUB_TOKEN }}",
"Accept": "application/vnd.github.v3+json"
},
"body": {
"title": "{{ $json.issueTitle }}",
"body": "{{ $json.issueBody }}",
"labels": ["bug", "auto-created"]
}
}
Example 3: Handle Paginated APIs
Many APIs paginate their responses. Use a Loop and Code node to auto-paginate:
// Code node: pagination logic
const staticData = $getWorkflowStaticData('global');
const allItems = staticData.collectedItems || [];
const currentPageItems = items.map(i => i.json);
allItems.push(...currentPageItems);
const hasNextPage = items.length === 100; // Assuming 100 per page
const nextPage = (staticData.currentPage || 1) + 1;
if (hasNextPage) {
staticData.currentPage = nextPage;
staticData.collectedItems = allItems;
return items; // Loop continues
} else {
// All pages collected โ reset and return everything
staticData.currentPage = 1;
staticData.collectedItems = [];
return allItems.map(item => ({ json: item }));
}
Advanced Features
Batch Size: Rate-Limit API Calls
In Options โ Batch Size, set how many items to process per batch. Combine with Wait Between Batches to pace API calls and avoid rate limits:
Batch Size: 5
Wait Between Batches: 1000 # 1 second between batches
Timeout and Retry
Configure in Options:
- Timeout: Request timeout in milliseconds (default: 300000 = 5 minutes)
- Retry On Fail: Auto-retry on failure โ recommended for unstable external APIs
Allow Unauthorized Certs
For internal services with self-signed certificates, enable Options โ Allow Unauthorized Certs (not recommended for public internet services).
Accessing Response Headers
Access response headers via $response.headers:
- Rate limit info:
$response.headers['x-ratelimit-remaining'] - Next page URL:
$response.headers['link'] - Request ID:
$response.headers['x-request-id']
Common Error Reference
| Error | Cause | Solution |
|---|---|---|
| 401 Unauthorized | API key wrong or missing | Check Authorization header config |
| 403 Forbidden | Insufficient permissions | Verify API key scope |
| 404 Not Found | Wrong URL path | Check API docs for correct endpoint |
| 429 Too Many Requests | Rate limited | Enable Batch Size + Wait, or upgrade API plan |
| 500 Internal Server Error | Target service down | Enable retry; check service status page |
| ECONNREFUSED | Service unreachable | Verify network connectivity and port access |
Debug tip: Enable Full Response in node Settings to include status code (
$json.statusCode), headers ($json.headers), and body ($json.body) in the output โ invaluable for debugging non-2xx responses.