SendGrid
Overview
SendGrid is a cloud-based email delivery platform that enables your Emergent app to send transactional and marketing emails reliably. Common use cases include password resets, order confirmations, notifications, and automated email campaigns.
This guide shows you how to integrate SendGrid with your Emergent app using environment variables and the SendGrid API.
Note
SendGrid offers a free tier with 100 emails per day, which is sufficient for testing and small applications. Production apps typically require a paid plan.
Prerequisites
Before you begin, you'll need:
- A SendGrid account (sign up)
- An active Emergent project
- Basic familiarity with environment variables
Setting up SendGrid
Create a SendGrid API key
- Log in to your SendGrid dashboard
- Navigate to Settings → API Keys
- Click Create API Key
- Choose Restricted Access and enable Mail Send permissions (recommended for security)
- Name your key (e.g.,
)emergent-production - Copy the generated API key immediately - it won't be shown again
Verify a sender identity
SendGrid requires you to verify the email address or domain you'll send from:
- Go to Settings → Sender Authentication
- For testing: verify a Single Sender email address
- For production: set up Domain Authentication to improve deliverability
Check your inbox for the verification email and follow the link.
Add the API key to your Emergent app
In the Emergent chat, add your SendGrid API key as an environment variable:
Add environment variable
with valueSENDGRID_API_KEYSG.xxxxx...
Mark this variable as secret to prevent it from appearing in logs or client-side code.
Configure sender details
Store your verified sender email as an environment variable for easy reuse:
Add environment variable
with valueSENDGRID_FROM_EMAILnoreply@yourdomain.com
Optionally add
SENDGRID_FROM_NAME for a friendly sender name.Sending emails from your app
Once configured, you can send emails using the SendGrid Node.js SDK or REST API. Here's how to implement common email scenarios:
Transactional emails
(The agent writes and wires up this code for you.)
(The agent writes and wires up this code for you.)
Using templates
SendGrid's Dynamic Templates let you design emails visually and populate them with data:
(The agent writes and wires up this code for you.)
Tip
Create templates in the SendGrid dashboard under Email API → Dynamic Templates. Copy the template ID (starts with
d-) and pass it in your code.Testing email delivery
Test mode
During development, consider these strategies to avoid sending real emails:
SendGrid's sandbox mode validates requests without sending actual emails:
(The agent writes and wires up this code for you.)
Send all development emails to a test address:
(The agent writes and wires up this code for you.)
Monitoring delivery
Track email activity in the SendGrid dashboard:
- Go to Activity to see recent sends, bounces, and opens
- Check Suppressions for bounced or unsubscribed addresses
- Review Statistics for delivery rates and engagement metrics
Warning
High bounce rates can damage your sender reputation. Regularly clean your email list and honor unsubscribe requests.
Common patterns in Emergent apps
Scheduled notifications
Use SendGrid's
sendAt parameter to schedule emails for a future time (up to 72 hours):
(The agent writes and wires up this code for you.)
Batch sending
Send the same email to multiple recipients efficiently:
(The agent writes and wires up this code for you.)
Info
For large batches (1000+ recipients), consider using SendGrid's batch ID feature to cancel or pause sends if needed.
Troubleshooting
- Verify your sender identity in the SendGrid dashboard
- Check the Activity feed for delivery status and errors
- Ensure
is set correctly and has Mail Send permissionsSENDGRID_API_KEY - Look for emails in spam/junk folders (improve this with domain authentication)
Your API key likely lacks the necessary permissions. Create a new key with Mail Send enabled under Restricted Access.
Free accounts are limited to 100 emails per day. Upgrade your SendGrid plan or implement retry logic with exponential backoff for production apps.
Include an unsubscribe link in marketing emails to comply with regulations:
(The agent writes and wires up this code for you.)
SendGrid automatically adds an unsubscribe footer when enabled.
Best practices
Use templates
Design emails visually in SendGrid and keep HTML out of your codebase. Templates are easier to update and A/B test.
Authenticate your domain
Set up SPF, DKIM, and DMARC records to improve deliverability and avoid the spam folder.
Handle errors gracefully
Wrap
sgMail.send() in try-catch blocks and log failures for investigation. Don't block user workflows on email delivery.Monitor suppressions
Regularly check bounced and unsubscribed addresses. Remove them from your database to maintain a healthy sender reputation.

