Skip to main content

Prerequisites

Installation

Install the SendLayer Node.js SDK in your project root.

Storing your API key

Add your API key to .env.local in the project root.
.env.local
Never prefix the key with NEXT_PUBLIC_. Any variable with that prefix ships to the browser, which exposes your credentials to every visitor.
Add .env.local to your .gitignore file so the key stays out of version control.

Sending an email from a route handler

Create a Route Handler at app/api/send-email/route.js. Next.js runs this file on the server only, so the SDK and your API key remain private.
app/api/send-email/route.js
The replyTo field points at the person who filled in the form. Support agents can then reply directly from their inbox.
Use a from address on a domain you authorized in SendLayer. Sending from an unauthorized domain causes the request to fail.

Submitting the form from a client component

Create a client component that posts JSON to the route handler.
app/contact/ContactForm.jsx
Render the component from a page in app/contact/page.jsx.
app/contact/page.jsx

Sending an email from a server action

A server action removes the need for a separate endpoint. Mark the file with 'use server' and call the SDK inside the action.
app/actions/sendEmail.js
Pass the action directly to a form. Next.js posts the form data to the server and runs the action there.
app/signup/page.jsx

Testing the endpoint

Start the development server.
Then post a request to the route handler 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

Track opens, clicks, and bounces with webhooks.

Retrieving Events

Query delivery events for any message.

Rate Limits

Understand recipient and request limits.