N8N Workflows
N8N Workflows
Section titled “N8N Workflows”This guide shows you how to integrate UniCraft with N8N workflows to automate AI-powered processes.
Overview
Section titled “Overview”N8N is a powerful workflow automation tool that can be integrated with UniCraft to create sophisticated AI-powered automations. This integration allows you to:
- Automate AI model selection and routing
- Process data through AI models in workflows
- Create intelligent decision-making processes
- Build AI-powered business processes
Prerequisites
Section titled “Prerequisites”Before setting up N8N workflows with UniCraft:
- N8N Instance: Have N8N running (self-hosted or cloud)
- UniCraft Account: Active UniCraft account with API access
- API Keys: UniCraft API key and configured providers
- N8N Custom Nodes: Install UniCraft custom nodes (if available)
Basic Setup
Section titled “Basic Setup”1. Install UniCraft Custom Nodes
Section titled “1. Install UniCraft Custom Nodes”If UniCraft provides custom N8N nodes:
# Install via N8N CLIn8n install @cloudcraftlabs/unicraft-nodes
# Or install via N8N interface# Go to Settings > Community Nodes > Install# Add: @cloudcraftlabs/unicraft-nodes2. Configure UniCraft Credentials
Section titled “2. Configure UniCraft Credentials”Set up UniCraft credentials in N8N:
- Go to Credentials in N8N
- Click Add Credential
- Select UniCraft API
- Enter your UniCraft API key
- Test the connection
3. Basic Workflow Example
Section titled “3. Basic Workflow Example”Create a simple workflow that uses UniCraft:
{ "name": "UniCraft Chat Completion", "nodes": [ { "name": "Start", "type": "n8n-nodes-base.start", "position": [240, 300] }, { "name": "UniCraft Chat", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "position": [460, 300], "parameters": { "messages": [ { "role": "user", "content": "{{ $json.prompt }}" } ], "model": "auto", "max_tokens": 100 } }, { "name": "Process Response", "type": "n8n-nodes-base.function", "position": [680, 300], "parameters": { "functionCode": "return { response: $input.first().json.choices[0].message.content };" } } ], "connections": { "Start": { "main": [["UniCraft Chat"]] }, "UniCraft Chat": { "main": [["Process Response"]] } }}Advanced Workflow Examples
Section titled “Advanced Workflow Examples”1. Content Generation Workflow
Section titled “1. Content Generation Workflow”Automate content generation with quality checks:
{ "name": "Content Generation Pipeline", "nodes": [ { "name": "Content Request", "type": "n8n-nodes-base.webhook", "parameters": { "path": "content-request", "httpMethod": "POST" } }, { "name": "Generate Content", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "You are a professional content writer." }, { "role": "user", "content": "Write a blog post about: {{ $json.topic }}" } ], "model": "gpt-4", "max_tokens": 1000, "temperature": 0.7 } }, { "name": "Quality Check", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "You are a content quality reviewer." }, { "role": "user", "content": "Rate the quality of this content (1-10): {{ $json.choices[0].message.content }}" } ], "model": "gpt-3.5-turbo", "max_tokens": 50 } }, { "name": "Conditional Processing", "type": "n8n-nodes-base.if", "parameters": { "conditions": { "number": [ { "value1": "{{ $json.choices[0].message.content }}", "operation": "larger", "value2": 7 } ] } } }, { "name": "Save Content", "type": "n8n-nodes-base.googleSheets", "parameters": { "operation": "append", "sheetId": "your-sheet-id", "columns": { "mappingMode": "defineBelow", "value": { "topic": "={{ $('Content Request').item.json.topic }}", "content": "={{ $('Generate Content').item.json.choices[0].message.content }}", "quality_score": "={{ $('Quality Check').item.json.choices[0].message.content }}" } } } } ]}2. Customer Support Automation
Section titled “2. Customer Support Automation”Automate customer support with AI:
{ "name": "Customer Support Bot", "nodes": [ { "name": "Support Request", "type": "n8n-nodes-base.webhook", "parameters": { "path": "support-request", "httpMethod": "POST" } }, { "name": "Classify Request", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "Classify this customer support request into categories: billing, technical, general, complaint" }, { "role": "user", "content": "{{ $json.message }}" } ], "model": "gpt-3.5-turbo", "max_tokens": 50 } }, { "name": "Route by Category", "type": "n8n-nodes-base.switch", "parameters": { "rules": { "rules": [ { "value1": "={{ $json.choices[0].message.content }}", "operation": "contains", "value2": "billing" }, { "value1": "={{ $json.choices[0].message.content }}", "operation": "contains", "value2": "technical" }, { "value1": "={{ $json.choices[0].message.content }}", "operation": "contains", "value2": "complaint" } ] } } }, { "name": "Generate Response", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "You are a helpful customer support agent. Provide a professional and helpful response." }, { "role": "user", "content": "Customer message: {{ $('Support Request').item.json.message }}\nCategory: {{ $('Classify Request').item.json.choices[0].message.content }}" } ], "model": "gpt-3.5-turbo", "max_tokens": 200 } }, { "name": "Send Response", "type": "n8n-nodes-base.emailSend", "parameters": { "toEmail": "={{ $('Support Request').item.json.email }}", "subject": "Re: {{ $('Support Request').item.json.subject }}", "message": "={{ $json.choices[0].message.content }}" } } ]}3. Data Analysis Workflow
Section titled “3. Data Analysis Workflow”Automate data analysis with AI:
{ "name": "Data Analysis Pipeline", "nodes": [ { "name": "Data Source", "type": "n8n-nodes-base.googleSheets", "parameters": { "operation": "read", "sheetId": "your-data-sheet-id" } }, { "name": "Analyze Data", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "You are a data analyst. Analyze the provided data and provide insights." }, { "role": "user", "content": "Analyze this data: {{ JSON.stringify($json) }}" } ], "model": "gpt-4", "max_tokens": 500 } }, { "name": "Generate Report", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "system", "content": "You are a business analyst. Create a professional report based on the analysis." }, { "role": "user", "content": "Create a report based on this analysis: {{ $json.choices[0].message.content }}" } ], "model": "gpt-4", "max_tokens": 1000 } }, { "name": "Save Report", "type": "n8n-nodes-base.googleDocs", "parameters": { "operation": "create", "title": "Data Analysis Report - {{ new Date().toISOString().split('T')[0] }}", "content": "={{ $json.choices[0].message.content }}" } } ]}Using HTTP Request Node
Section titled “Using HTTP Request Node”If custom nodes aren’t available, use the HTTP Request node:
Basic HTTP Request to UniCraft
Section titled “Basic HTTP Request to UniCraft”{ "name": "UniCraft HTTP Request", "type": "n8n-nodes-base.httpRequest", "parameters": { "url": "https://api.unicraft.com/v1/chat/completions", "method": "POST", "headers": { "Authorization": "Bearer {{ $credentials.unicraftApi.apiKey }}", "Content-Type": "application/json" }, "body": { "messages": [ { "role": "user", "content": "{{ $json.prompt }}" } ], "model": "auto", "max_tokens": 100 } }}Advanced HTTP Request with Error Handling
Section titled “Advanced HTTP Request with Error Handling”{ "name": "UniCraft with Error Handling", "type": "n8n-nodes-base.httpRequest", "parameters": { "url": "https://api.unicraft.com/v1/chat/completions", "method": "POST", "headers": { "Authorization": "Bearer {{ $credentials.unicraftApi.apiKey }}", "Content-Type": "application/json" }, "body": { "messages": [ { "role": "user", "content": "{{ $json.prompt }}" } ], "model": "auto", "max_tokens": 100 }, "options": { "retry": { "enabled": true, "maxRetries": 3 }, "timeout": 30000 } }}Workflow Best Practices
Section titled “Workflow Best Practices”1. Error Handling
Section titled “1. Error Handling”Always implement error handling in your workflows:
{ "name": "Error Handler", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "if ($input.first().json.error) { throw new Error($input.first().json.error.message); } return $input.all();" }}2. Rate Limiting
Section titled “2. Rate Limiting”Implement rate limiting to avoid hitting API limits:
{ "name": "Rate Limiter", "type": "n8n-nodes-base.wait", "parameters": { "amount": 1, "unit": "seconds" }}3. Data Validation
Section titled “3. Data Validation”Validate input data before processing:
{ "name": "Validate Input", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "const input = $input.first().json; if (!input.prompt || input.prompt.length < 10) { throw new Error('Prompt too short'); } return $input.all();" }}4. Logging and Monitoring
Section titled “4. Logging and Monitoring”Add logging for debugging and monitoring:
{ "name": "Log Response", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "console.log('UniCraft Response:', JSON.stringify($input.first().json, null, 2)); return $input.all();" }}Testing Workflows
Section titled “Testing Workflows”1. Manual Testing
Section titled “1. Manual Testing”Test workflows manually using the N8N interface:
- Click Execute Workflow
- Provide test data
- Monitor execution
- Check results
2. Automated Testing
Section titled “2. Automated Testing”Set up automated testing for your workflows:
{ "name": "Test Workflow", "nodes": [ { "name": "Test Data", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "return [{ json: { prompt: 'Test prompt', expected: 'Test response' } }];" } }, { "name": "UniCraft Request", "type": "@cloudcraftlabs/unicraft-nodes.chatCompletion", "parameters": { "messages": [ { "role": "user", "content": "{{ $json.prompt }}" } ], "model": "gpt-3.5-turbo", "max_tokens": 50 } }, { "name": "Validate Response", "type": "n8n-nodes-base.function", "parameters": { "functionCode": "const response = $input.first().json.choices[0].message.content; if (!response || response.length < 10) { throw new Error('Invalid response'); } return $input.all();" } } ]}Deployment and Production
Section titled “Deployment and Production”1. Environment Configuration
Section titled “1. Environment Configuration”Set up different environments for development and production:
{ "development": { "unicraft_api_url": "https://api-dev.unicraft.com", "model": "gpt-3.5-turbo", "max_tokens": 100 }, "production": { "unicraft_api_url": "https://api.unicraft.com", "model": "auto", "max_tokens": 500 }}2. Security Considerations
Section titled “2. Security Considerations”- Use environment variables for API keys
- Implement proper authentication
- Validate all inputs
- Monitor for unusual activity
3. Performance Optimization
Section titled “3. Performance Optimization”- Use appropriate models for tasks
- Implement caching where possible
- Batch requests when appropriate
- Monitor response times
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”- Authentication Errors: Check API key configuration
- Rate Limiting: Implement proper rate limiting
- Model Selection: Ensure models are available
- Data Format: Validate input data format
Debug Tips
Section titled “Debug Tips”- Use the N8N execution log
- Add logging nodes to workflows
- Test with simple requests first
- Check UniCraft API documentation
Next Steps
Section titled “Next Steps”After setting up N8N workflows with UniCraft:
- Test Thoroughly: Test all workflows with various inputs
- Monitor Performance: Set up monitoring for workflow execution
- Optimize Costs: Monitor and optimize AI usage costs
- Scale Gradually: Start with simple workflows and add complexity
- Document Processes: Document your workflows for team use