293 lines
7.6 KiB
Markdown
293 lines
7.6 KiB
Markdown
# N8N Configuration Fix - Environment Variables & Webhook Activation
|
|
|
|
## 🔍 Problems Identified
|
|
|
|
Based on your error logs, there are **THREE critical issues**:
|
|
|
|
1. ❌ **N8N_API_KEY is not set in environment variables**
|
|
2. ❌ **404 Error**: Webhook "mission-created" is not registered (workflow not active)
|
|
3. ❌ **500 Error**: "Error in workflow" (workflow is running but failing)
|
|
|
|
---
|
|
|
|
## ✅ Fix 1: Set N8N_API_KEY Environment Variable
|
|
|
|
### Problem
|
|
```
|
|
N8N_API_KEY is not set in environment variables
|
|
```
|
|
|
|
### Solution
|
|
|
|
**Add to your `.env` or `.env.local` file**:
|
|
|
|
```env
|
|
N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4
|
|
```
|
|
|
|
**Or if using a different key**, use your actual N8N API key.
|
|
|
|
### Where to Add
|
|
|
|
1. **Local Development** (`.env.local`):
|
|
```env
|
|
N8N_API_KEY=your-actual-api-key-here
|
|
```
|
|
|
|
2. **Production** (environment variables in your hosting platform):
|
|
- Vercel: Settings → Environment Variables
|
|
- Docker: `docker-compose.yml` or `.env` file
|
|
- CapRover: App Settings → App Configs → Environment Variables
|
|
|
|
### Verify It's Set
|
|
|
|
After adding, restart your application and check logs. You should **NOT** see:
|
|
```
|
|
N8N_API_KEY is not set in environment variables
|
|
```
|
|
|
|
---
|
|
|
|
## ✅ Fix 2: Activate N8N Workflow
|
|
|
|
### Problem
|
|
```
|
|
404 Error: The requested webhook "mission-created" is not registered.
|
|
Hint: Click the 'Execute workflow' button on the canvas, then try again.
|
|
```
|
|
|
|
### Solution
|
|
|
|
**In N8N Interface**:
|
|
|
|
1. **Open your workflow** in N8N (the one with the webhook node)
|
|
2. **Click "Active" toggle** in the top right to activate the workflow
|
|
- The toggle should be **GREEN/ON** ✅
|
|
- If it's gray/off, click it to activate
|
|
|
|
3. **Verify the webhook node**:
|
|
- The webhook node should show as "Active"
|
|
- The webhook path should be: `mission-created`
|
|
- The full URL should be: `https://brain.slm-lab.net/webhook/mission-created`
|
|
|
|
### Alternative: Test Mode
|
|
|
|
If you're testing:
|
|
1. Click **"Execute Workflow"** button on the canvas
|
|
2. This activates the webhook for **one test call**
|
|
3. After the test, activate the workflow permanently
|
|
|
|
### Verify Webhook is Active
|
|
|
|
**Test the webhook URL**:
|
|
```bash
|
|
curl -X POST https://brain.slm-lab.net/webhook/mission-created \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"test": "data"}'
|
|
```
|
|
|
|
**Expected**:
|
|
- If active: Should trigger the workflow (may return error if data is invalid, but should not be 404)
|
|
- If not active: Returns 404 with message about webhook not registered
|
|
|
|
---
|
|
|
|
## ✅ Fix 3: Fix Workflow Errors (500 Error)
|
|
|
|
### Problem
|
|
```
|
|
500 Error: {"message":"Error in workflow"}
|
|
```
|
|
|
|
This means the workflow is running but encountering an error. Common causes:
|
|
|
|
### Common Issues & Fixes
|
|
|
|
#### Issue 3.1: Missing missionId in Process Mission Data
|
|
|
|
**Check**: The "Process Mission Data" node should include `missionId` in its output.
|
|
|
|
**Fix**: Ensure the node includes:
|
|
```javascript
|
|
missionId: missionData?.missionId || missionData?.body?.missionId
|
|
```
|
|
|
|
#### Issue 3.2: Incorrect URL in Save Mission To API Node
|
|
|
|
**Check**: The "Save Mission To API" node URL should be:
|
|
```
|
|
{{ $node['Process Mission Data'].json.config.MISSION_API_URL }}/api/missions/mission-created
|
|
```
|
|
|
|
**NOT**:
|
|
```
|
|
{{ $node['Process Mission Data'].json.config.MISSION_API_URL + '/mission-created' }}
|
|
```
|
|
|
|
#### Issue 3.3: Missing missionId in Save Mission To API Body
|
|
|
|
**Check**: The "Save Mission To API" node body should include:
|
|
- Parameter: `missionId`
|
|
- Value: `{{ $node['Process Mission Data'].json.missionId }}`
|
|
|
|
#### Issue 3.4: API Key Mismatch
|
|
|
|
**Check**: The API key in the "Save Mission To API" node header should match your `N8N_API_KEY` environment variable.
|
|
|
|
**Fix**: Use:
|
|
```
|
|
{{ $node['Process Mission Data'].json.config.N8N_API_KEY }}
|
|
```
|
|
|
|
### Debug Workflow Errors
|
|
|
|
1. **Check N8N Execution Logs**:
|
|
- Go to N8N → Executions
|
|
- Find the failed execution
|
|
- Click on it to see which node failed
|
|
- Check the error message
|
|
|
|
2. **Test Each Node Individually**:
|
|
- Execute the workflow step by step
|
|
- Check each node's output
|
|
- Verify data is flowing correctly
|
|
|
|
---
|
|
|
|
## 📋 Complete Checklist
|
|
|
|
### Environment Variables
|
|
- [ ] `N8N_API_KEY` is set in `.env.local` or production environment
|
|
- [ ] Value matches the API key used in N8N workflow
|
|
- [ ] Application has been restarted after adding the variable
|
|
|
|
### N8N Workflow Configuration
|
|
- [ ] Workflow is **ACTIVE** (green toggle in N8N)
|
|
- [ ] Webhook path is: `mission-created`
|
|
- [ ] Webhook URL is: `https://brain.slm-lab.net/webhook/mission-created`
|
|
- [ ] "Process Mission Data" node includes `missionId` in output
|
|
- [ ] "Save Mission To API" node URL is correct: `{{ MISSION_API_URL }}/api/missions/mission-created`
|
|
- [ ] "Save Mission To API" node includes `missionId` in body parameters
|
|
- [ ] "Save Mission To API" node includes `x-api-key` header with correct value
|
|
|
|
### Testing
|
|
- [ ] Test webhook URL returns 200 (not 404)
|
|
- [ ] Create a test mission
|
|
- [ ] Check N8N execution logs for errors
|
|
- [ ] Verify mission IDs are saved to database after creation
|
|
|
|
---
|
|
|
|
## 🧪 Step-by-Step Testing
|
|
|
|
### Step 1: Verify Environment Variable
|
|
|
|
```bash
|
|
# In your terminal (if running locally)
|
|
echo $N8N_API_KEY
|
|
|
|
# Or check in your application logs
|
|
# Should NOT see: "N8N_API_KEY is not set in environment variables"
|
|
```
|
|
|
|
### Step 2: Test Webhook is Active
|
|
|
|
```bash
|
|
curl -X POST https://brain.slm-lab.net/webhook/mission-created \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"test": "data"}'
|
|
```
|
|
|
|
**Expected Results**:
|
|
- ✅ **200/400/500 with workflow error**: Webhook is active (workflow may fail due to invalid data, but webhook is registered)
|
|
- ❌ **404 with "webhook not registered"**: Webhook is NOT active → Activate workflow in N8N
|
|
|
|
### Step 3: Test Mission Creation
|
|
|
|
1. Create a mission via your frontend
|
|
2. Check server logs - should NOT see:
|
|
- ❌ "N8N_API_KEY is not set"
|
|
- ❌ "404 webhook not registered"
|
|
3. Check N8N execution logs - should see successful execution
|
|
4. Check database - mission should have integration IDs saved
|
|
|
|
---
|
|
|
|
## 🔧 Quick Fix Commands
|
|
|
|
### Add N8N_API_KEY to .env.local
|
|
|
|
```bash
|
|
# Add to .env.local file
|
|
echo "N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4" >> .env.local
|
|
|
|
# Restart your development server
|
|
# npm run dev
|
|
# or
|
|
# yarn dev
|
|
```
|
|
|
|
### Verify Environment Variable is Loaded
|
|
|
|
Create a test endpoint to verify:
|
|
|
|
```typescript
|
|
// app/api/test-n8n-config/route.ts
|
|
import { NextResponse } from 'next/server';
|
|
|
|
export async function GET() {
|
|
return NextResponse.json({
|
|
hasN8NApiKey: !!process.env.N8N_API_KEY,
|
|
n8nWebhookUrl: process.env.N8N_WEBHOOK_URL || 'https://brain.slm-lab.net/webhook/mission-created',
|
|
missionApiUrl: process.env.NEXT_PUBLIC_API_URL
|
|
});
|
|
}
|
|
```
|
|
|
|
Then visit: `http://localhost:3000/api/test-n8n-config`
|
|
|
|
---
|
|
|
|
## 📝 Summary of Fixes
|
|
|
|
1. **Add `N8N_API_KEY` to environment variables**
|
|
- File: `.env.local` (development) or production environment
|
|
- Value: Your actual N8N API key
|
|
- Restart application after adding
|
|
|
|
2. **Activate N8N Workflow**
|
|
- Open workflow in N8N
|
|
- Click "Active" toggle (should be green/on)
|
|
- Verify webhook is registered
|
|
|
|
3. **Fix Workflow Configuration**
|
|
- Ensure "Save Mission To API" URL is correct
|
|
- Ensure `missionId` is included in body
|
|
- Check N8N execution logs for specific errors
|
|
|
|
---
|
|
|
|
## 🚨 If Still Not Working
|
|
|
|
### Check N8N Execution Logs
|
|
|
|
1. Go to N8N → Executions
|
|
2. Find the latest failed execution
|
|
3. Click on it
|
|
4. Check which node failed
|
|
5. Look at the error message
|
|
6. Fix the specific issue
|
|
|
|
### Common Additional Issues
|
|
|
|
- **Network connectivity**: N8N can't reach your API
|
|
- **CORS issues**: If calling from browser
|
|
- **Authentication**: API key mismatch
|
|
- **Data format**: Body parameters don't match expected format
|
|
|
|
---
|
|
|
|
**Document Created**: $(date)
|
|
**Priority**: CRITICAL - Blocks mission creation
|
|
|