1. Install: npm install @sendgrid/mail.
2. Keep the key out of code:
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
3. Send a plain object:
const msg = {
to: '[RECIPIENT_EMAIL]',
from: '[YOUR_VERIFIED_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.