Prerequisites
- A Next.js project (version 13.4 or later for the App Router)
- Node.js 16 or later
- Authorize your sending domain
- Create or retrieve your SendLayer API key
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
.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 atapp/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
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
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
app/signup/page.jsx
Testing the endpoint
Start the development server.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.