N8n Integration

N8N Workflow Integration

Overview

The NextAI starter kit includes seamless integration with n8n, a powerful workflow automation platform. This integration allows you to create complex AI workflows that can be triggered from your application, extending the capabilities of your chatbot beyond simple text interactions.

Setup

Prerequisites

  • An n8n instance (cloud or self-hosted)
  • A webhook URL from your n8n instance

Configuration

To set up the n8n integration, add your webhook URL to the .env file:

N8N_WEBHOOK_URL="https://your-n8n-instance.app.n8n.cloud/webhook/path"

Example: Random Word Generator

The starter kit includes an example n8n workflow integration that generates random words. This is implemented in lib/ai/tools/n8n-workflow-example.ts:

export async function getRandomWord() {
  // Makes a request to your n8n instance
  // Returns a random word
}

This tool is automatically available in the chat interface when using a model with reasoning capabilities.

API

You can also just call your n8n workflow in an api route.

api/do-something/route.ts
// Get the data from the request body
const data = await req.json();
 
// Configure the n8n webhook URL
const n8nUrl = process.env.N8N_WEBHOOK_URL || 'https://your-n8n-instance.com/webhook/path';
 
// Make the request to n8n
const response = await fetch(n8nUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        // Add any authentication headers if needed
        // 'Authorization': `Bearer ${process.env.N8N_API_KEY}`,
    },
    body: JSON.stringify(data),
});
 
// Handle the response from n8n
if (!response.ok) {
    throw new Error(`n8n responded with status: ${response.status}`);
}
 
const responseData = await response.json();
 
// Return the response from n8n
return NextResponse.json(responseData, { status: 200 });

Creating Custom Workflows

In n8n

  1. Create a new workflow in your n8n instance
  2. Add a webhook node as the trigger
  3. Configure your processing logic
  4. Deploy the workflow and copy the webhook URL

In Your Application

  1. Create a new tool file in lib/ai/tools/
  2. Implement the logic to call your n8n webhook
  3. Add the tool to the experimental_activeTools array in the chat configuration

Advanced Use Cases

Data Processing

Use n8n to process and transform data before or after AI interactions:

  • Clean and format input data
  • Enrich AI responses with additional information
  • Store interaction data in external systems

Multi-step Workflows

Create complex workflows that involve multiple steps and services:

  • Trigger email notifications based on chat content
  • Update CRM records with information from conversations
  • Generate and process documents based on user requests

Integration with External Services

Connect your AI chatbot with other services through n8n:

  • Fetch data from APIs
  • Update databases
  • Trigger actions in other applications

Best Practices

  1. Keep workflows modular and focused on specific tasks
  2. Implement proper error handling in both n8n workflows and your application code
  3. Use environment variables for sensitive configuration
  4. Test workflows thoroughly before deployment
  5. Monitor workflow execution and performance

Security Considerations

  • Protect your webhook URLs with authentication when possible
  • Validate input data before processing
  • Limit the scope of actions that can be performed through workflows
  • Implement rate limiting to prevent abuse

For more examples and detailed implementation guides, check out the other sections of the documentation.