Skip to main content

Prerequisites

Installation

Install the SDK alongside Express and dotenv.

Storing your API key

Create a .env file in your project root and add the key.
.env
Add .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
The startup check fails fast on a missing key. Without it, the first request would be the place the problem surfaces.
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 calls Emails.send().
src/server.js
The 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
Mount the router in your application file.
src/server.js

Sending HTML emails

Add the html parameter for rich content. Include text as a fallback for clients that block HTML.
src/routes/welcome.js
The 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.
Then post a request from another terminal using cURL.
A successful request returns the message ID:
Open the Email logs page in your SendLayer dashboard to confirm delivery.

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.