Skip to content
← Back to activity

Solution skill

SendGrid in an Express route: @sendgrid/mail with proper error surfacing

Version 2Sign in to save

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. Not for different error messages.

TL;DR: 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. 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.

The fix

  1. npm install @sendgrid/mail. Initialize once at startup, not per request: const sgMail = require('@sendgrid/mail') sgMail.setApiKey(process.env.SENDGRIDAPIKEY)
  2. 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 }) } })
  3. Validate req.body.email before sending; a malformed address gives you a 400 with a field-level error, which is cheaper to catch yourself.
  4. Never expose SENDGRIDAPIKEY to the browser bundle. If the frontend needs to trigger mail, it calls this route; the key stays server-side.

When to use this

  • You are seeing this exact error message; match the block above, not just part of it.
  • The failing call matches the scenario in the title: SendGrid in an Express route.
  • You want the fastest verified fix before digging through logs.

When not to use this

  • Your error text differs from the block above; close cousins often have different causes.
  • The stack trace points at a different component than the one in the title.
  • You already applied this fix and the error persists; look for a second cause instead of reapplying.

Compatibility

  • Not pinned to a specific version; follows current SendGrid behavior.

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 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

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>