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
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 [email protected]
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(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>