client.send.message from server code. Courier delivers them through Resend and adds SMS, push, and in-app when you need them.
first-send.mts, then run it with your API key and email address:
courier-nextjs-email-quickstart
The finished Next.js app from this guide, ready to clone and run.
Build this with a coding agent
To have a coding agent add Courier to your Next.js app, paste this prompt into Claude Code, Cursor, or Codex from the project root:npx skills add trycourier/courier-skills, and connect the Courier MCP server so the agent can check Courier Logs and templates itself.
Rules for AI agents
- Use
@trycourier/courierthroughlib/courier.ts, which keepsimport "server-only". Only the standalonescripts/send.tsbuilds its own client. - Read
COURIER_API_KEYfrom the environment on the server. Never import the client into a client component. - Pass an
"Idempotency-Key"on resets and receipts:password-reset-${resetId}for a reset, Stripe’sevent.idfor a receipt. - Send from the server action or route handler that owns the event. Verify the Stripe signature before sending a receipt.
- Build reset links from
APP_URL, never from the request’sHostheader. - Never call Resend directly. Courier routes the
emailchannel to it.
What you’ll build
This Next.js 16 App Router app sends three emails, each oneclient.send.message call from @trycourier/courier:
- A welcome email from the
signUpserver action - A password reset email from
POST /api/password-reset - A receipt email from
POST /api/stripe, oninvoice.payment_succeeded
Prerequisites
For the first send
- A Courier API key
- Node.js 20.9 or newer
For the full build
- A Next.js app on the App Router
- A Resend integration connected in Courier
- A verified sending domain in Resend
- The Stripe CLI
Clone the finished Next.js app
Clone the Courier Next.js quickstart to run the finished app with your own key:Install the Courier Node SDK in your Next.js app
Install the Courier Node SDK,@trycourier/courier, in your Next.js app with server-only and stripe:
.env.local, as in the repo’s .env.example:
Create a server-only Courier client for Next.js
lib/courier.ts gives every Next.js route one Courier client that stays on the server.
lib/courier.ts
Send a welcome email from a Next.js server action
The Next.jssignUp server action sends the welcome email through Courier.
app/actions.ts
app/page.tsx
Send a password reset email from a Next.js route handler
The Next.js route handler emails a one-time reset link through Courier, with one idempotency key per reset.app/api/password-reset/route.ts
Send a receipt email from a Stripe webhook in Next.js
The Next.js Stripe webhook route verifies the event, then sends a receipt through Courier keyed by the event id.app/api/stripe/route.ts
Add a test send script
scripts/send.ts sends the welcome email from the command line, so you can check your key and Resend before wiring up a form. server-only resolves only inside Next.js, so the script builds its own client. Add its tools and an npm run send script:
scripts/send.ts
Verify
Send each Next.js email once, then find it in Courier Logs:- Run
npm run send. It prints a request id. - Run
npm run dev, openlocalhost:3000, and sign up with your email. - Request a reset for your
DEMO_EMAILaddress. The route answers{"ok":true}either way, so check Logs:curl -X POST localhost:3000/api/password-reset -H "Content-Type: application/json" -d '{"email": "YOUR_DEMO_EMAIL"}' - Run
stripe listen --forward-to localhost:3000/api/stripe, put itswhsec_secret in.env.local, restart, thenstripe trigger invoice.payment_succeeded --override customer:email=YOUR_EMAIL
Go to production with Courier
Courier is notification infrastructure for humans and AI agents. In a Next.js app it sends through Resend. Go live covers moving from Test to Production:- Use a Production key, with Resend connected in Production.
- Record processed Stripe event ids. Stripe retries for three days, past Courier’s 25-hour idempotency window (Stripe guide).
- Put resets and receipts on a
REQUIREDtopic so preferences never block them.
FAQ
Can I keep using Resend with Courier?
Can I keep using Resend with Courier?
Resend stays your email provider when you add Courier. Connect the Resend integration, and Courier sends every email through it.
How do I stop a password reset email from sending twice?
How do I stop a password reset email from sending twice?
A password reset sends once per
"Idempotency-Key", which covers retries for 25 hours. A second form submit is a new reset, so return early when the user has an unexpired one.Can I send email from a Next.js server action?
Can I send email from a Next.js server action?
A Next.js server action runs on the server, so it can call Courier’s
client.send.message from a module marked import "server-only".How do I add SMS or in-app notifications later?
How do I add SMS or in-app notifications later?
Courier adds a channel through the send’s
routing. Connect an SMS provider or the Courier Inbox, then list it: method: "single" falls back in order, and method: "all" sends on every channel.