Immutable solution version
SendGrid in an Express route: @sendgrid/mail with proper error surfacing
Complete solution
The standard Express pattern: initialize @sendgrid/mail once at startup, send inside an async route handler, and return SendGrid's real error body instead of a bare 500.
- npm install @sendgrid/mail. Initialize once at startup, not per request:
const sgMail = require('@sendgrid/mail') sgMail.setApiKey(process.env.SENDGRIDAPIKEY)
- Send inside the handler and surface real errors:
app.post('/invite', async (req, res) => { const msg = { to: req.body.email, from: '[YOURVERIFIEDSENDER]', // verified sender subject: 'Your invite', text: 'Welcome aboard', } try { await sgMail.send(msg) res.status(202).json({ sent: true }) } catch (error) { console.error(error.response && error.response.body) res.status(502).json({ sent: false, detail: error.response && error.response.body }) } })
- Validate req.body.email before sending; a malformed address gives you a 400 with a field-level error, which is cheaper to catch yourself.
- Never expose SENDGRIDAPIKEY to the browser bundle. If the frontend needs to trigger mail, it calls this route; the key stays server-side.
Evidence
Express has no mailer of its own; the standard pattern is @sendgrid/mail inside a route handler, with the key from the environment and error.response.body surfaced on failure, per the official Node quickstart. Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/quickstart-nodejs.md | https://github.com/sendgrid/sendgrid-nodejs
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Maintainer review
No maintainer verification is recorded for this version.
A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_C_BSlSnZoMBxHvWNgtspOA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">SendGrid in an Express route: @sendgrid/mail with proper error surfacing <span>— Vectle</span></a>