n8n Workflow Integration
n8n is the workflow automation backbone of AltSportsLeagues.ai, providing intelligent orchestration between AI services, databases, external APIs, and user interfaces. This comprehensive workflow system enables complex business processes to be automated with minimal technical overhead.
Architecture Overview
Workflow Categories
Atomic Workflows
Single-purpose workflows that perform one operation exceptionally well.
Benefits:
- Testability: Easy to unit test and validate
- Reusability: Can be composed into complex workflows
- Maintainability: Clear responsibility boundaries
- Performance: Optimized for specific operations
Examples:
email-classification.json- Classifies incoming emailspartnership-scoring.json- Scores partnership opportunitiesdocument-extraction.json- Extracts data from PDFscontract-generation.json- Creates contract templates
Composite Workflows
Orchestrates multiple atomic workflows to accomplish complex business processes.
Benefits:
- Modularity: Leverages existing atomic workflows
- Flexibility: Easy to modify without breaking components
- Error Handling: Granular error handling at each step
- Monitoring: Clear visibility into process progress
Examples:
email-to-contract-pipeline.json- Full partnership acquisition pipelineleague-onboarding-workflow.json- Complete league onboarding processmonthly-reporting-pipeline.json- Automated business intelligence reports
Integration Workflows
Manages connections to external systems and data synchronization.
Benefits:
- Centralization: Single point of integration management
- Consistency: Standardized error handling and logging
- Maintainability: Easy updates when APIs change
- Monitoring: Clear visibility into system health
Examples:
jira-sync.json- Synchronizes partnership data to Jiragmail-integration.json- Processes incoming emailsdatabase-sync.json- Updates Supabase/Firestore recordsnotification-system.json- Sends alerts and notifications
Core Workflows
Chat Interface Integration
See: Chat Interface Integration - Complete guide to the modern chat interface that connects users with n8n automation workflows using ChatKit styling and ShadCN components.
Partnership Acquisition Pipeline
The complete automated pipeline from initial email inquiry to contract signing:
Workflow Components:
-
Email Classification (
email-classification.json)- Uses AI to categorize incoming emails
- Identifies partnership inquiries vs. support tickets
- Routes to appropriate processing workflows
-
Document Processing (
pdf-extraction-workflow.json)- OCR processing of league questionnaires
- Structured data extraction
- Quality validation and error correction
-
Partnership Scoring (
partnership-scoring.json)- Multi-dimensional scoring algorithm
- Tier classification (Premium/Growth/Entry)
- Risk assessment and opportunity evaluation
-
Jira Integration (
jira-ticket-creation.json)- Creates structured Jira tickets
- Includes all extracted and scored data
- Enables manual review and approval workflows
-
Contract Generation (
contract-generator.json)- Dynamic contract creation based on tier
- Customizable terms and conditions
- PDF generation and delivery
League Onboarding System
Automated league onboarding from submission to activation:
Key Workflows:
league-submission-processor.json- Processes league applicationsadmin-notification-system.json- Alerts administrators of new submissionsapproval-workflow.json- Handles approval/rejection processesactivation-system.json- Activates approved leagues
Email Intelligence Pipeline
AI-powered email processing and response generation:
Capabilities:
- Smart Classification: Automatically categorizes emails by intent
- Intelligent Extraction: Pulls relevant data from email content
- Automated Responses: Generates contextually appropriate replies
- Follow-up Management: Tracks conversation threads and deadlines
Technical Architecture
Workflow Design Patterns
Error Handling Pattern
// n8n error handling with retry logic
{
"nodes": [
{
"parameters": {
"url": "https://api.example.com/process",
"method": "POST",
"options": {
"retry": {
"maxRetries": 3,
"retryInterval": 1000,
"exponentialBackoff": true
}
}
},
"continueOnFail": true,
"onError": "continue"
}
]
}Circuit Breaker Pattern
// Circuit breaker for external API calls
{
"parameters": {
"code": `
const circuitBreaker = context.getGlobalState('circuit_breaker') || {
failures: 0,
lastFailure: null,
state: 'closed'
};
if (circuitBreaker.state === 'open') {
if (Date.now() - circuitBreaker.lastFailure > 60000) {
circuitBreaker.state = 'half-open';
} else {
throw new Error('Circuit breaker is open');
}
}
try {
// API call logic
circuitBreaker.failures = 0;
circuitBreaker.state = 'closed';
} catch (error) {
circuitBreaker.failures++;
circuitBreaker.lastFailure = Date.now();
if (circuitBreaker.failures >= 3) {
circuitBreaker.state = 'open';
}
throw error;
}
context.setGlobalState('circuit_breaker', circuitBreaker);
`
}
}Data Flow Architecture
Schema-Based Processing
All workflows use standardized data schemas for consistency:
// Schema validation in n8n
{
"parameters": {
"code": `
const { validateLeagueQuestionnaire } = require('./data_layer/shared');
const inputData = $json;
const validation = validateLeagueQuestionnaire(inputData);
if (!validation.valid) {
throw new Error(\`Validation failed: \${validation.errors.join(', ')}\`);
}
return validation.data;
`
}
}Idempotency Handling
Ensures workflows can be safely retried:
// Idempotency key generation
{
"parameters": {
"code": `
const generateIdempotencyKey = (data) => {
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(data));
return hash.digest('hex');
};
const key = generateIdempotencyKey($json);
const existingResult = await checkIdempotencyStore(key);
if (existingResult) {
return existingResult;
}
// Process the request
const result = await processRequest($json);
// Store result for idempotency
await storeIdempotencyResult(key, result);
return result;
`
}
}Integration Points
AI Intelligence Service
- Endpoint:
https://intelligence-api-*.run.app - Capabilities: Email classification, document processing, partnership scoring
- Authentication: API key with rate limiting
- Error Handling: Comprehensive retry logic with exponential backoff
Database Integrations
- Supabase: Primary application database
- Firestore: Real-time data and caching
- PostgreSQL: Complex queries and reporting
- Redis: Session storage and caching
External APIs
- Jira/Atlassian: Project management and ticketing
- Gmail API: Email processing and automation
- Google Drive: Document storage and collaboration
- Slack: Team notifications and alerts
Monitoring and Observability
Workflow Metrics
- Execution Time: Average and percentile performance
- Success Rate: Percentage of successful executions
- Error Patterns: Common failure modes and root causes
- Throughput: Workflows processed per minute/hour
Alerting System
- Failure Alerts: Immediate notification of workflow failures
- Performance Alerts: Threshold-based performance monitoring
- Capacity Alerts: Queue depth and resource utilization warnings
- Business Alerts: Partnership pipeline and revenue metric monitoring
Logging Architecture
- Structured Logging: JSON-formatted logs with context
- Log Aggregation: Centralized logging with search capabilities
- Trace Correlation: End-to-end request tracing across workflows
- Audit Trails: Complete history of all workflow executions
Deployment and Scaling
Environment Strategy
- Development: Local n8n instance with hot reloading
- Staging: Cloud deployment with automated testing
- Production: Multi-region deployment with failover
Scaling Patterns
- Horizontal Scaling: Multiple n8n instances behind load balancer
- Queue-Based Processing: Redis-backed queues for high throughput
- Database Sharding: Partitioned databases for performance
- CDN Integration: Global distribution of static assets
Security and Compliance
Authentication & Authorization
- OAuth 2.0: Secure API authentication
- JWT Tokens: Stateless authentication for microservices
- Role-Based Access: Granular permissions for workflow operations
- API Keys: Service-to-service authentication
Data Protection
- Encryption at Rest: Database-level encryption
- Encryption in Transit: TLS 1.3 for all communications
- Data Sanitization: Input validation and sanitization
- Audit Logging: Complete audit trail of all operations
Development Workflow
Workflow Development Process
- Design: Create workflow specification and requirements
- Implement: Build using n8n visual editor or JSON
- Test: Unit test individual components and integration tests
- Deploy: Automated deployment with rollback capabilities
- Monitor: Real-time monitoring and alerting
Code Organization
n8n_workflows/
βββ workflows/ # Production workflows
βββ config/ # Environment configurations
βββ scripts/ # Deployment and utility scripts
βββ tests/ # Test suites and fixtures
βββ docs/ # Documentation and guides
βββ shared-code/ # Reusable JavaScript functionsPerformance Optimization
Caching Strategies
- Workflow Results: Cache expensive operation results
- API Responses: Cache external API calls with TTL
- Database Queries: Query result caching with invalidation
- Static Assets: CDN caching for improved performance
Optimization Techniques
- Batch Processing: Group similar operations for efficiency
- Async Operations: Non-blocking I/O for better throughput
- Memory Management: Efficient memory usage for large datasets
- Connection Pooling: Reusable database connections
Troubleshooting Guide
Common Issues
Workflow Fails to Start
Symptoms: Workflow doesn't trigger on schedule Solutions:
- Check n8n service status
- Verify cron expressions
- Review webhook endpoints
- Check authentication credentials
API Call Timeouts
Symptoms: Workflows fail with timeout errors Solutions:
- Increase timeout values
- Implement retry logic
- Check API rate limits
- Optimize API payload size
Database Connection Issues
Symptoms: Database operation failures Solutions:
- Verify connection strings
- Check database server status
- Review connection pool settings
- Implement connection retry logic
Debugging Tools
- Workflow Logs: Detailed execution logs with timestamps
- Execution History: Complete history of all workflow runs
- Performance Metrics: Real-time performance monitoring
- Error Tracking: Centralized error collection and analysis
Quick Reference
Workflow Templates
| Template | Purpose | Key Features |
|---|---|---|
| Email Processing | Process incoming emails | AI classification, data extraction, automated responses |
| Document Processing | Handle PDF uploads | OCR, validation, structured data extraction |
| Jira Integration | Sync with Atlassian | Ticket creation, status updates, comment synchronization |
| Database Operations | Data CRUD operations | Batch processing, transaction management, error recovery |
| Notification System | Send alerts | Email, Slack, SMS notifications with templating |
Key Scripts
| Script | Purpose | Usage |
|---|---|---|
bulk_upload_workflows.py | Upload multiple workflows | ./scripts/bulk_upload_workflows.py --env prod |
evaluation_framework.py | Test workflow performance | ./scripts/evaluation_framework.py --workflow email-pipeline |
sync_to_n8n_cloud.py | Deploy to n8n cloud | ./scripts/sync_to_n8n_cloud.py --project alt-sports |
Environment Variables
| Variable | Purpose | Example |
|---|---|---|
N8N_ENCRYPTION_KEY | Workflow encryption | n8n_encryption_key_12345 |
GOOGLE_SERVICE_ACCOUNT | Google API access | Path to service account JSON |
DATABASE_URL | Database connection | postgresql://user:pass@host:5432/db |
OPENAI_API_KEY | AI service access | sk-... |
This n8n workflow system provides the automation backbone for AltSportsLeagues.ai, enabling complex business processes to be executed reliably and efficiently.