If you have an issue logging into your Twilio SendGrid account, please read this document. For any questions regarding login issues, please contact our support team.
If you have a non-library SendGrid issue, please contact our support team.
If you can't find a solution below, please open an issue.
- Migrating from Web API v2 to Web API v3
- Continue Using Web API v2
- Testing Web API v3 /mail/send Calls Directly
- Error Messages
- Versions
- Environment Variables and Your SendGrid API Key
- Using the Package Manager
- Viewing the Request Body
- Wrapping Text
- Verifying Event Webhooks
Please review our guide on how to migrate from v2 to v3.
Here is the last working version with v2 support.
The following recommended installation requires npm. If you are unfamiliar with npm, see the npm docs. Npm comes installed with Node.js since node version 0.8.x, therefore, you likely already have it.
Add the following to your package.json
file:
{
...
"dependencies": {
...
"sendgrid": "1.9.2"
}
}
Install sendgrid-nodejs and its dependencies:
npm install
You can also install sendgrid locally with the following command:
npm install sendgrid@1.9.2
Download:
Click the "Clone or download" green button in GitHub and choose download.
Here are some cURL examples for everyday use cases.
To read the error message returned by SendGrid's API, please see this example.
We follow the MAJOR.MINOR.PATCH versioning scheme as described by SemVer.org. Therefore, we recommend that you always pin (or vendor) the particular version you are utilizing with your code and never auto-update to the latest version. Especially when there is a MAJOR point release since that is guaranteed to be a breaking change. Changes are documented in the CHANGELOG and releases section.
All of our examples assume you are using environment variables to hold your SendGrid API key.
If you choose to add your SendGrid API key directly (not recommended):
process.env.SENDGRID_API_KEY
becomes
'SENDGRID_API_KEY'
In the first case, SENDGRID_API_KEY is in reference to the name of the environment variable, while the second case references the actual SendGrid API Key.
If you're using Kubernetes Secrets and passing the API Keys to the Environment using it, You may find that there is a \n
character in the environment variable. You can use the trim function to remove it like this:
process.env.SENDGRID_API_KEY.trim();
We upload this library to npm whenever we make a release. This allows you to use npm for easy installation.
In most cases we recommend you download the latest version of the library, but if you need a different version, please use:
npm install @sendgrid/[package name]@X.X.X
Please check here for a list of package names.
If you are using a package.json
file:
{
...
"dependencies": {
...
"@sendgrid/[package name]": "X.X.X"
}
}
When debugging or testing, it may be useful to examine the raw request body to compare against the documented format.
You can do this right before you call sgMail.send(msg);
like so:
const {
classes: {
Mail,
},
} = require('@sendgrid/helpers');
const mail = Mail.create(data);
const body = mail.toJSON();
console.log(JSON.stringify(body));
You can write blog posts using e-mail with the help of SendGrid API, like so:
sgMail.setApiKey(process.env.SendGrid_API_KEY);
let msg = {
to: '<your-name>@blogger.com',
from: '<your-name>@gmail.com',
subject: title,
html: html,
};
sgMail.send(msg);
You can also wrap the text in the HTML to make a multi-line blog post:
<div style="white-space: pre-wrap;">
<code>
int a = 10;
int b = 10;
int d = 10;
</code>
</div>
Twilio SendGrid's Event Webhook will notify a URL via HTTP POST with information about events that occur as your mail is processed. This article covers all you need to know to secure the Event Webhook, allowing you to verify that incoming requests originate from Twilio SendGrid. The sendgrid-nodejs library can help you verify these Signed Event Webhooks.
You can find the end-to-end usage example here and the tests here. If you are still having trouble getting the validation to work, follow the following instructions:
- Be sure to use the raw payload for validation
- Be sure to include a trailing carriage return and newline in your payload
- In case of multi-event webhooks, make sure you include the trailing newline and carriage return after each event