uSend Docs
Quickstarts

Send with Next.js

Send email from a Next.js route handler or server action with the usend SDK.

Send from the server only: an API key must never reach the browser. This page uses the App Router. You need a verified domain and an API key.

1. Install

Shell
npm install @usend/node

2. Add the key

.env.local
USEND_API_KEY=us_123456789

Do not prefix it with NEXT_PUBLIC_: that would put it in the browser bundle.

3. Route handler

app/api/send/route.ts
import { Usend } from '@usend/node';

const usend = new Usend(process.env.USEND_API_KEY);

export async function POST(request: Request) {
  const { email } = await request.json();

  const { data, error } = await usend.emails.send({
    from: 'Acme <[email protected]>',
    to: [email],
    subject: 'Welcome to Acme',
    html: '<p>Thanks for signing up.</p>',
  });

  if (error) return Response.json({ error }, { status: error.statusCode || 500 });
  return Response.json({ id: data.id });
}

Or a server action

app/actions.ts
'use server';
import { Usend } from '@usend/node';

const usend = new Usend(process.env.USEND_API_KEY);

export async function sendWelcome(email: string) {
  const { error } = await usend.emails.send({
    from: 'Acme <[email protected]>',
    to: [email],
    subject: 'Welcome to Acme',
    text: 'Thanks for signing up.',
  });
  return { ok: !error };
}
Tip

Calling the usend API from client-side code does not work by design: the API answers cross-origin browser requests only for the usend dashboard. See CORS.

Test it with curl -X POST localhost:3000/api/send -d '{"email":"[email protected]"}'. In the sandbox, send to a verified member of your organization.