Skip to main content
This is the advanced guide for Vercel Functions. If you’re just getting started, check out the Quickstart Guide first.
This guide covers advanced patterns for sending emails from Vercel Functions, including Server Actions, Edge Runtime, environment variables, and production best practices.

Using Raw Fetch API

For a zero-dependency approach, use the native fetch API:

App Router

Pages Router

Server Actions (Next.js 14+)

For Next.js 14+ projects, you can send emails directly from Server Actions:

Creating a Server Action

Create a file at app/actions/email.ts:

Using in a Component

Server Actions are perfect for form submissions that trigger emails — no need to create a separate API route.

Edge Runtime

For ultra-fast cold starts, deploy your email function to Vercel’s Edge Runtime:
Edge Runtime functions deploy to Vercel’s global edge network for sub-50ms cold starts. However, they have some limitations compared to Node.js runtime (e.g., no native Node.js APIs).

Environment Variables

Development Setup

Create a .env.local file in your project root:
Never commit .env.local to version control. Add it to your .gitignore file.

Production Setup

Add environment variables via the Vercel dashboard or CLI:
You can also add environment variables in the Vercel dashboard:
  1. Go to your project settings
  2. Navigate to Environment Variables
  3. Add LETTR_API_KEY and FROM_EMAIL
  4. Select the environments (Production, Preview, Development)

Calling Your API Route

From Client Components

From Server Components

Advanced Patterns

Rate Limiting with Upstash

Implement rate limiting to prevent abuse:

Email Validation

Add email validation before sending:

Template-Based Emails

Use Lettr templates for consistent email design:

Troubleshooting

If environment variables aren’t available in your function:
  1. Verify .env.local exists in your project root
  2. Restart the dev server after adding new variables
  3. Check Vercel dashboard for production variables
  4. Redeploy after adding production variables
If your function times out:
  1. Increase maxDuration (requires Pro plan or higher):
  2. Check your API key is valid and not rate-limited
  3. Monitor Vercel logs for specific error messages
If you’re calling the API from a different domain, add CORS headers:
If you see TypeScript errors about process.env:
  1. Use non-null assertion for required variables:
  2. Add type definitions in env.d.ts:
If you’re hitting Lettr’s rate limits:
  1. Implement request queuing to smooth out bursts
  2. Add exponential backoff for retries
  3. Consider upgrading your Lettr plan
  4. Use batch sending for multiple recipients

Best Practices

  1. Use environment variables for all sensitive data
  2. Set appropriate maxDuration based on your plan (30s recommended)
  3. Validate inputs before sending emails
  4. Implement rate limiting to prevent abuse
  5. Log request IDs for debugging and tracking
  6. Use Edge Runtime for faster cold starts when possible
  7. Handle errors gracefully with proper HTTP status codes
  8. Consider Server Actions for form-triggered emails

What’s Next

Quickstart Guide

Back to quickstart

AWS Lambda

Deploy on AWS Lambda

Cloudflare Workers

Deploy on Cloudflare

Templates

Use Lettr templates