Skip to main content

Prerequisites

To get the most out of this guide, you’ll need to: Make sure you have the latest version of the Vercel CLI installed.

1. Install dependencies

Install the Emailr SDK:
npm install emailr

2. Set up environment variables

Add your Emailr API key to your environment variables:
.env.local
EMAILR_API_KEY=em_xxxxxxxxx

3. Create a Next.js function

Create a route file under app/api/send/route.ts if you’re using the App Router.
route.ts
import { Emailr } from 'emailr';

const emailr = new Emailr(process.env.EMAILR_API_KEY);

export async function POST() {
  const response = await emailr.emails.send({
    from: 'Acme <[email protected]>',
    to: ['[email protected]'],
    subject: 'hello world',
    html: '<strong>it works!</strong>',
  });

  return Response.json(response, {
    status: response.error ? 500 : 200,
  });
}

4. Send email locally

Run function locally:
npm run dev
Open the endpoint URL to send an email: http://localhost:3000/api/send

5. Send email in production

Deploy function to Vercel:
vercel
Make sure to add your EMAILR_API_KEY environment variable in your Vercel project settings. Open the endpoint URL to send an email: https://your-project.vercel.app/api/send

6. Try it yourself

Vercel Functions Example

See the full source code.