Send email from DaloyJS with SendGrid
Twilio SendGrid is a long-standing email delivery service that combines transactional and marketing sending. This guide uses the official @sendgrid/mail SDK, which wraps the v3 Mail Send REST API.
- 01requestRoute handlersgMailsgMail.send({ from, to, subject, text })API key set once via sgMail.setApiKey()
- 02requestsgMailSendGrid v3 APIPOST /v3/mail/sendBearer token, JSON body
- 03responseSendGrid v3 APIRoute handler202 AcceptedX-Message-Id header returned as { id }
- 04asyncSendGrid v3 APIEvent Webhookdelivered, bounce, open eventsverify the signature before trusting payloads
1. Provision
- Create a SendGrid account, enable 2FA, then go to Settings → API Keys and generate a Restricted Access key with only Mail Send → Full Access enabled.
- Complete Domain Authentication (SPF/DKIM CNAMEs) for your sending domain, or use Single Sender Verification for quick tests only.
2. Install
3. Environment variables
4. Plugin
5. Use it in a route
Dynamic templates
For server-side templating, create a Dynamic Transactional Template in the SendGrid UI and pass its ID with substitution values:
Error handling
On non-2xx responses the SDK throws an error with response.body.errors describing each failure. Surface those to your client through the standard problem+json helper rather than echoing raw text.
Runtimes
The @sendgrid/mail package is Node-oriented (it uses @sendgrid/client with Node's HTTPS module). For Cloudflare Workers or Vercel, call the v3 REST API directly with fetch against https://api.sendgrid.com/v3/mail/send using the same JSON body and a Bearer token.
See also Resend, Postmark, and the email integrations overview.