This is the advanced guide for Vercel Functions. If you’re just getting started, check out the Quickstart Guide first.
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 atapp/actions/email.ts:
Using in a Component
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:
Production Setup
Add environment variables via the Vercel dashboard or CLI:- Go to your project settings
- Navigate to Environment Variables
- Add
LETTR_API_KEYandFROM_EMAIL - 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
Environment variables not loading
Environment variables not loading
If environment variables aren’t available in your function:
- Verify
.env.localexists in your project root - Restart the dev server after adding new variables
- Check Vercel dashboard for production variables
- Redeploy after adding production variables
Function timeouts
Function timeouts
If your function times out:
- Increase maxDuration (requires Pro plan or higher):
- Check your API key is valid and not rate-limited
- Monitor Vercel logs for specific error messages
CORS errors
CORS errors
If you’re calling the API from a different domain, add CORS headers:
TypeScript errors
TypeScript errors
If you see TypeScript errors about
process.env:- Use non-null assertion for required variables:
- Add type definitions in
env.d.ts:
Rate limiting
Rate limiting
If you’re hitting Lettr’s rate limits:
- Implement request queuing to smooth out bursts
- Add exponential backoff for retries
- Consider upgrading your Lettr plan
- Use batch sending for multiple recipients
Best Practices
- Use environment variables for all sensitive data
- Set appropriate maxDuration based on your plan (30s recommended)
- Validate inputs before sending emails
- Implement rate limiting to prevent abuse
- Log request IDs for debugging and tracking
- Use Edge Runtime for faster cold starts when possible
- Handle errors gracefully with proper HTTP status codes
- 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