#API SendGrid: Email delivery automation API
An overview of SendGrid API, introduction to the endpoints in it and execution example for it
What does SendGrid API do?
SendGrid API is an email delivery platform that enables businesses, marketers, and developers to send large volumes of email effectively and efficiently. It provides APIs, open rate tracking, click tracking, segmentation tools, and analytics for knowing user behaviour insights better.
With SendGrid, users can send and receive emails through their accounts, utilizing its RESTful web-based API and to integrate with third-party applications. This helps create a more engaging customer experience with faster delivery times. The host API key can be received when the user signs up on the portal.
Overview of endpoints present in the SendGrid API
Update an alert: Based on the unique ID for the alert, information about a particular email alert can be updated using this endpoint.
Retrieve all alerts: This endpoint allows for fetching all the alerts
Send mail: This allows the user to send email using SendGrid’s latest Web API.
Blocks: Blocks are the instances when a message that is sent, gets rejected owing to the constitution of the message and not the recipient details. Based on the email address of the recipient, a specific or all blocks related to set of given parameters can be fetched using the GET call. DELETE can be used to delete a specific email address from the block list of the user.
Bounce: The Bounce endpoint is useful to delete or retrieve a specific or a set of bounces adhering to the query parameters that can be set by the user. The bounced messages are received back to the servers that sent it, because it is undeliverable for reasons like incorrect email address format or non-existent email addresses.
Invalid Emails: This endpoint allows to track the invalid emails that were sent from the user’s server. An example of invalid email would be one with error in the domain name while adding it in the recipient space or missing an @ in the domain name. A specific email can be retrieved using the GET call by specifying the invalid entry or set of emails can be fetched using the query parameters. A specific invalid email can be removed from the directory using the DELETE call in this endpoint.
Spam Reports: Spam reports are instances that occur when the recipient marks the emails received from the sender’s email address as spams. Using this endpoint the set of such spam reports or a specific one can be fetched using the GET call or a spam report from a specific recipient can be removed using the DELETE call.
Cancel Scheduled emails: Using the POST call here, a unique batch ID in SMTPAPI header format can be generated that can be used to batch fetch, cancel, pause send, fetch a particular scheduled send email, update a user scheduled information in a batch or delete a particular scheduled send or cancel scheduled send batch of emails.
Unsubscribe groups: Each user can create up to 25 supression (Unsubscribe email) group. The groups can be a newsletter, an invoice, a system alert etc. The Product/Item Suggestion/ group ID belonging to set of emails in a suppression group string specifies this in the schema for this endpoint.
Suppressions (Unsubscribe): This endpoint allows to create a suppression group with an unique group_id, fetch information about it, delete such group or batch fetch a set of suppression groups as per the query parameters.
Setting – Tracking/ mail/ inbound parse: The setting-tracking and setting-mail endpoints are used to entirely or partially update a particular tracking or mail related settings for an email. The setting – inbound parse is used to parse the inbound emails in a particular format. It can be then used for POSTing via SendGrid to the desired URL.
Account stats: This endpoint is used to fetch the information about the email related statistics for emails for previous 7 days. There are different functions present under this endpoint for different statistics.
API Execution Example
Following is the example of executing an application function using Python,
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='sender@example.com',
to_emails='recipient@example.com',
subject='Test email',
html_content='<p>Hello, this is a test email!</p>')
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
The process
We first import the necessary modules from the SendGrid API library.
Then we create a Mail object with the sender email address with a valid domain name, recipient email with valid domain name, the subject, and message content to be sent.
We then initialize the SendGridAPIClient object validating the API key. The API key should be stored as an environment variable.
Finally, we call the send method of the sg object with the message object as a parameter.
This sends the email using the SendGrid API.
The details of the response status code, body, and headers for debugging purposes can be obtained using the print queries in the end.