Send email from Node.js with an API
Updated November 24, 20264 min read
Why an API and not SMTP
With SMTP your process opens a connection, negotiates TLS and waits for the server on every email; if something fails halfway, you don’t know whether it went out. With an API you make a request, get an ID back and hear about the delivery or the bounce by webhook.
The full differences are in SMTP or API. Here is the code.
What you need
- Node.js 18 or later, which ships
fetch. Nothing to install. - A test key (
tw_test_…) from Organization → API, in theTELLWING_API_KEYenvironment variable. Never in your code or in the browser. - To send for real, your verified domain and a
tw_live_key. API Free includes 3,000 emails a month for nothing.
The request, with fetch
The API answers 202 with id, status, recipients (the ones counted against the quota), suppressed (addresses skipped because they bounced before) and delivery_id, the same one your webhooks carry.
One function for your whole app
In production you want a single function that retries what can be retried. Since the Idempotency-Key is stored for 24 hours, retrying with the same key never sends twice:
With a template your team built in the builder, you send its ID and its variables instead of the HTML:
Express, Next.js or NestJS
The function works the same in any framework, always on the server: in Next.js, from a route handler or a server action; never from a client component, because it would expose your key. If you send when a purchase completes, use the order ID in the Idempotency-Key: a double click or a retry from your payment processor won’t send two receipts.
Webhooks: verify the signature
Every event (email.delivered, email.bounced, email.complained) arrives signed in the Tellwing-Signature header. Verify it with the raw request body and your secret:
In Express, receive the webhook with express.raw(), which keeps the raw body, and deduplicate by the event’s id: a retry carries the same one.
Test it without sending anything
With the test key, each sandbox address gives the result its name says: delivered@sandbox.tellwing.com is delivered, bounced@ bounces, complained@ complains and suppressed@ is treated as an address that already bounced. Add +anything before the at sign for unique addresses in your tests.
Frequently asked questions
Do I need to install a package to send email from Node.js?
No. Since Node.js 18, fetch is built in, and Tellwing’s API is REST with JSON: one request, one email.
Where do I keep the API key?
In a server environment variable, like TELLWING_API_KEY. Never in the repository or in the browser: whoever has it can send in your name.
How do I avoid sending the same email twice?
With the Idempotency-Key header: if you repeat the request with the same key within 24 hours, the API returns the stored response and doesn’t send again.
How much does sending email from Node.js with Tellwing cost?
API Free comes with every account: 3,000 transactional emails a month, up to 100 a day. API Pro costs $20 for 50,000 or $35 for 100,000, and the yearly plan bills 11 months for 12.