246 lines
5.6 KiB
Markdown
246 lines
5.6 KiB
Markdown
# 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):
|
|
|
|
```typescript
|
|
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:
|
|
|
|
```env
|
|
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
|
|
```
|
|
|
|
**Then restart your development server**:
|
|
```bash
|
|
# Stop the server (Ctrl+C)
|
|
# Restart
|
|
npm run dev
|
|
# or
|
|
yarn dev
|
|
```
|
|
|
|
#### Option B: Production Server
|
|
|
|
**If using Docker**:
|
|
Add to `docker-compose.yml`:
|
|
```yaml
|
|
services:
|
|
app:
|
|
environment:
|
|
- N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
|
|
```
|
|
|
|
**Or in `.env` file** (if using docker-compose with env_file):
|
|
```env
|
|
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
|
|
```
|
|
|
|
**If using CapRover**:
|
|
1. Go to App Settings
|
|
2. App Configs → Environment Variables
|
|
3. Add: `N8N_API_KEY` = `LwgeE1ntADD20OuWC88S3pR0EaO7FtO4`
|
|
4. Save and restart the app
|
|
|
|
**If using Vercel**:
|
|
1. Go to Project Settings
|
|
2. Environment Variables
|
|
3. Add: `N8N_API_KEY` = `LwgeE1ntADD20OuWC88S3pR0EaO7FtO4`
|
|
4. Redeploy
|
|
|
|
**If using other hosting**:
|
|
- Add `N8N_API_KEY` to your hosting platform's environment variables
|
|
- Restart/redeploy the application
|
|
|
|
---
|
|
|
|
## 🧪 Verification
|
|
|
|
### Step 1: Check if Variable is Set
|
|
|
|
**Create a test endpoint** to verify:
|
|
|
|
```typescript
|
|
// 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**:
|
|
```json
|
|
{
|
|
"hasN8NApiKey": true,
|
|
"n8nApiKeyLength": 32
|
|
}
|
|
```
|
|
|
|
### Step 2: Test the Endpoint Manually
|
|
|
|
**After adding the variable and restarting**:
|
|
|
|
```bash
|
|
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_KEY` is 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**:
|
|
1. **Wrong file**: Using `.env` instead of `.env.local` (Next.js prefers `.env.local`)
|
|
2. **Not restarted**: Server needs restart after adding env variable
|
|
3. **Wrong location**: `.env.local` must be in project root (same level as `package.json`)
|
|
4. **Syntax error**: Check for quotes, spaces, or special characters
|
|
|
|
**Fix**:
|
|
```env
|
|
# 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_KEY` added 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
|
|
|
|
1. **Mission created** ✅
|
|
2. **N8N workflow triggered** ✅
|
|
3. **N8N creates integrations** ✅
|
|
4. **N8N calls `/api/missions/mission-created`** ✅
|
|
5. **Endpoint receives request** ✅
|
|
6. **API key validated** ✅
|
|
7. **IDs saved to database** ✅
|
|
8. **Mission has integration IDs** ✅
|
|
|
|
---
|
|
|
|
## 📝 Summary
|
|
|
|
**Problem**: 500 "Server configuration error"
|
|
|
|
**Root Cause**: `N8N_API_KEY` environment variable is not set
|
|
|
|
**Solution**:
|
|
1. Add `N8N_API_KEY` to environment variables
|
|
2. Use the same key value that N8N is sending in the `x-api-key` header
|
|
3. Restart the application
|
|
4. 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
|
|
|