Today we are thrilled to welcome the SendGrid API to the RapidAPI marketplace! The Colorado-based user communications platform is responsible for sending billions of emails for some of the best and brightest companies in the world, and is now offering its best in class email API to the RapidAPI developer community.
“SendGrid is extremely excited to be partnering with RapidAPI. RapidAPI’s Marketplace is focused on providing better access to, and simplifying the developer’s experience with APIs. As a developer-first company, we believe our partnership will create a seamless experience for our community.” Elliot Goldwater, Director of Business Developement @SendGrid.
With the SendGrid API now available on the RapidAPI marketplace, sending emails programmatically has never been easier. In this post we will highlight the different functionality available via the SendGrid API, and take you through the steps of testing, connecting and subscribing to the API via RapidAPI.
What Is Possible with the SendGrid API?
SendGrid has built a premiere cloud-based email service that reliably delivers emails for companies of all sizes. No matter the size of your project or company, the SendGrid API will enable you to simplify the process of programmatically sending your emails. Beyond the ability to programmatically send emails, the SendGrid API provides features like unsubscribe tracking, reputation monitoring, and an Inbound Parse Webhook.
How to Use the SendGrid API
The best way to get started is to head straight to the pricing tab for the SendGrid API. Before making any requests to the API, you’ll first need to subscribe to an API pricing plan. There are four different tiers available, so choose the plan that best suits your needs. And if you need a more customized pricing solution, just reach out to us at support@rapidapi.com and we would be happy to help!
Once subscribed to an API plan, go to the functions page, where you’ll find all of the endpoints available to you, along with code snippets to help you get started.
How Do I Find my SendGrid API Key?
With RapidAPI, you don’t need a SendGrid API Key. Once you’re subscribed to a pricing plan, you will use your RapidAPI API key to make your requests to the SendGrid API. Your RapidAPI key is provided with every code snippet as the “X-Mashape-Key” and can also be found in your RapidAPI Dashboard under the “Security” tab of your application.
List of SendGrid API Endpoints
- Blocks
- Bounces
- Invalid Emails
- Spam Reports
- Cancel Scheduled Sends
- Unsubscribe Group
- Suppressions (Unsubscribe)
- Settings – Tracking
- Settings – Mail
The first endpoint we’ll cover is the Send Mail endpoint.
How to Send Email with the SendGrid API
With the Send Mail endpoint, you need to provide a request payload JSON object. The JSON email object tells the SendGrid API where to send the email, what the content of the email is, which unsubscribe group to use, and much more. The most basic email object you can send is one that includes: to email address, from email address, and plain text content for the email.
{ "personalizations": [ { "to": [ { "email": "my_to_email@rapidapi.com" } ], "subject": "Hello, World!" } ], "from": { "email": "my_from_email@rapidapi.com" }, "content": [ { "type": "text/plain", "value": "Hello, World!" } ] }
My programming language of choice is NodeJS, so that’s what I’ll be using in this tutorial, but you can use any of the SDKs we support to do the same functionality.
SendGrid API – SDKs Supported:
- NodeJS
- PHP
- Python
- Ruby
- Objective-C
- Java
- C#
- cURL
To start using the API, copy the code snippet from RapidAPI into an index.js file and install the unirest npm module.
const unirest = require(‘unirest’) unirest.post("https://rapidprod-sendgrid-v1.p.mashape.com/mail/send") .header("X-Mashape-Key", "********************************************") .header("Content-Type", "application/json") .send({"personalizations":[{"to":[{"email":"my_to_email@rapidapi.com"}],"subject":"Hello, World!"}],"from":{"email":"my_from_email@rapidapi.com"},"content":[{"type":"text/plain","value":"Hello, World!"}]}) .end(function (result) { console.log(result.status, result.headers, result.body); });
Running the program will send an email to “my_to_email@rapidapi.com” with the subject “Hello, World!” and a plain text body of “Hello, World!” This is all it takes to send an email using the SendGrid API and RapidAPI! Easy!
Sending a simple text email is great, but there are other amazing ways to improve your emails with the SendGrid API which we’ll cover next. The first step would be separating the JSON object out into its own variable to make it easier to manipulate.
const unirest = require(‘unirest’) let emailObject = { "personalizations":[ { "to":[ { "email":"my_to_email@rapidapi.com" } ], "subject":"Hello, World!" } ], "from":{ "email":"my_from_email@rapidapi.com" }, "content":[ { "type":"text/plain", "value":"Hello, World!" } ] }; unirest.post("https://rapidprod-sendgrid-v1.p.mashape.com/mail/send") .header("X-Mashape-Key", "********************************************") .header("Content-Type", "application/json") .send(emailObject) .end(function (result) { console.log(result.status, result.headers, result.body); });
How to Send an HTML Email with the SendGrid API
The next improvement is to use an HTML email instead of a plain text email. To do this, I’m going to use the Responsive HTML Email Template created by leemunroe. To do so, download the email.html file from the GitHub repo into your projects directory, then load it into your index.js file by using the fs node module. Once the HTML file is loaded in, change the content value of your emailObject to use the new emailHTML variable:
const unirest = require(‘unirest’); const fs = require('fs'); let emailHTML = fs.readFileSync(email.html', 'utf8'); let emailObject = { "personalizations":[ { "to":[ { "email":"my_to_email@rapidapi.com" } ], "subject":"Hello, World!" } ], "from":{ "email":"my_from_email@rapidapi.com" }, "content":[ { "type": "text/html", "value": emailHTML } ] } unirest.post("https://rapidprod-sendgrid-v1.p.mashape.com/mail/send") .header("X-Mashape-Key", "********************************************") .header("Content-Type", "application/json") .send(emailObject) .end(function (result) { console.log(result.status, result.headers, result.body); });
SendGrid Unsubscribe Groups
The last feature of the SendGrid API I want to show off is the unsubscribe groups functionality. To start, you’ll first need to create a new unsubscribe group. You can do this by using the Create a new suppression group endpoint. Simply call the endpoint through the browser, a cURL command, or in the programming language of your choice.
By calling the endpoint, the API will create a new unsubscribe group for your account and the API response will include the id of your new group. Make sure to copy the unsubscribe group id into your application so you can use it later. If you forget to copy the unsubscribe group id into your application or accidentally delete it, use the Retrieve all suppression groups associated with the user endpoint to locate the id once again.
You need to add this new unsubscribe group id to your emailObject using the “asm” value. Your code should now look something like this.
const unirest = require(‘unirest’); const fs = require('fs'); let asmGroupId = 78227; let emailHTML = fs.readFileSync(email.html', 'utf8'); let emailObject = { "personalizations":[ { "to":[ { "email":"my_to_email@rapidapi.com" } ], "subject":"Hello, World!" } ], "from":{ "email":"my_from_email@rapidapi.com" }, "content":[ { "type": "text/html", "value": emailHTML } ], "asm": { "group_id": asmGroupId } } unirest.post("https://rapidprod-sendgrid-v1.p.mashape.com/mail/send") .header("X-Mashape-Key", "********************************************") .header("Content-Type", "application/json") .send(emailObject) .end(function (result) { console.log(result.status, result.headers, result.body); });
When you provide an asm group in your email request, SendGrid automatically appends unsubscribe links to the end of your email.
If you want to customize where the unsubscribe link is located, use the <%asm_group_unsubscribe_raw_url%> tag as the href link in your HTML file.
<!-- START FOOTER --> <div class="footer"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td class="content-block"> <span class="apple-link">RapidAPI, 164 Townsend St. Apt. B, San Francisco CA 94102</span> <br> Don't like these emails? <a href="<%asm_group_unsubscribe_raw_url%>">Unsubscribe</a>. </td> </tr> </table> </div> <!-- END FOOTER -->
SendGrid automatically populates this tag with the unsubscribe URL for the asm group provided so your email recipients can unsubscribe with ease!
Conclusion
As you can see, there’s a lot you can do with the SendGrid API, and we couldn’t be more excited to have it now live and available to our developer community on RapidAPI. We’d love to learn about your use case and how you’ve been using the API so please don’t hesitate to shoot us a note at community@rapidapi.com!
Read more SendGrid API Documentation.
Scott says
Thank you this helped me !
RapidAPI Staff says
No problem Scott.
Glad we could help!