### Install sendwithus Node.js Client Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Install the sendwithus Node.js client library using npm. ```bash npm install sendwithus ``` -------------------------------- ### Install Dependencies Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Installs the necessary project dependencies using npm. Run this command in your project directory. ```bash npm install ``` -------------------------------- ### Run Unit Tests Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Executes the project's unit tests using npm. Ensure dependencies are installed first. ```bash npm test ``` -------------------------------- ### Start Customer on a Drip Campaign Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Activates a customer on a specified drip campaign, optionally providing email data. ```APIDOC ## Start Customer on a Drip Campaign ### Description Activates a customer on a specified drip campaign, optionally providing email data. ### Method ```javascript api.dripCampaignActivate(drip_campaign_id, data, callback) ``` ### Parameters #### Path Parameters - **drip_campaign_id** (string) - Required - The ID of the drip campaign. #### Request Body - **recipient** (object) - Required - Information about the recipient. - **address** (string) - Required - The email address of the recipient. - **name** (string) - Optional - The name of the recipient. - **email_data** (object) - Optional - Data to be used in the drip campaign emails. ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); var data = { recipient: { address: 'RECIPIENT_ADDRESS', name: 'RECIPIENT_NAME' }, email_data: { country: 'Latveria' } } api.dripCampaignActivate('DRIP_CAMPAIGN_ID', data, callback); ``` ``` -------------------------------- ### api.dripCampaignActivate(dripCampaignId, data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Starts a customer on a drip campaign sequence. The campaign must be active; attempting to activate on a disabled campaign returns a 400 error. ```APIDOC ## `api.dripCampaignActivate(dripCampaignId, data, callback)` — Enroll a Customer in a Drip Campaign Starts a customer on a drip campaign sequence. The campaign must be active; attempting to activate on a disabled campaign returns a 400 error. ### Parameters #### Path Parameters - **dripCampaignId** (string) - Required - The ID of the drip campaign. #### Request Body - **recipient** (object) - Required - The recipient's details. - **address** (string) - Required - The recipient's email address. - **name** (string) - Optional - The recipient's name. - **email_data** (object) - Optional - Data to be used in the drip campaign emails. - **country** (string) - **plan** (string) ### Callback Parameters - **err** (object) - An error object if the activation fails. - **statusCode** (number) - The HTTP status code of the error. - **result** (object) - An object indicating the success of the enrollment. - **success** (boolean) ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignActivate('dc_CAMPAIGN_ID', { recipient: { address: 'customer@example.com', name: 'John Customer' }, email_data: { country: 'Canada', plan: 'trial' } }, (err, result) => { if (err) { // err.statusCode === 400 if campaign is disabled or doesn't exist console.error('Activation failed:', err.message, err.statusCode); return; } console.log('Enrolled:', result.success); // true }); ``` ``` -------------------------------- ### Sample Rendered Email Response Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Example of a successful response when rendering an email, showing the rendered subject, HTML, and text content. ```json { "success": true, "status": "OK", "template": { "id": "ver_r4nd0ml3tt3rsv15h4l0l", "name": "Template name", "version_name": "Template version name", "locale": "en-US" }, "subject": "RENDERED SUBJECT WITH DATA", "html": "RENDERED HTML BODY WITH DATA", "text": "RENDERED TEXT BODY WITH DATA" } ``` -------------------------------- ### Get Drip Campaign Details Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Fetches the complete details for a specific drip campaign, including its steps and configuration. Requires the drip campaign ID. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignDetails('dc_CAMPAIGN_ID', (err, result) => { if (err) { console.error('Failed to fetch campaign:', err.message); return; } console.log('Campaign:', result.name); console.log('Steps:', result.drip_steps.length); }); ``` -------------------------------- ### Start Customer on a Drip Campaign Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Activates a customer on a specified drip campaign. You can also include custom email data. Requires the drip campaign ID and recipient details. ```javascript var api = require('sendwithus')('API_KEY'); var data = { recipient: { address: 'RECIPIENT_ADDRESS', name: 'RECIPIENT_NAME' }, email_data: { country: 'Latveria' } } api.dripCampaignActivate('DRIP_CAMPAIGN_ID', data, callback); ``` -------------------------------- ### API Response - Success Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Example of a successful API response, indicating the status and success flag. ```javascript > response.status; OK > response.success; True > response.receipt_id; 'numeric-receipt-id' ``` -------------------------------- ### Enroll Customer in Drip Campaign Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Starts a customer on a drip campaign sequence. The campaign must be active; attempting to activate on a disabled campaign will result in a 400 error. You can pass custom email data for personalization. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignActivate('dc_CAMPAIGN_ID', { recipient: { address: 'customer@example.com', name: 'John Customer' }, email_data: { country: 'Canada', plan: 'trial' } }, (err, result) => { if (err) { // err.statusCode === 400 if campaign is disabled or doesn't exist console.error('Activation failed:', err.message, err.statusCode); return; } console.log('Enrolled:', result.success); // true }); ``` -------------------------------- ### Initialization Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Require the package and instantiate the Sendwithus client with your API key. Debug mode can be enabled by passing `true` as the second argument. Event listeners for requests and responses can be attached. ```APIDOC const sendwithus = require('sendwithus'); // Basic initialization const api = sendwithus('YOUR_API_KEY'); // With debug mode enabled const apiDebug = sendwithus('YOUR_API_KEY', true); // Attach global event listeners api.on('request', (method, url, headers, body) => { console.log(`→ ${method} ${url}`); }); api.on('response', (statusCode, body, response) => { console.log(`← ${statusCode}`, body); }); ``` -------------------------------- ### Initialize Sendwithus Node.js Client Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Require the package and instantiate with your API key. Pass `true` to enable debug logging. Global event listeners for 'request' and 'response' can be attached. ```javascript const sendwithus = require('sendwithus'); // Basic initialization const api = sendwithus('YOUR_API_KEY'); // With debug mode enabled const apiDebug = sendwithus('YOUR_API_KEY', true); // Prints: SENDWITHUS: Debug enabled // Prints each built URL and request/response payloads // Attach global event listeners api.on('request', (method, url, headers, body) => { console.log(`→ ${method} ${url}`); }); api.on('response', (statusCode, body, response) => { console.log(`← ${statusCode}`, body); }); ``` -------------------------------- ### api.dripCampaignDetails(dripCampaignId, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Fetches full details of a single drip campaign, including its steps and configuration. ```APIDOC ## `api.dripCampaignDetails(dripCampaignId, callback)` — Get Drip Campaign Details Fetches full details of a single drip campaign, including its steps and configuration. ### Parameters #### Path Parameters - **dripCampaignId** (string) - Required - The ID of the drip campaign. ### Callback Parameters - **err** (object) - An error object if the request fails. - **result** (object) - An object containing the drip campaign details. - **name** (string) - **drip_steps** (array) ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignDetails('dc_CAMPAIGN_ID', (err, result) => { if (err) { console.error('Failed to fetch campaign:', err.message); return; } console.log('Campaign:', result.name); console.log('Steps:', result.drip_steps.length); }); ``` ``` -------------------------------- ### api.dripCampaignList(callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Returns all drip campaigns configured on the account. ```APIDOC ## `api.dripCampaignList(callback)` — List Drip Campaigns Returns all drip campaigns configured on the account. ### Callback Parameters - **err** (object) - An error object if the request fails. - **result** (array) - An array of drip campaign objects. - **id** (string) - **name** (string) - **enabled** (boolean) ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignList((err, result) => { if (err) { console.error('Failed to list campaigns:', err.message); return; } result.forEach(campaign => { console.log(campaign.id, campaign.name, campaign.enabled); }); }); ``` ``` -------------------------------- ### List Your Templates Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Retrieves a list of all available templates. ```APIDOC ## List Your Templates ### Description Retrieves a list of all available templates. ### Method GET ### Endpoint /templates ### Code Example ```javascript var api = require('sendwithus')('API_KEY'); api.templates(callback); ``` ``` -------------------------------- ### List Sendwithus Templates Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Initialize the sendwithus API client with your API key and call the templates function to list all available templates. ```javascript var api = require('sendwithus')('API_KEY'); api.templates(callback); ``` -------------------------------- ### Create Sendwithus Template Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Initialize the sendwithus API client and use the createTemplate function with template details (name, subject, HTML, text) to create a new template. ```javascript var api = require('sendwithus')('API_KEY'); var data = { name: 'name', subject: 'subject', html: '
', text: 'some text' }; api.createTemplate(data, callback); ``` -------------------------------- ### Send Email with Required Parameters Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Send an email using the send function, providing the template ID and recipient's email address. A customer will be created if they don't exist. ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com' } }, callback); ``` -------------------------------- ### Create Template Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Creates a new email template. ```APIDOC ## Create Template ### Description Creates a new email template with the provided name, subject, HTML, and text content. ### Method POST ### Endpoint /templates ### Parameters #### Request Body - **name** (string) - Required - The name of the template. - **subject** (string) - Required - The subject line of the email. - **html** (string) - Required - The HTML content of the email. - **text** (string) - Required - The plain text content of the email. ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); var data = { name: 'name', subject: 'subject', html: '', text: 'some text' }; api.createTemplate(data, callback); ``` ``` -------------------------------- ### List Drip Campaigns Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Retrieves a list of all available drip campaigns. Requires a valid API key. ```javascript var api = require('sendwithus')('API_KEY'); api.dripCampaignList(callback); ``` -------------------------------- ### Render a Template with Data Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Use this to preview email content or debug template variables before sending. Ensure the API key, template ID, and version ID are correctly set. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.render({ template: 'tem_TEMPLATE_ID', template_data: { first_name: 'Bob', order_total: '$42.00' }, locale: 'en-US', version_id: 'ver_VERSION_ID', // optional: pin to a specific version strict: true // error if template variables are missing }, (err, result) => { if (err) { console.error('Render failed:', err.message); return; } // { // success: true, // status: 'OK', // template: { id: 'ver_...', name: '...', version_name: '...', locale: 'en-US' }, // subject: 'Hello Bob', // html: '...', // text: 'Hello Bob, your total is $42.00' // } console.log('Subject:', result.subject); console.log('HTML body:', result.html); }); ``` -------------------------------- ### Event System — `request` and `response` Events Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt The Sendwithus instance extends EventEmitter and emits `request` before each HTTP call and `response` upon receiving a successful reply. Use these for logging, metrics, or request tracing. ```APIDOC ## Event System — `request` and `response` Events The `Sendwithus` instance extends `EventEmitter` and emits `request` before each HTTP call and `response` upon receiving a successful reply. Use these for logging, metrics, or request tracing without wrapping every callback. ### Events - **request**: Emitted before an HTTP call is made. - Arguments: - **method** (string) - The HTTP method of the request. - **url** (string) - The URL of the request. - **headers** (object) - The request headers. - **body** (object) - The request body. - **response**: Emitted upon receiving a successful reply (2xx and non-error 1xx/3xx). - Arguments: - **statusCode** (number) - The HTTP status code of the response. - **body** (object) - The response body. - **response** (object) - The raw response object. ### Usage Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); // Log all outgoing requests api.on('request', (method, url, headers, body) => { console.log(`[REQUEST] ${method} ${url}`); console.log(' Headers:', JSON.stringify(headers)); console.log(' Body:', JSON.stringify(body)); }); // Log all successful responses (2xx and non-error 1xx/3xx) api.on('response', (statusCode, body, response) => { console.log(`[RESPONSE] ${statusCode}`, body.success ? 'OK' : 'FAILED'); }); // Use .once() to handle only the next event of a type api.once('response', (statusCode, body) => { if (statusCode === 200 && body.success) { console.log('First successful send confirmed.'); } }); api.send({ template: 'tem_TEMPLATE_ID', recipient: { address: 'user@example.com' } }, (err, result) => { if (err) console.error(err); }); ``` ``` -------------------------------- ### Show Drip Campaign Details Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Fetches detailed information about a specific drip campaign using its ID. An API key is necessary. ```javascript var api = require('sendwithus')('API_KEY'); api.dripCampaignDetails('DRIP_CAMPAIGN_ID', callback); ``` -------------------------------- ### Update or Create a Customer Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Creates a new customer or updates an existing one with the provided email and optional details. ```APIDOC ## Update or Create a Customer ### Description Creates a new customer or updates an existing one with the provided email and optional details. ### Method ```javascript api.customersUpdateOrCreate(data, callback) ``` ### Parameters #### Request Body - **email** (string) - Required - The email address of the customer. - **locale** (string) - Optional - The locale for the customer. ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); api.customersUpdateOrCreate({ email: 'foo@bar.com', locale: 'fr-CA' }, callback); ``` ``` -------------------------------- ### Create Sendwithus Template Version Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Initialize the sendwithus API client and use createTemplateVersion to add a new version to an existing template, providing the template ID and version data. ```javascript var api = require('sendwithus')('API_KEY'); var data = { name: 'name', subject: 'subject', html: '', text: 'some text' }; api.createTemplateVersion(TEMPLATE_ID, data, callback); ``` -------------------------------- ### List Drip Campaigns Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Retrieves a list of all available drip campaigns. ```APIDOC ## List Drip Campaigns ### Description Retrieves a list of all available drip campaigns. ### Method ```javascript api.dripCampaignList(callback) ``` ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); api.dripCampaignList(callback); ``` ``` -------------------------------- ### Update or Create a Customer Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Use this to update an existing customer's information or create a new one if they don't exist. Requires an API key and customer email. ```javascript var api = require('sendwithus')('API_KEY'); api.customersUpdateOrCreate({ email: 'foo@bar.com', locale: 'fr-CA' }, callback); ``` -------------------------------- ### Create a New Email Template Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Creates a new email template with a name, subject line, HTML body, and optional plain-text body. The callback receives the created template's `id` and `name` upon success. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.createTemplate({ name: 'Welcome Email', subject: 'Welcome to My App, {{ first_name }}!', html: 'Thanks for signing up.
', text: 'Hello {{ first_name }}, thanks for signing up.' }, (err, result) => { if (err) { console.error('Failed to create template:', err.message); return; } console.log('Template created:', result.id, result.name); }); ``` -------------------------------- ### Create a New Version for an Existing Template Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Adds a new version to an existing template, enabling A/B testing or iterative content updates. The callback receives the new version's `name` upon success. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.createTemplateVersion('tem_TEMPLATE_ID', { name: 'Version B', subject: 'Welcome, {{ first_name }}! (v2)', html: 'New layout for {{ first_name }}.
', text: 'New layout, {{ first_name }}.' }, (err, result) => { if (err) { console.error('Failed to create version:', err.message); return; } console.log('Version created:', result.name); }); ``` -------------------------------- ### Show Drip Campaign Details Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Fetches the details for a specific drip campaign using its ID. ```APIDOC ## Show Drip Campaign Details ### Description Fetches the details for a specific drip campaign using its ID. ### Method ```javascript api.dripCampaignDetails(drip_campaign_id, callback) ``` ### Parameters #### Path Parameters - **drip_campaign_id** (string) - Required - The ID of the drip campaign. ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); api.dripCampaignDetails('DRIP_CAMPAIGN_ID', callback); ``` ``` -------------------------------- ### Send Email with Optional Version Name Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Send an email using a specific optional template version name. The version name should be provided as a string, e.g., 'Version A'. ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, version_name:'Version A' }, callback); ``` -------------------------------- ### api.dripCampaignDeactivateAll(data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Unsubscribes a customer from every active drip campaign at once. This is useful for account deletion or global unsubscribe flows. ```APIDOC ## `api.dripCampaignDeactivateAll(data, callback)` — Remove Customer from All Campaigns Unsubscribes a customer from every active drip campaign at once — useful for account deletion or global unsubscribe flows. ### Parameters - **data** (object) - Required - An object containing recipient information. - **recipient_address** (string) - Required - The email address of the customer to unsubscribe from all campaigns. - **callback** (function) - Required - A callback function to handle the response. ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignDeactivateAll({ recipient_address: 'customer@example.com' }, (err, result) => { if (err) { console.error('Global deactivation failed:', err.message); return; } console.log('Removed from all campaigns:', result.success); // true }); ``` ### Response - **success** (boolean) - Indicates if the customer was successfully unsubscribed from all campaigns. ``` -------------------------------- ### Event System: Request and Response Logging Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt The Sendwithus instance emits 'request' before each HTTP call and 'response' upon receiving a successful reply. Use these events for logging, metrics, or request tracing without wrapping every callback. The 'request' event provides method, URL, headers, and body. The 'response' event provides status code, body, and the response object. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); // Log all outgoing requests api.on('request', (method, url, headers, body) => { console.log(`[REQUEST] ${method} ${url}`); console.log(' Headers:', JSON.stringify(headers)); console.log(' Body:', JSON.stringify(body)); }); // Log all successful responses (2xx and non-error 1xx/3xx) api.on('response', (statusCode, body, response) => { console.log(`[RESPONSE] ${statusCode}`, body.success ? 'OK' : 'FAILED'); }); // Use .once() to handle only the next event of a type api.once('response', (statusCode, body) => { if (statusCode === 200 && body.success) { console.log('First successful send confirmed.'); } }); api.send({ template: 'tem_TEMPLATE_ID', recipient: { address: 'user@example.com' } }, (err, result) => { if (err) console.error(err); }); ``` -------------------------------- ### Create Template Version Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Creates a new version for an existing email template. ```APIDOC ## Create Template Version ### Description Creates a new version for an existing email template, allowing for updates to the template's content. ### Method POST ### Endpoint /templates/{TEMPLATE_ID}/versions ### Parameters #### Path Parameters - **TEMPLATE_ID** (string) - Required - The ID of the template to create a new version for. #### Request Body - **name** (string) - Required - The name of the template version. - **subject** (string) - Required - The subject line of the email. - **html** (string) - Required - The HTML content of the email. - **text** (string) - Required - The plain text content of the email. ### Request Example ```javascript var api = require('sendwithus')('API_KEY'); var data = { name: 'name', subject: 'subject', html: '', text: 'some text' }; api.createTemplateVersion(TEMPLATE_ID, data, callback); ``` ``` -------------------------------- ### api.batch(data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Executes multiple API operations in a single HTTP request. Each item in the array specifies a path, method, and body. Individual item results include a status code. ```APIDOC ## `api.batch(data, callback)` — Batch Multiple API Calls Executes multiple API operations in a single HTTP request. Each item in the array specifies a `path`, `method`, and `body`. Individual item results include a `status_code` field; a `400` on one item does not fail the whole batch. ### Parameters - **data** (array) - Required - An array of objects, where each object represents an API call. - Each object should have: - **path** (string) - Required - The API endpoint path. - **method** (string) - Required - The HTTP method (e.g., 'POST'). - **body** (object) - Required - The request body for the API call. - **callback** (function) - Required - A callback function to handle the response. ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.batch([ { path: '/api/v1/send', method: 'POST', body: { template: 'tem_TEMPLATE_ID', recipient: { address: 'user1@example.com' }, template_data: { first_name: 'Alice' } } }, { path: '/api/v1/send', method: 'POST', body: { template: 'tem_TEMPLATE_ID', recipient: { address: 'user2@example.com' }, template_data: { first_name: 'Bob' } } } ], (err, result) => { if (err) { console.error('Batch request error:', err.message); return; } result.forEach((item, i) => { console.log(`Item ${i}: status ${item.status_code}`); // item.status_code === 200 → success // item.status_code === 400 → template not found or bad data }); }); ``` ### Response - **result** (array) - An array of results, one for each item in the batch request. - Each item contains: - **status_code** (number) - The HTTP status code of the individual API call. ``` -------------------------------- ### Render an Email Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Renders an email template with provided data, returning the subject, HTML, and text content. ```APIDOC ## Render an Email ### Description Renders an email template with provided data, returning the subject, HTML, and text content. ### Method ```javascript api.render(data, callback) ``` ### Parameters #### Request Body - **template** (string) - Required - The ID of the template to render. - **template_data** (object) - Required - Data to populate the template. - **strict** (boolean) - Required - If true, throws an error if template data is missing. - **locale** (string) - Optional - The locale to use for rendering. - **version_id** (string) - Optional - The ID of a specific template version to render. ### Request Example (Basic) ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, strict: true }, callback); ``` ### Request Example (Optional Locale) ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, locale: 'en-US', strict: true }, callback); ``` ### Request Example (Optional Template Version) ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, version_id: 'SAMPLE_VERSION_ID', strict: true }, callback); ``` ### Sample Response ```json { "success": true, "status": "OK", "template": { "id": "ver_r4nd0ml3tt3rsv15h4l0l", "name": "Template name", "version_name": "Template version name", "locale": "en-US" }, "subject": "RENDERED SUBJECT WITH DATA", "html": "RENDERED HTML BODY WITH DATA", "text": "RENDERED TEXT BODY WITH DATA" } ``` ``` -------------------------------- ### Send Email with Optional Headers Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Send an email with custom optional SMTP headers. The headers should be provided as an object with key-value pairs. ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, headers:{ 'X-HEADER-ONE': 'header-value' } }, callback); ``` -------------------------------- ### List All Drip Campaigns Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Retrieves a list of all drip campaigns configured for your Sendwithus account. The result includes campaign ID, name, and enabled status. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.dripCampaignList((err, result) => { if (err) { console.error('Failed to list campaigns:', err.message); return; } result.forEach(campaign => { console.log(campaign.id, campaign.name, campaign.enabled); }); }); ``` -------------------------------- ### api.render(data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Renders a template with provided data and returns the fully interpolated subject, HTML, and text without sending an email. Useful for previewing output or debugging template variables. ```APIDOC ## `api.render(data, callback)` — Render a Template Renders a template with provided data and returns the fully interpolated subject, HTML, and text — without sending an email. Useful for previewing output or debugging template variables. ### Parameters #### Request Body - **template** (string) - Required - The ID of the template to render. - **template_data** (object) - Optional - Data to interpolate into the template. - **locale** (string) - Optional - The locale to use for rendering. - **version_id** (string) - Optional - Pin to a specific version of the template. - **strict** (boolean) - Optional - If true, errors if template variables are missing. ### Callback Parameters - **err** (object) - An error object if the render fails. - **result** (object) - An object containing the rendered subject, HTML, and text. - **success** (boolean) - **status** (string) - **template** (object) - **subject** (string) - **html** (string) - **text** (string) ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.render({ template: 'tem_TEMPLATE_ID', template_data: { first_name: 'Bob', order_total: '$42.00' }, locale: 'en-US', version_id: 'ver_VERSION_ID', // optional: pin to a specific version strict: true // error if template variables are missing }, (err, result) => { if (err) { console.error('Render failed:', err.message); return; } console.log('Subject:', result.subject); console.log('HTML body:', result.html); }); ``` ``` -------------------------------- ### API Response - Error Cases Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Illustrates potential error responses from the API, such as malformed requests or invalid API keys. ```javascript > err.statusCode; 400 ``` ```javascript > err.statusCode; 403 ``` -------------------------------- ### api.templates(callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Retrieves all email templates associated with the account. The result is an array of template objects. ```APIDOC const api = require('sendwithus')('YOUR_API_KEY'); api.templates((err, result) => { if (err) { console.error('Failed to list templates:', err.message); return; } result.forEach(template => { console.log(template.id, template.name); }); }); ``` -------------------------------- ### Render Email with Optional Template Version Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Renders a specific version of an email template. Useful for testing or using older versions. Requires template ID, data, and version ID. ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, version_id: 'SAMPLE_VERSION_ID', strict: true }, callback); ``` -------------------------------- ### Send Batch Requests Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Sends multiple API requests in a single batch. Each request specifies the path, method, and body. Useful for efficiency. ```javascript var api = require('sendwithus')('API_KEY'); api.batch([{ "path": "/api/v1/send", "method": "POST", "body": { "template": "TEMPLATE_ID", "recipient": { "address": "test+1@mydomain.com" } } }, { "path": "/api/v1/send", "method": "POST", "body": { "template": "TEMPLATE_ID", "recipient": { "address": "test+2@mydomain.com" } } }], callback); ``` -------------------------------- ### Render Email with Optional Locale Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Renders an email template, allowing specification of a locale for localized content. Includes template ID, data, and locale. ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, locale: 'en-US', strict: true }, callback); ``` -------------------------------- ### List All Email Templates Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Retrieves all email templates associated with the account. The result is an array of template objects, each containing an `id` and `name`. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.templates((err, result) => { if (err) { console.error('Failed to list templates:', err.message); return; } // result is an array of template objects result.forEach(template => { console.log(template.id, template.name); }); }); ``` -------------------------------- ### Send an Email Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Sends an email using a specified template and recipient. ```APIDOC ## Send an Email ### Description Sends an email using a specified template. If the recipient does not exist, a customer record will be created. ### Method POST ### Endpoint /send ### Parameters #### Request Body - **template** (string) - Required - The ID of the template to send. - **recipient** (object) - Required - The recipient's information. - **address** (string) - Required - The recipient's email address. - **name** (string) - Optional - The recipient's name. - **template_data** (object) - Optional - Data to populate the template. - **sender** (object) - Optional - The sender's information. - **address** (string) - Required - The sender's email address. - **reply_to** (string) - Optional - The sender's reply-to address. - **name** (string) - Optional - The sender's name. - **cc** (array) - Optional - An array of CC recipients. - Each object should have an **address** (string) field. - **bcc** (array) - Optional - An array of BCC recipients. - Each object should have an **address** (string) field. - **headers** (object) - Optional - SMTP headers to include with the email. - **esp_account** (string) - Optional - The ID of the ESP Account to send through. - **locale** (string) - Optional - The template locale to send (e.g., 'en-US'). - **version_name** (string) - Optional - The template version to send (e.g., 'Version A'). ### Request Example (Required Parameters Only) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com' } }, callback); ``` ### Request Example (With Template Data) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com', // required name: 'Matt and Brad' }, template_data: { first_name: 'Matt' } }, callback); ``` ### Request Example (Optional Sender) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com' }, template_data: { first_name: 'Matt' }, sender: { address: 'company@company.com', // required name: 'Company' } }, callback); ``` ### Request Example (Optional Sender with Reply-To) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com' }, template_data: { first_name: 'Matt' }, sender: { address: 'company@company.com', // required name: 'Company', reply_to: 'info@company.com' } }, callback); ``` ### Request Example (Optional BCC/CC) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, bcc: [{ address: 'bcc@sendwithus.com' }], cc: [ { address: 'cc1@sendwithus.com' }, { address: 'cc2@sendwithus.com' } ] }, callback); ``` ### Request Example (Optional Headers) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, headers:{ 'X-HEADER-ONE': 'header-value' } }, callback); ``` ### Request Example (Optional ESP Account) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, esp_account:'esp_1234asdf1234' }, callback); ``` ### Request Example (Optional Locale) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, locale:'en-US' }, callback); ``` ### Request Example (Optional Version) ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, version_name:'Version A' }, callback); ``` ``` -------------------------------- ### Enable Debug Mode Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Instantiates the Sendwithus API object with debug mode enabled. This logs detailed request and payload information, useful for troubleshooting. ```javascript var api = require('sendwithus')('API_KEY', debug=true); ``` -------------------------------- ### api.customersUpdateOrCreate(data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Creates a new customer record or updates an existing one identified by email address. Supports locale and arbitrary custom data fields. ```APIDOC ## `api.customersUpdateOrCreate(data, callback)` — Upsert a Customer Creates a new customer record or updates an existing one identified by email address. Supports locale and arbitrary custom data fields. ### Parameters #### Request Body - **email** (string) - Required - The email address of the customer. - **locale** (string) - Optional - The locale for the customer. - **data** (object) - Optional - Arbitrary custom data fields for the customer. - **first_name** (string) - **plan** (string) - **signup_date** (string) ### Callback Parameters - **err** (object) - An error object if the upsert fails. - **result** (object) - An object indicating the success of the operation. - **success** (boolean) ### Request Example ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.customersUpdateOrCreate({ email: 'jane@example.com', locale: 'fr-CA', data: { first_name: 'Jane', plan: 'pro', signup_date: '2024-01-15' } }, (err, result) => { if (err) { console.error('Customer upsert failed:', err.message); return; } console.log('Customer saved:', result.success); // true }); ``` ``` -------------------------------- ### Render an Email Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Renders an email template with provided data. Requires the template ID and template data. The 'strict' option ensures errors are thrown for missing data. ```javascript var api = require('sendwithus')('API_KEY'); api.render({ template: 'SAMPLE_TEMPLATE_ID', template_data: { name: 'Bob' }, strict: true }, callback); ``` -------------------------------- ### Batch Multiple API Calls Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Executes multiple API operations in a single HTTP request to reduce overhead. Each item specifies a path, method, and body. Individual item results include a status code; a 400 on one item does not fail the whole batch. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.batch([ { path: '/api/v1/send', method: 'POST', body: { template: 'tem_TEMPLATE_ID', recipient: { address: 'user1@example.com' }, template_data: { first_name: 'Alice' } } }, { path: '/api/v1/send', method: 'POST', body: { template: 'tem_TEMPLATE_ID', recipient: { address: 'user2@example.com' }, template_data: { first_name: 'Bob' } } } ], (err, result) => { if (err) { console.error('Batch request error:', err.message); return; } result.forEach((item, i) => { console.log(`Item ${i}: status ${item.status_code}`); // item.status_code === 200 → success // item.status_code === 400 → template not found or bad data }); }); ``` -------------------------------- ### api.send(data, callback) Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Sends a transactional email to a recipient using a saved template. Supports various options including sender override, CC/BCC, custom headers, locale, and template version targeting. ```APIDOC const api = require('sendwithus')('YOUR_API_KEY'); const callback = (err, result) => { if (err) { console.error('Send failed:', err.message, '| Status:', err.statusCode); return; } console.log('Sent! Receipt ID:', result.receipt_id); }; // Minimal send api.send({ template: 'tem_TEMPLATE_ID', recipient: { address: 'user@example.com' } }, callback); // Full-featured send api.send({ template: 'tem_TEMPLATE_ID', recipient: { address: 'user@example.com', name: 'Jane Doe' }, template_data: { first_name: 'Jane', order_id: '12345' }, sender: { address: 'support@myapp.com', name: 'My App', reply_to: 'noreply@myapp.com' }, cc: [{ address: 'cc@myapp.com' }], bcc: [{ address: 'bcc@myapp.com' }], headers: { 'X-Custom-Header': 'value' }, esp_account: 'esp_1a2b3c4d5e', locale: 'en-US', version_name: 'Version A' }, callback); ``` -------------------------------- ### Upsert a Customer Record Source: https://context7.com/sendwithus/sendwithus_nodejs/llms.txt Creates a new customer or updates an existing one based on their email address. You can specify locale and custom data fields. Ensure the email address is valid. ```javascript const api = require('sendwithus')('YOUR_API_KEY'); api.customersUpdateOrCreate({ email: 'jane@example.com', locale: 'fr-CA', data: { first_name: 'Jane', plan: 'pro', signup_date: '2024-01-15' } }, (err, result) => { if (err) { console.error('Customer upsert failed:', err.message); return; } console.log('Customer saved:', result.success); // true }); ``` -------------------------------- ### Send Email with Optional Reply-To Address Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Send an email with an optional sender name and reply-to address. The sender's address remains a required field. ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: 'TEMPLATE_ID', recipient: { address: 'us@sendwithus.com' }, template_data: { first_name: 'Matt' }, sender: { address: 'company@company.com', // required name: 'Company', reply_to: 'info@company.com' } }, callback); ``` -------------------------------- ### Send Email with Optional Locale Source: https://github.com/sendwithus/sendwithus_nodejs/blob/master/README.md Send an email using a specific optional locale for the template. The locale should be provided as a string, e.g., 'en-US'. ```javascript var api = require('sendwithus')('API_KEY'); api.send({ template: TEMPLATE_ID, recipient: { address: 'us@sendwithus.com' }, locale:'en-US' }, callback); ```