5.6 KiB
N8N_API_KEY Missing - Server Configuration Error
🔍 Problem Identified
Error: 500 - "Server configuration error"
Cause: N8N_API_KEY is NOT set in the server's environment variables.
✅ Solution: Add N8N_API_KEY to Environment Variables
The Error
Looking at app/api/missions/mission-created/route.ts (lines 34-39):
if (!expectedApiKey) {
logger.error('N8N_API_KEY not configured in environment');
return NextResponse.json(
{ error: 'Server configuration error' },
{ status: 500 }
);
}
This error means: process.env.N8N_API_KEY is undefined or empty.
🔧 How to Fix
Step 1: Determine Your Environment
Are you running:
- Local development?
- Production server?
- Docker container?
- Vercel/other hosting?
Step 2: Add N8N_API_KEY
Option A: Local Development (.env.local)
Create or edit .env.local file in your project root:
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
Then restart your development server:
# Stop the server (Ctrl+C)
# Restart
npm run dev
# or
yarn dev
Option B: Production Server
If using Docker:
Add to docker-compose.yml:
services:
app:
environment:
- N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
Or in .env file (if using docker-compose with env_file):
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
If using CapRover:
- Go to App Settings
- App Configs → Environment Variables
- Add:
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4 - Save and restart the app
If using Vercel:
- Go to Project Settings
- Environment Variables
- Add:
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4 - Redeploy
If using other hosting:
- Add
N8N_API_KEYto your hosting platform's environment variables - Restart/redeploy the application
🧪 Verification
Step 1: Check if Variable is Set
Create a test endpoint to verify:
// app/api/test-env/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
return NextResponse.json({
hasN8NApiKey: !!process.env.N8N_API_KEY,
n8nApiKeyLength: process.env.N8N_API_KEY?.length || 0,
// Don't expose the actual key!
});
}
Then visit: http://localhost:3000/api/test-env (or your production URL)
Expected:
{
"hasN8NApiKey": true,
"n8nApiKeyLength": 32
}
Step 2: Test the Endpoint Manually
After adding the variable and restarting:
curl -X POST https://hub.slm-lab.net/api/missions/mission-created \
-H "Content-Type: application/json" \
-H "x-api-key: LwgeE1ntADD20OuWC88S3pR0EaO7FtO4" \
-d '{
"missionId": "test-mission-id",
"name": "Test Mission",
"creatorId": "test-user-id"
}'
Expected:
- ✅ 200 OK with JSON response (if mission exists)
- ❌ 500 error if
N8N_API_KEYis still not set - ❌ 401 error if API key doesn't match
Step 3: Check Server Logs
After adding the variable, check your server logs. You should NOT see:
N8N_API_KEY not configured in environment
You SHOULD see (when endpoint is called):
Mission Created Webhook Received
Received mission-created data: { ... }
🔍 Troubleshooting
Issue 1: Variable Not Loading
Symptom: Still getting 500 error after adding variable
Possible causes:
- Wrong file: Using
.envinstead of.env.local(Next.js prefers.env.local) - Not restarted: Server needs restart after adding env variable
- Wrong location:
.env.localmust be in project root (same level aspackage.json) - Syntax error: Check for quotes, spaces, or special characters
Fix:
# Correct
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
# Wrong (with quotes)
N8N_API_KEY="LwgeE1ntADD20OuWC88S3pR0EaO7FtO4"
# Wrong (with spaces)
N8N_API_KEY = LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
Issue 2: Different Key in N8N
Symptom: 401 Unauthorized error
Cause: The API key in N8N workflow doesn't match the one in environment
Fix:
- Use the same key in both places
- Or update N8N workflow to use the key from environment
Issue 3: Production vs Development
Symptom: Works locally but not in production
Cause: Environment variable only set in development
Fix: Add the variable to production environment as well
📋 Complete Checklist
N8N_API_KEYadded to.env.local(development) or production environment- Variable has correct value (no quotes, no spaces)
- Application restarted after adding variable
- Test endpoint shows
hasN8NApiKey: true - Manual curl test returns 200 (not 500)
- Server logs show "Mission Created Webhook Received" (not "N8N_API_KEY not configured")
- N8N workflow uses same API key in header
🎯 Expected Flow After Fix
- Mission created ✅
- N8N workflow triggered ✅
- N8N creates integrations ✅
- N8N calls
/api/missions/mission-created✅ - Endpoint receives request ✅
- API key validated ✅
- IDs saved to database ✅
- Mission has integration IDs ✅
📝 Summary
Problem: 500 "Server configuration error"
Root Cause: N8N_API_KEY environment variable is not set
Solution:
- Add
N8N_API_KEYto environment variables - Use the same key value that N8N is sending in the
x-api-keyheader - Restart the application
- Test the endpoint
After Fix: The endpoint should return 200 OK and save integration IDs to the database.
Document Created: $(date) Priority: CRITICAL - Blocks integration IDs from being saved