EMAIL / LESSON 06
Setting Up SendGrid.
Create a SendGrid account, make an API key, verify your sender and send your first email from Node.js

Loading the lesson…
Loading activities…
EMAIL / LESSON 06
Create a SendGrid account, make an API key, verify your sender and send your first email from Node.js

Loading the lesson…
Loading activities…
Time to get SendGrid working for real. By the end of this lesson you'll have an account, an API key tucked away safely, a verified sender and your first email sent from a few lines of Node.js.
Go through these in order. Every step leans on the one before it.
Head to twilio.com/en-us/sendgrid and sign up. New accounts start on a free trial, so you can follow along without paying anything.
SendGrid's official quickstart also asks you to turn on two factor authentication, which means signing in takes your password plus a code from your phone. Just do it now and get it over with.
An API key is the password your code uses to talk to SendGrid. To make one, go to Settings, then API Keys, and click Create API Key.
The safest habit is giving a key ONLY the access it needs. SendGrid's quickstart says to create a restricted key with Mail Send set to Full Access. On the API Keys page, that's the Custom Access option, where you pick access one area at a time. Your app can then send email and do nothing else. The other choices, Full Access for everything and Billing Access, are way more than a sending app needs.
Click to create it, and SendGrid shows you the key.
SendGrid's docs are pretty blunt about this one. Treat your API key like a password, and never put it in your code or commit it to a repository like GitHub.
Keep it in an environment variable instead. That's a named value living outside your code that your app reads when it runs. Here's the setup from the official @sendgrid/mail README, run inside your project folder.
Line by line, here's what that does.
sendgrid.env.gitignore so Git never uploads itSwap YOUR_API_KEY for the real key you copied. Your code will now find it as process.env.SENDGRID_API_KEY.
Before SendGrid sends a single thing, it needs to know you're allowed to use the "From" address. SendGrid calls this a Sender Identity, which is the address your recipients see as the sender. Skip this part and your first send fails with a 403 Forbidden error.
There are two ways to prove it.
This one's good for testing. Go to Settings, then Sender Authentication, and click Verify a Single Sender.
Fill out the form. It asks for a From Name, From Email Address, Reply To, your company address and a nickname. Click Create, then open the inbox for that From address and click the link SendGrid emails you. That address is now verified.
For real sending, you authenticate your whole domain, like yourapp.com. SendGrid's quickstart lists this as a required step, and its docs note that domain authentication is also required to upgrade from a free account.
You'll actually SEE a benefit too. Without it, some inboxes show "via sendgrid.net" next to your From address. Domain authentication gets rid of that tag and makes your mail look more trustworthy to inbox providers and to actual humans.
So how does it work? Email authentication runs three checks, and each one lives as a record in your domain's DNS settings.
SendGrid's docs also point out that big inbox providers like Gmail and Yahoo require DMARC policies for bulk senders. So this step matters more every single year.
To set it up, go to Settings, then Sender Authentication, and start the domain authentication flow.
The records SendGrid hands you depend on your settings. With its default automated security turned on, you get CNAME records that cover SPF and DKIM plus a TXT record for DMARC. Copy them EXACTLY as shown.
Side note, I got tired of squinting at DNS records by hand, so one of the free tools I put out is an email deliverability checker that looks at a domain's SPF, DKIM and DMARC for you. Running your domain through something like that after you add the records is a nice sanity check.
Key stored, sender verified. You're ready to send.
In your Node.js project folder, install the official mail package.
Create a file called index.js and paste in this example from SendGrid's official Node.js quickstart.
Here's what each piece is doing.
require('@sendgrid/mail') loads the librarysetApiKey hands it your key, read from the environment variable and never typed into the filemsg is the email itself. to is who gets it, from has to be your verified sender, and subject is the subject linetext and html are two versions of the body. Email apps that can show formatting use the HTML version, and the rest fall back to plain textsend sends it, then logs the result or the errorChange to to your own email address and from to the sender you verified in Step 4 or Step 5.
In the same terminal where you loaded your key, run the file.
If it worked, you'll see 202 printed first. That status code means SendGrid accepted your email for delivery. Go check your inbox, and peek in your spam folder too just in case.
@sendgrid/mail, call setApiKey, then send a message with to, from, subject, text and html202 means SendGrid accepted your email, and a 403 usually means your sender isn't verified