5.7 KiB
SendGrid Email Notifications Setup
This document explains how to set up and configure SendGrid for email notifications in the Duxiter backend.
Overview
The notification system automatically sends email alerts when risk changes are detected during monitoring. It uses SendGrid as the email service provider.
Features
- Risk Change Notifications: Automatic email alerts when company risk levels change
- Tenant Isolation: Notifications are sent only to users within the same tenant
- Professional Email Templates: HTML and text email templates with company branding
- Retry Mechanism: Failed notifications are tracked and can be retried
- Configuration Testing: Test endpoint to verify SendGrid setup
- Notification History: Track all sent and pending notifications
Setup Instructions
1. Create SendGrid Account
- Go to SendGrid and create an account
- Verify your email address
- Complete the account setup process
2. Generate API Key
- Log in to your SendGrid dashboard
- Go to Settings > API Keys
- Click Create API Key
- Choose Restricted Access and configure the following permissions:
- Mail Send: Full Access
- Mail Settings: Read Access (optional)
- Tracking: Read Access (optional)
- Copy the generated API key (you won't be able to see it again)
3. Configure Environment Variables
Update your .env file with the following variables:
# SendGrid Configuration
SENDGRID_API_KEY=your_actual_sendgrid_api_key_here
SENDGRID_FROM_EMAIL=notifications@yourdomain.com
SENDGRID_FROM_NAME=Duxiter Notifications
Important Notes:
- Replace
your_actual_sendgrid_api_key_herewith your actual SendGrid API key - Use a verified sender email address (see Domain Authentication below)
- The
FROM_NAMEwill appear as the sender name in emails
4. Domain Authentication (Recommended)
For production use, set up domain authentication:
- In SendGrid dashboard, go to Settings > Sender Authentication
- Click Authenticate Your Domain
- Follow the instructions to add DNS records to your domain
- Use an email address from your authenticated domain as
SENDGRID_FROM_EMAIL
5. Test Configuration
Use the test endpoint to verify your setup:
POST /api/notifications/test
Content-Type: application/json
Authorization: Bearer <admin_token>
{
"email": "test@example.com"
}
API Endpoints
Test Configuration
POST /api/notifications/test
Sends a test email to verify SendGrid configuration.
Get Pending Notifications
GET /api/notifications/pending?limit=50&skip=0
Retrieve notifications that haven't been sent yet.
Get Notification History
GET /api/notifications/history?limit=50&skip=0&sent=true
Retrieve notification history with optional filtering.
Process Pending Notifications
POST /api/notifications/process-pending
Manually trigger processing of pending notifications.
Get Notification Statistics
GET /api/notifications/stats
Get statistics about notifications for the tenant.
How It Works
Automatic Notifications
- Risk Monitoring: The monitoring service runs scheduled checks on companies
- Change Detection: When a risk level change is detected, a
RiskChangeNotificationrecord is created - Email Sending: The notification service automatically sends emails to all active users in the tenant
- Status Tracking: The system tracks whether notifications were sent successfully
Email Template
The system generates professional HTML emails with:
- Company name and RUT
- Previous and new risk levels with color coding
- Detection date and time
- Professional styling with Duxiter branding
Error Handling
- Failed email attempts are logged with error messages
- Notifications remain in "pending" status if sending fails
- Manual retry is available through the API
- Multiple recipients per notification are supported
Monitoring and Troubleshooting
Check Notification Status
# Get notification statistics
GET /api/notifications/stats
# Check pending notifications
GET /api/notifications/pending
# View notification history
GET /api/notifications/history
Common Issues
-
"SendGrid not initialized" warnings
- Check that
SENDGRID_API_KEYis set correctly - Ensure the API key has proper permissions
- Check that
-
Emails not being delivered
- Verify sender email is authenticated
- Check SendGrid dashboard for delivery statistics
- Ensure recipient emails are valid
-
"Failed to send" errors
- Check SendGrid API key permissions
- Verify account is not suspended
- Check rate limits
Logs
The system logs notification activities:
[NotificationService] SendGrid initialized successfully
[NotificationService] Risk change notification sent to user@example.com for RUT 12345678-9
[MonitoringService] Risk change notifications sent successfully for RUT 12345678-9
Security Considerations
- API Key Security: Never commit API keys to version control
- Environment Variables: Store sensitive configuration in
.envfiles - Tenant Isolation: Notifications are automatically filtered by tenant
- Authentication: All notification endpoints require authentication
- Authorization: Test endpoint requires admin role
Production Deployment
- Set up domain authentication in SendGrid
- Use environment variables for configuration
- Monitor delivery rates in SendGrid dashboard
- Set up alerts for failed notifications
- Regularly check notification statistics
Support
For issues with:
- SendGrid Configuration: Check SendGrid documentation
- Email Delivery: Monitor SendGrid dashboard
- API Issues: Check application logs
- Feature Requests: Contact development team