Vercel Functions
Send emails using Vercel Functions and the MailChannels Node.js SDK.
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.
npm i mailchannels-sdkyarn add mailchannels-sdkpnpm add mailchannels-sdkbun add mailchannels-sdkdeno add npm:mailchannels-sdk2. Configure your API key
Add your MailChannels API key to your .env or .env.local file.
MAILCHANNELS_API_KEY=your-api-key3. 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.
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.
npm run devYour 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.
vercelYour function will be available at https://your-project.vercel.app/api/send.