Skip to content

Vercel Functions

Send emails using Vercel Functions and the MailChannels Node.js SDK.

Deploy on Vercel

In this guide we use Next.js route handlers to create Vercel Functions, but you can use any framework that supports Vercel Functions.

Prerequisites

1. Install

Add the mailchannels-sdk package dependency to your Next.js project.

sh
npm i mailchannels-sdk
sh
yarn add mailchannels-sdk
sh
pnpm add mailchannels-sdk
sh
bun add mailchannels-sdk
sh
deno add npm:mailchannels-sdk

2. Configure your API key

Add your MailChannels API key to your .env or .env.local file.

.env.local
sh
MAILCHANNELS_API_KEY=your-api-key

3. Create a Next.js function

Register an App route handler under app/api/send/route.ts. Each route file you create under app/api/ is automatically deployed as a Vercel Function.

Use the html property to send an email with HTML content.

app/api/send/route.ts
ts
import { MailChannels } from 'mailchannels-sdk'

const mailchannels = new MailChannels(process.env.MAILCHANNELS_API_KEY)

export async function POST () {
  const { data, error } = await mailchannels.emails.send({
    from: 'Name <from@example.com>',
    to: 'to@example.com',
    subject: 'Test email',
    html: '<p>Hello World</p>'
  })

  if (error) {
    return Response.json(error, {
      status: error.statusCode || 400
    })
  }

  return Response.json(data)
}

4. Test locally

Start the Next.js development server.

sh
npm run dev

Your function will be available at http://localhost:3000/api/send as a POST endpoint.

5. Deploy to Vercel

Add your MAILCHANNELS_API_KEY as an environment variable in your Vercel project.

Then deploy your project with the Vercel CLI.

sh
vercel

Your function will be available at https://your-project.vercel.app/api/send.

Released under the MIT License.