The `smtpapi` library is an official Twilio SendGrid Node.js module designed to programmatically construct X-SMTPAPI headers. These headers enable advanced email features such as scheduled sends, substitutions, categories, unique arguments, and recipient lists for emails sent via SendGrid's SMTP API or older V2 Mail Send API. The current stable version is 1.4.7. The package has a maintenance-focused release cadence, with recent updates primarily consisting of chore and documentation improvements rather than new features. While functional, it's crucial to note that newer SendGrid V3 Mail Send API integrations typically utilize `custom_args` directly within the API request body, which differs from the X-SMTPAPI header approach this library facilitates. This library serves as a dedicated tool for those using the SMTP or V2 API methods that rely on the X-SMTPAPI header format.
npm install smtpapiVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize the `smtpapi` header, add recipients, unique arguments, categories, and substitutions, then retrieve the complete JSON string for inclusion in an email's X-SMTPAPI header.
For new projects or integrations with SendGrid's V3 Mail Send API, consult the official `@sendgrid/mail` Node.js SDK documentation for how to pass `custom_args`, `categories`, and other parameters directly in the mail send request body. Only use this library when explicitly sending email via SendGrid's SMTP endpoint or V2 Mail Send API.
Ensure your project is configured for CommonJS if you wish to use `require()`. If you must use it within an ESM project, use dynamic `import()`: `const SmtpapiModule = await import('smtpapi'); const Smtpapi = SmtpapiModule.default;`.Always use a currently supported Node.js Long Term Support (LTS) release for development and production environments. While this library is likely compatible with newer Node.js versions due to its simplicity, it's not officially stated to support them beyond v16.
If your project is ESM, either convert the file using `smtpapi` to CommonJS (e.g., change extension to `.cjs` or remove `"type": "module"` from `package.json`), or use dynamic `import()`: `const SmtpapiModule = await import('smtpapi'); const Smtpapi = SmtpapiModule.default;`.Always instantiate the `Smtpapi` object with `new`: `const header = new Smtpapi();`. If using dynamic `import()` in an ESM context, remember to access the default export: `const SmtpapiModule = await import('smtpapi'); const Smtpapi = SmtpapiModule.default; const header = new Smtpapi();`.Ensure all values passed to `setUniqueArgs()` (and similar methods that operate on JSON fields within X-SMTPAPI) are explicitly converted to strings. For example, `{ id: 123 }` should be `{ id: '123' }`.No dependency data recorded yet.