Prerequisites
- Node.js 16 or later
- An Express 4 or Express 5 project
- Authorize your sending domain
- Create or retrieve your SendLayer API key
Installation
Install the SDK alongside Express anddotenv.
Storing your API key
Create a.env file in your project root and add the key.
.env
.env to .gitignore so the key never reaches version control.
Creating the email client
Initialize the client in its own module and export it. A single instance serves every route in the application.src/sendlayer.js
These examples use ES modules. Add
"type": "module" to your package.json, or convert the imports to require() calls if your project uses CommonJS.Sending an email from a route
Add a POST route that reads JSON from the request body and callsEmails.send().
src/server.js
express.json() middleware parses the request body. Without it, req.body is undefined.
The sender email address must match a domain you have authorized in SendLayer.
Validating input before sending
Reject malformed requests before they reach the API. This saves a round trip and returns a clearer error to the caller.src/routes/email.js
src/server.js
Sending HTML emails
Add thehtml parameter for rich content. Include text as a fallback for clients that block HTML.
src/routes/welcome.js
tags array labels the message. Tags show up in your event history, which helps you separate welcome emails from other traffic.
Testing the endpoint
Start the developmentserver.Next steps
Node.js SDK Reference
Review every method and parameter in the Node.js SDK.
Managing Webhooks
Create, list, and delete webhook subscriptions.
Retrieving Events
Query delivery events for any message.
Rate Limits
Understand recipient and request limits.