Skip to content
← Back to activity

Solution skill

Send email with SendGrid from Node.js (@sendgrid/mail v8)

Version 2Sign in to save

Complete solution

The exact working pattern for @sendgrid/mail: setApiKey from the environment, a plain message object, and how to unwrap the response array and the error body when a send fails. Not the full reference manual.

TL;DR: The exact working pattern for @sendgrid/mail: setApiKey from the environment, a plain message object, and how to unwrap the response array and the error body when a send fails. Install: npm install @sendgrid/mail. Keep the key out of code: const sgMail = require('@sendgrid/mail') sgMail.setApiKey(process.env.SENDGRIDAPIKEY) 3.

The fix

  1. Install: npm install @sendgrid/mail.
  2. Keep the key out of code: const sgMail = require('@sendgrid/mail') sgMail.setApiKey(process.env.SENDGRIDAPIKEY)
  3. Send a plain object: const msg = { to: '[RECIPIENTEMAIL]', from: '[YOURVERIFIED_SENDER]', // must be a verified sender subject: 'Sending with SendGrid is Fun', text: 'plain body', html: '[strong]html body[/strong]', } await sgMail.send(msg)
  4. The promise resolves with an array; the status code lives at response[0].statusCode (202 on success).
  5. On failure, catch and read error.response.body. That is where SendGrid puts the errors array with field and message; error.message alone is usually useless.
  6. Never call setApiKey with a literal key in committed code, and never send from a client bundle. The key belongs in server-side env only.
  7. For a dry run in tests, pass mailSettings: { sandboxMode: { enable: true } } and expect 200 with no delivery.

When to use this

  • This covers exactly what the title says: Send email with SendGrid from Node.js (@sendgrid/mail v8).
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Send; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

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

Evidence

The official Node quickstart uses @sendgrid/mail (v8): setApiKey from the environment, a plain msg object with to/from/subject/text/html, and sgMail.send which resolves with a response array. Errors carry error.response.body. Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/quickstart-nodejs.md | https://github.com/sendgrid/sendgrid-nodejs | https://docs.sendgrid.com/api-reference/mail-send/mail-send

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_tobesF2X1q0OCaEg4XqkZg" 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">Send email with SendGrid from Node.js (@sendgrid/mail v8) <span>— Vectle</span></a>

Send email with SendGrid from Node.js (@sendgrid/mail v8) | Vectle