How to integrate Resend email service with Functions
Learn how to integrate Resend email service with Azion Functions to send transactional emails directly from the edge with optimal performance and reliability.
This guide demonstrates how to integrate Resend email service with Azion Functions to send transactional emails directly from the edge. This integration provides fast, reliable email delivery with optimal performance and global reach.
Requirements
Before you begin, ensure you have:
- An Azion account
- A Resend account with API access
- Azion CLI installed and configured
- Node.js version 18 or higher
Code
This is an example of how to use Resend with Azion Functions to send transactional emails. The complete code example you can find in this GitHub repository.
Deploying to Azion
Step 1: Authenticate with Azion
- Log in to your Azion account via CLI:
- Follow the authentication prompts to connect your CLI with your Azion account.
Step 2: Create a new Applications
- Initialize a new Application:
-
Select the template Hono Boilerplate to use for your email application.
-
Follow the prompts to configure your new Application.
Step 3: Create a secret for the Resend API key
For security, store your Resend API key as a secret:
When prompted, enter your Resend API key. This ensures your sensitive data is encrypted and secure.
Step 4: Deploy the Function
Deploy your email application to Azion’s edge network:
The deployment process will:
- Upload your Function code
- Configure the application
- Set up the necessary routing rules
- Configure environment variables and secrets
- Provide you with a unique domain
Step 4: Access your application
After deployment, you’ll receive a domain like https://xxxxxxx.map.azionedge.net. Your email application will be available at this URL within a few minutes after DNS propagation.
Understanding the implementation
Email sending process
The Resend Function typically handles:
- Request validation: Checking email parameters and authentication
- Email composition: Building email content with templates
- API integration: Communicating with Resend’s API
- Response handling: Processing API responses and errors
- Logging: Recording email sending activities
Key benefits
- Performance: Email processing at the edge reduces latency
- Reliability: Resend’s robust infrastructure ensures delivery
- Scalability: Automatically scales with demand
- Global reach: Edge locations provide worldwide coverage
- Cost efficiency: Optimized email delivery costs
Testing your email application
Step 1: Test email sending functionality
- Navigate to your deployed application
- Use the email form to send test emails
- Verify emails are delivered to recipients
- Check email formatting and content
Step 2: Test error handling
- Invalid email addresses: Test with malformed email addresses
- Rate limiting: Test with rapid successive requests
- Authentication failures: Test with invalid API keys
- Network issues: Test error handling for API failures
Step 3: Performance testing
- Response time: Measure email sending response times
- Concurrent requests: Test multiple simultaneous email sends
- Error rates: Monitor success and failure rates
Common use cases
- Welcome emails: Send onboarding emails to new users
- Password resets: Handle password reset email notifications
- Order confirmations: Send transactional emails for purchases
- Notification emails: Send alerts and updates to users
- Contact forms: Handle form submissions with email notifications
Email templates and customization
Using HTML templates
Create dynamic email templates with placeholders:
Template variables
Replace placeholders with dynamic content:
Troubleshooting
Common issues and solutions
- Email delivery failures: Check API key validity and domain configuration
- Rate limiting: Implement proper request throttling
- Template rendering: Verify template syntax and variable substitution
- Authentication errors: Ensure API key is correctly configured as a secret
Error handling best practices
- Detailed logging: Log all email sending attempts and results
- Retry mechanisms: Implement retry logic for temporary failures
- Efficient degradation: Handle API downtime appropriately
- User feedback: Provide clear status messages to users
Security considerations
API key management
- Use secrets: Always store API keys as Azion secrets
- Rotate regularly: Periodically rotate your API keys
- Monitor usage: Track API key usage and access patterns
- Limit permissions: Use API keys with minimal required permissions
Input validation
- Email validation: Validate email addresses before sending
- Content sanitization: Clean and validate email content
- Rate limiting: Implement rate limiting to prevent abuse
- Authentication: Add proper authentication to your email endpoints
Advanced features
Batch email sending
Implement batch email functionality for newsletter and marketing campaigns:
Email tracking
Add tracking capabilities to monitor email engagement:
- Open tracking: Track when emails are opened
- Click tracking: Monitor link clicks in emails
- Bounce handling: Handle bounced emails appropriately
- Unsubscribe management: Process unsubscribe requests
Next steps
- Explore advanced email templating techniques
- Implement email analytics and tracking
- Add email scheduling and delayed sending
- Integrate with other Azion products like Object Storage
- Monitor email delivery performance and optimize accordingly
Resend API Documentation Learn more about Functions