Loading...
Please wait while we prepare your experience
Please wait while we prepare your experience
Master API integration in N8N - step-by-step code examples, authentication patterns, and production workflows
Learn to connect N8N to any external service via APIs. This comprehensive guide covers HTTP methods, authentication, error handling, and real-world integration patterns.
Simplest method - key passed in header or URL parameter.
// Method 1: Header (Recommended)
Headers:
- X-API-Key: sk_live_xxxxxxxxxxxx
- Content-Type: application/json
// Method 2: Query Parameter
URL: https://api.example.com/data?apikey=sk_live_xxxxxxxxxxxx
// Method 3: Body
POST /api/endpoint
Body: {
"apiKey": "sk_live_xxxxxxxxxxxx",
"data": { ... }
}HTTP Request Node → Authentication → None + Custom Headers → Add Header "X-API-Key" with credential
Username and password encoded in Base64.
// Raw credentials username: john@example.com password: secure_password // Base64 encoded Authorization: Basic am9obkBleGFtcGxlLmNvbTpzZWN1cmVfcGFzc3dvcmQ= // N8N automatically encodes Node: HTTP Request Authentication: Basic Auth Username: john@example.com Password: secure_password
Temporary token requiring refresh mechanism.
// Step 1: Get Access Token
POST https://auth.example.com/oauth/token
Body: {
"client_id": "your_client_id",
"client_secret": "your_secret",
"grant_type": "client_credentials"
}
Response: {
"access_token": "eyJhbGc...",
"expires_in": 3600
}
// Step 2: Use Token in API Calls
Headers:
- Authorization: Bearer eyJhbGc...
// Step 3: N8N - Store in workflow variables
Set Token Node → HTTP Request Node (use token variable)// Simple GET URL: https://api.example.com/users/123 Method: GET Headers: - Authorization: Bearer token // With Query Parameters URL: https://api.example.com/users?status=active&limit=10 OR URL: https://api.example.com/users Query Parameters: - status: active - limit: 10 - offset: 0
// Standard POST
Method: POST
URL: https://api.example.com/users
Headers:
- Content-Type: application/json
- Authorization: Bearer token
Body (JSON):
{
"name": "John Doe",
"email": "john@example.com",
"role": "user"
}
// N8N: Use JSON/RAW body, not x-www-form-urlencoded// PUT - Replace entire resource
Method: PUT
URL: https://api.example.com/users/123
Body: {
"name": "Jane Doe",
"email": "jane@example.com",
"role": "admin"
}
// PATCH - Partial update
Method: PATCH
URL: https://api.example.com/users/123
Body: {
"role": "admin" // Only update this field
}// Simple DELETE
Method: DELETE
URL: https://api.example.com/users/123
Headers:
- Authorization: Bearer token
// Often returns 204 No Content or 200 with confirmation
Response: { "success": true, "message": "User deleted" }// API Response
{
"data": {
"user": {
"id": 123,
"name": "John",
"contacts": [
{ "type": "email", "value": "john@example.com" },
{ "type": "phone", "value": "+1234567890" }
]
}
},
"status": "success"
}
// N8N - Access nested values
{{$json.data.user.id}} // = 123
{{$json.data.user.contacts[0].value}} // = john@example.com
// N8N - Transform data
Use Merge/Transform node:
{
"userId": "{{$json.data.user.id}}",
"email": "{{$json.data.user.contacts[0].value}}"
}// HTTP Request Node Error
Response Status: 400
Body: {
"error": "Invalid email format",
"code": "INVALID_INPUT"
}
// N8N - Add error handler
Continue on Fail: ON
↓
IF error occurred:
IF status === 404 → Log "User not found"
IF status === 401 → Log "Authentication failed"
IF status === 429 → Wait 60 seconds, retry
ELSE → Send alert email// API returns array of items
[
{ "id": 1, "name": "Item A" },
{ "id": 2, "name": "Item B" },
{ "id": 3, "name": "Item C" }
]
// N8N - Loop through array
Loop Node:
For each item in response:
POST to another API with item data
Store result in database
// Or use Expression:
{{$json.map(item => ({...item, processed: true}))}}Cause: Invalid/expired authentication
Fix: Verify API key, check header format, renew token
Cause: Malformed request body or invalid parameters
Fix: Validate JSON, check required fields, test in Postman first
Cause: Rate limit exceeded
Fix: Add delays, use batch endpoints, queue requests
Cause: API server down or overloaded
Fix: Implement retry with exponential backoff, add health checks
Get complete API integration workflows and real-world examples
Explore N8N AI Automations Course