UNPKG

n8n-nodes-smartlead-v1-8-0

Version:

🚀 SmartLead API v1.8.0 - Complete Email Marketing Automation with 20+ Operations

791 lines (790 loc) • 36.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartLead = void 0; const n8n_workflow_1 = require("n8n-workflow"); class SmartLead { constructor() { this.description = { displayName: 'SmartLead', name: 'smartLead', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Comprehensive SmartLead API integration for email marketing automation', defaults: { name: 'SmartLead', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'smartLeadApi', required: true, }, ], requestDefaults: { baseURL: 'https://server.smartlead.ai/api/v1', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }, properties: [ // Resource Selection { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: '🎯 Campaign Management', value: 'campaign', description: 'Create, manage, and control email campaigns', }, { name: '👥 Lead Management', value: 'lead', description: 'Manage leads, categories, and communication', }, { name: '📧 Email Accounts', value: 'emailAccount', description: 'Manage email accounts and warmup settings', }, { name: '📊 Analytics & Stats', value: 'analytics', description: 'Campaign statistics and performance data', }, ], default: 'campaign', required: true, }, // Campaign Operations { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['campaign'], }, }, options: [ { name: 'Get All', value: 'getAll', description: 'Retrieve all campaigns', action: 'Get all campaigns', }, { name: 'Get by ID', value: 'get', description: 'Get campaign by ID', action: 'Get a campaign', }, { name: 'Create', value: 'create', description: 'Create a new campaign', action: 'Create a campaign', }, { name: 'Update Schedule', value: 'updateSchedule', description: 'Update campaign schedule settings', action: 'Update campaign schedule', }, { name: 'Update General Settings', value: 'updateSettings', description: 'Update campaign general settings', action: 'Update campaign settings', }, { name: 'Save Sequence', value: 'saveSequence', description: 'Save campaign email sequence', action: 'Save campaign sequence', }, { name: 'Update Status', value: 'updateStatus', description: 'Start, pause, or stop campaign', action: 'Update campaign status', }, { name: 'Get Sequence', value: 'getSequence', description: 'Fetch campaign sequence by ID', action: 'Get campaign sequence', }, { name: 'Export Data', value: 'export', description: 'Export campaign data', action: 'Export campaign data', }, { name: 'Delete', value: 'delete', description: 'Delete a campaign', action: 'Delete a campaign', }, ], default: 'getAll', required: true, }, // Lead Operations { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['lead'], }, }, options: [ { name: 'Get All by Campaign', value: 'getAllByCampaign', description: 'Get all leads from a specific campaign', action: 'Get all leads by campaign', }, { name: 'Get Categories', value: 'getCategories', description: 'Fetch all lead categories', action: 'Get lead categories', }, { name: 'Get by Email', value: 'getByEmail', description: 'Find lead by email address', action: 'Get lead by email', }, { name: 'Add to Campaign', value: 'addToCampaign', description: 'Add leads to a campaign', action: 'Add leads to campaign', }, { name: 'Resume', value: 'resume', description: 'Resume lead in campaign', action: 'Resume lead', }, { name: 'Pause', value: 'pause', description: 'Pause lead in campaign', action: 'Pause lead', }, { name: 'Delete', value: 'delete', description: 'Delete lead from campaign', action: 'Delete lead', }, { name: 'Unsubscribe from Campaign', value: 'unsubscribeFromCampaign', description: 'Unsubscribe lead from specific campaign', action: 'Unsubscribe from campaign', }, { name: 'Unsubscribe from All', value: 'unsubscribeFromAll', description: 'Unsubscribe lead from all campaigns', action: 'Unsubscribe from all campaigns', }, { name: 'Add to Block List', value: 'addToBlockList', description: 'Add lead/domain to global block list', action: 'Add to block list', }, { name: 'Update', value: 'update', description: 'Update lead information', action: 'Update lead', }, { name: 'Update Category', value: 'updateCategory', description: 'Update lead category', action: 'Update lead category', }, { name: 'Get Message History', value: 'getMessageHistory', description: 'Fetch lead message history', action: 'Get message history', }, { name: 'Reply to Lead', value: 'reply', description: 'Reply to lead from master inbox', action: 'Reply to lead', }, ], default: 'getAllByCampaign', required: true, }, // Email Account Operations { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['emailAccount'], }, }, options: [ { name: 'Get All for User', value: 'getAllForUser', description: 'Get all email accounts for current user', action: 'Get all email accounts', }, { name: 'Get All for Campaign', value: 'getAllForCampaign', description: 'Get all email accounts for a campaign', action: 'Get campaign email accounts', }, { name: 'Get by ID', value: 'get', description: 'Get email account by ID', action: 'Get email account', }, { name: 'Create', value: 'create', description: 'Create new email account', action: 'Create email account', }, { name: 'Update', value: 'update', description: 'Update email account settings', action: 'Update email account', }, { name: 'Add to Campaign', value: 'addToCampaign', description: 'Add email account to campaign', action: 'Add to campaign', }, { name: 'Remove from Campaign', value: 'removeFromCampaign', description: 'Remove email account from campaign', action: 'Remove from campaign', }, { name: 'Update Warmup', value: 'updateWarmup', description: 'Update warmup settings', action: 'Update warmup settings', }, { name: 'Reconnect Failed', value: 'reconnect', description: 'Reconnect failed email accounts', action: 'Reconnect email account', }, ], default: 'getAllForUser', required: true, }, // Analytics Operations { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['analytics'], }, }, options: [ { name: 'Campaign Stats', value: 'campaignStats', description: 'Get campaign statistics by ID', action: 'Get campaign statistics', }, { name: 'Campaign Stats by Date Range', value: 'campaignStatsByDate', description: 'Get campaign stats for date range', action: 'Get campaign stats by date', }, { name: 'Warmup Stats', value: 'warmupStats', description: 'Get warmup stats by email account ID', action: 'Get warmup statistics', }, { name: 'Top Level Analytics', value: 'topLevelAnalytics', description: 'Get campaign top level analytics', action: 'Get top level analytics', }, ], default: 'campaignStats', required: true, }, // Campaign ID field (used by multiple operations) { displayName: 'Campaign ID', name: 'campaignId', type: 'string', required: true, displayOptions: { show: { resource: ['campaign'], operation: [ 'get', 'updateSchedule', 'updateSettings', 'saveSequence', 'updateStatus', 'getSequence', 'export', 'delete', ], }, }, default: '', description: 'The ID of the campaign', }, { displayName: 'Campaign ID', name: 'campaignId', type: 'string', required: true, displayOptions: { show: { resource: ['lead'], operation: [ 'getAllByCampaign', 'addToCampaign', 'resume', 'pause', 'delete', 'unsubscribeFromCampaign', 'updateCategory', 'getMessageHistory', ], }, }, default: '', description: 'The ID of the campaign', }, { displayName: 'Campaign ID', name: 'campaignId', type: 'string', required: true, displayOptions: { show: { resource: ['emailAccount'], operation: [ 'getAllForCampaign', 'addToCampaign', 'removeFromCampaign', ], }, }, default: '', description: 'The ID of the campaign', }, { displayName: 'Campaign ID', name: 'campaignId', type: 'string', required: true, displayOptions: { show: { resource: ['analytics'], operation: [ 'campaignStats', 'campaignStatsByDate', 'topLevelAnalytics', ], }, }, default: '', description: 'The ID of the campaign', }, // Email Account ID field { displayName: 'Email Account ID', name: 'emailAccountId', type: 'string', required: true, displayOptions: { show: { resource: ['emailAccount'], operation: ['get', 'update', 'updateWarmup', 'reconnect'], }, }, default: '', description: 'The ID of the email account', }, { displayName: 'Email Account ID', name: 'emailAccountId', type: 'string', required: true, displayOptions: { show: { resource: ['analytics'], operation: ['warmupStats'], }, }, default: '', description: 'The ID of the email account', }, // Lead ID field { displayName: 'Lead ID', name: 'leadId', type: 'string', required: true, displayOptions: { show: { resource: ['lead'], operation: ['resume', 'pause', 'delete', 'update'], }, }, default: '', description: 'The ID of the lead', }, // Email field for lead lookup { displayName: 'Email Address', name: 'email', type: 'string', required: true, displayOptions: { show: { resource: ['lead'], operation: [ 'getByEmail', 'unsubscribeFromCampaign', 'unsubscribeFromAll', 'reply', ], }, }, default: '', description: 'Email address of the lead', }, // Additional fields for specific operations { displayName: 'Campaign Name', name: 'campaignName', type: 'string', required: true, displayOptions: { show: { resource: ['campaign'], operation: ['create'], }, }, default: '', description: 'Name for the new campaign', }, { displayName: 'Start Date', name: 'startDate', type: 'dateTime', required: false, displayOptions: { show: { resource: ['analytics'], operation: ['campaignStatsByDate'], }, }, default: '', description: 'Start date for analytics (YYYY-MM-DD format)', }, { displayName: 'End Date', name: 'endDate', type: 'dateTime', required: false, displayOptions: { show: { resource: ['analytics'], operation: ['campaignStatsByDate'], }, }, default: '', description: 'End date for analytics (YYYY-MM-DD format)', }, { displayName: 'Additional Fields', name: 'additionalFields', type: 'collection', placeholder: 'Add Field', default: {}, options: [ { displayName: 'Limit', name: 'limit', type: 'number', default: 100, description: 'Number of results to return', }, { displayName: 'Offset', name: 'offset', type: 'number', default: 0, description: 'Number of results to skip', }, { displayName: 'Status', name: 'status', type: 'options', options: [ { name: 'Active', value: 'ACTIVE' }, { name: 'Paused', value: 'PAUSED' }, { name: 'Stopped', value: 'STOPPED' }, ], default: 'ACTIVE', description: 'Campaign status', }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); let responseData; let endpoint = ''; let method = 'GET'; let body = {}; // Campaign Management Operations if (resource === 'campaign') { const campaignId = this.getNodeParameter('campaignId', i, ''); switch (operation) { case 'getAll': endpoint = 'https://server.smartlead.ai/api/v1/campaigns'; break; case 'get': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}`; break; case 'create': endpoint = 'https://server.smartlead.ai/api/v1/campaigns'; method = 'POST'; const campaignName = this.getNodeParameter('campaignName', i); body = { name: campaignName }; break; case 'updateSchedule': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/schedule`; method = 'POST'; break; case 'updateSettings': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/settings`; method = 'POST'; break; case 'saveSequence': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/sequence`; method = 'POST'; break; case 'updateStatus': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/status`; method = 'PATCH'; break; case 'getSequence': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/sequence`; break; case 'export': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/export`; break; case 'delete': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}`; method = 'DELETE'; break; } } // Lead Management Operations else if (resource === 'lead') { const campaignId = this.getNodeParameter('campaignId', i, ''); const leadId = this.getNodeParameter('leadId', i, ''); const email = this.getNodeParameter('email', i, ''); switch (operation) { case 'getAllByCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads`; break; case 'getCategories': endpoint = 'https://server.smartlead.ai/api/v1/leads/categories'; break; case 'getByEmail': endpoint = `https://server.smartlead.ai/api/v1/leads/email/${email}`; break; case 'addToCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads`; method = 'POST'; break; case 'resume': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/${leadId}/resume`; method = 'POST'; break; case 'pause': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/${leadId}/pause`; method = 'POST'; break; case 'delete': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/${leadId}`; method = 'DELETE'; break; case 'unsubscribeFromCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/unsubscribe`; method = 'POST'; body = { email }; break; case 'unsubscribeFromAll': endpoint = 'https://server.smartlead.ai/api/v1/leads/unsubscribe-all'; method = 'POST'; body = { email }; break; case 'addToBlockList': endpoint = 'https://server.smartlead.ai/api/v1/leads/block-list'; method = 'POST'; body = { email }; break; case 'update': endpoint = `https://server.smartlead.ai/api/v1/leads/${leadId}`; method = 'POST'; break; case 'updateCategory': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/category`; method = 'POST'; break; case 'getMessageHistory': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/leads/messages`; break; case 'reply': endpoint = 'https://server.smartlead.ai/api/v1/leads/reply'; method = 'POST'; body = { email }; break; } } // Email Account Management Operations else if (resource === 'emailAccount') { const emailAccountId = this.getNodeParameter('emailAccountId', i, ''); const campaignId = this.getNodeParameter('campaignId', i, ''); switch (operation) { case 'getAllForUser': endpoint = 'https://server.smartlead.ai/api/v1/email-accounts'; break; case 'getAllForCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/email-accounts`; break; case 'get': endpoint = `https://server.smartlead.ai/api/v1/email-accounts/${emailAccountId}`; break; case 'create': endpoint = 'https://server.smartlead.ai/api/v1/email-accounts'; method = 'POST'; break; case 'update': endpoint = `https://server.smartlead.ai/api/v1/email-accounts/${emailAccountId}`; method = 'POST'; break; case 'addToCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/email-accounts`; method = 'POST'; body = { email_account_id: emailAccountId }; break; case 'removeFromCampaign': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/email-accounts/${emailAccountId}`; method = 'DELETE'; break; case 'updateWarmup': endpoint = `https://server.smartlead.ai/api/v1/email-accounts/${emailAccountId}/warmup`; method = 'POST'; break; case 'reconnect': endpoint = `https://server.smartlead.ai/api/v1/email-accounts/${emailAccountId}/reconnect`; method = 'POST'; break; } } // Analytics Operations else if (resource === 'analytics') { const campaignId = this.getNodeParameter('campaignId', i, ''); const emailAccountId = this.getNodeParameter('emailAccountId', i, ''); switch (operation) { case 'campaignStats': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/stats`; break; case 'campaignStatsByDate': const startDate = this.getNodeParameter('startDate', i, ''); const endDate = this.getNodeParameter('endDate', i, ''); endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/stats/date-range`; if (startDate) endpoint += `?start_date=${startDate}`; if (endDate) endpoint += `${startDate ? '&' : '?'}end_date=${endDate}`; break; case 'warmupStats': endpoint = `https://server.smartlead.ai/api/v1/email-accounts/${emailAccountId}/warmup/stats`; break; case 'topLevelAnalytics': endpoint = `https://server.smartlead.ai/api/v1/campaigns/${campaignId}/analytics`; break; } } // Execute the API request responseData = await this.helpers.requestWithAuthentication.call(this, 'smartLeadApi', { method, url: endpoint, json: true, ...(Object.keys(body).length > 0 && { body }), }); // Extract data if it's wrapped in a 'data' property const dataToReturn = (responseData === null || responseData === void 0 ? void 0 : responseData.data) || responseData; const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(dataToReturn), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, }); } } return [returnData]; } } exports.SmartLead = SmartLead;