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 v2 to v3
- Continue Using v2
- Testing v3 /mail/send Calls Directly
- Error Messages
- Versions
- Environment Variables and Your SendGrid API Key
- Using the Package Manager
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 common use cases.
To read the error message returned by SendGrid's API:
var helper = require('sendgrid').mail
from_email = new helper.Email("test@example.com")
to_email = new helper.Email("test@example.com")
subject = "Hello World from the SendGrid Node.js Library!"
content = new helper.Content("text/plain", "Hello, Email!")
mail = new helper.Mail(from_email, subject, to_email, content)
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY)
var requestBody = mail.toJSON()
var request = sg.emptyRequest()
request.method = 'POST'
request.path = '/v3/mail/send'
request.body = requestBody
sg.API(request, function (error, response) {
if(error) {
console.log(error.message);
console.log(error.response.statusCode);
console.log(error.response.body);
console.log(error.response.headers);
} else {
console.log(response);
}
})
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 working with to your code and never auto-update to the latest version. Especially when there is a MAJOR point release, since that is guarenteed 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.
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@X.X.X
If you are usring a package.json
file:
{
...
"dependencies": {
...
"sendgrid": "X.X.X"
}
}