Get started with sending emails from Vercel Functions in minutes
Send transactional emails from Vercel serverless functions using Lettr’s HTTP API. Vercel’s global edge network and fast cold starts make it perfect for email-triggered API routes and server actions.Using Cursor? Jump straight in using this prompt
For Next.js 13+ projects using the App Router, create a file at app/api/send/route.ts:
import { NextResponse } from 'next/server';import { Lettr } from 'lettr';const lettr = new Lettr(process.env.LETTR_API_KEY!);export async function POST(request: Request) { try { const body = await request.json(); const { to, subject, html, text } = body; // Validate required fields if (!to || !subject || (!html && !text)) { return NextResponse.json( { error: 'Missing required fields: to, subject, and html or text' }, { status: 400 } ); } // Send email via Lettr const result = await lettr.emails.send({ from: process.env.FROM_EMAIL || 'noreply@yourdomain.com', to: Array.isArray(to) ? to : [to], subject, html, text, }); console.log(`Email sent successfully. Request ID: ${result.request_id}`); return NextResponse.json({ success: true, requestId: result.request_id, accepted: result.accepted, }); } catch (error: any) { console.error('Failed to send email:', error); return NextResponse.json( { error: error.message || 'Failed to send email' }, { status: error.status || 500 } ); }}// Configure function executionexport const maxDuration = 30; // Max duration in seconds (Pro plan and higher)
The maxDuration export configures the maximum execution time for the function. Email sending typically completes in under 1 second, but setting a higher limit accounts for retries and network latency.