Back

SendGrid API Essential Guide

Aug 12, 20246 minute read

What type of API does SendGrid provide?

REST API

  • SendGrid's main API is a REST API, which they refer to as their Web API.

  • The Web API allows you to send emails via HTTP requests.

  • It is faster and more efficient than SMTP, especially for customers outside the US.

  • Provides additional security features like API keys for authentication.

SMTP Relay

  • SendGrid also offers SMTP relay as an alternative sending method.

  • SMTP is a standard email protocol that can be used with existing applications like CRM systems or mail clients.

  • However, SMTP requires more back-and-forth communication and can be slower than the REST API.

Key Considerations

  • SendGrid recommends using the Web API (REST) when possible for better performance and security.

  • The REST API is often preferred by developers building custom applications.

  • SMTP may be necessary for integrating with some existing systems.

In summary, while SendGrid supports both REST and SMTP, their primary and recommended API is the REST-based Web API. They do not appear to offer GraphQL or SOAP APIs for email sending.

Does the SendGrid API have webhooks?

Yes, the official SendGrid API does have webhooks. Here are the key points about SendGrid's Event Webhook:

Event Types

SendGrid's Event Webhook allows you to subscribe to the following types of events:

  • Delivery events:

    • Processed
    • Dropped
    • Delivered
    • Deferred
    • Bounce
  • Engagement events:

    • Open
    • Click
    • Spam Report
    • Unsubscribe
    • Group Unsubscribe
    • Group Resubscribe
  • Account events:

    • Account status change (e.g. compliance suspend, reactivate, etc.)

Key Features

  • Near real-time data delivery - events are posted within 30 seconds or when a batch reaches 768 KB
  • Ability to send data to multiple endpoints
  • Security features like signature verification and OAuth
  • Retry mechanism - SendGrid will retry sending event data for 24 hours if your endpoint is down
  • Customizable - you can choose which event types to receive

Benefits

  • Get real-time insights into email performance and recipient engagement
  • Enable data-driven decision making to improve email campaigns
  • Back up email event data to your own systems
  • Give different internal teams access to relevant event data

Implementation

To use the Event Webhook, you need to:

  1. Set up an endpoint on your server to receive the webhook POST requests
  2. Configure the webhook in your SendGrid account, specifying the endpoint URL and which events to receive
  3. Implement logic to process the event data received by your endpoint

SendGrid provides detailed documentation and code examples to help implement the Event Webhook.

In summary, the SendGrid Event Webhook is a powerful tool that provides comprehensive visibility into email delivery and engagement, enabling you to optimize your email programs and react quickly to important events.

Rate Limits and other limitations

Here are the key points about the API rate limits for SendGrid:

Default Rate Limit

  • SendGrid has a default rate limit of 600 requests per minute for most API endpoints [1][2].

Mail Send Endpoint

  • The v2 and v3 mail/send endpoints do not have a rate limit [1][3].

  • With the v3 mail/send endpoint, you can send up to 1000 email addresses per API call [4].

Rate Limit Response

  • If you exceed the rate limit, you'll receive a 429 "Too Many Requests" error response [1].

  • The response will include headers like:

    • X-RateLimit-Limit: The rate limit ceiling
    • X-RateLimit-Remaining: The number of requests left for the time window
    • X-RateLimit-Reset: The remaining window before the rate limit resets [2]

Key Points to Consider

  • Rate limits are per API endpoint, not per account [2].

  • The mail/send endpoint does not return rate limit headers [3].

  • For high volume sending, it's recommended to spread out large batches over time to build up sending reputation [4].

  • Authentication via API keys is preferred over username/password [1].

Best Practices

  • Monitor rate limit headers and pause sending if approaching the limit.

  • Use API keys with appropriate scoped permissions.

  • For large sends, break into smaller batches of 1000 recipients or less.

  • Warm up new sending domains/IPs gradually to establish reputation.

In summary, while SendGrid has a general 600 requests/minute limit, the critical mail/send endpoint is not rate limited. However, best practices should still be followed for optimal deliverability.

Latest API Version

The most recent version of the SendGrid API is v3. Here are the key points:

  1. The v3 Mail Send endpoint is the latest version of SendGrid's Web API for sending email.

  2. The v3 API is a RESTful API that allows you to send email by making an HTTP POST request to https://api.sendgrid.com/v3/mail/send.

  3. SendGrid recommends using the v3 API to access all the latest features and upcoming developments.

  4. The v3 API replaced the older v2 API, which is still available but no longer being actively developed.

  5. SendGrid provides client libraries for many programming languages to easily integrate with the v3 API, including Ruby, Python, Node.js, C#, PHP, Go, and Java.

  6. The current version of the SendGrid Python library that supports the v3 API is 6.11.0, released on December 1, 2023.

Key considerations:

  • If you're currently using the v2 API, SendGrid recommends migrating to v3 to access the latest features.
  • The v3 API uses JSON payloads for requests and responses, unlike the v2 API which used form-encoded data.
  • SendGrid provides extensive documentation and code examples for using the v3 API with various programming languages.

Best practices:

  • Use the official SendGrid client libraries when possible for easier integration.
  • Store your SendGrid API key securely as an environment variable rather than hardcoding it.
  • Familiarize yourself with the v3 API documentation to take full advantage of its capabilities.

How to get a SendGrid developer account and API Keys?

Create a SendGrid Account

  1. Go to the SendGrid website and sign up for a new account.

  2. Choose a plan that fits your needs - SendGrid offers options for startups, enterprises, high volume senders, marketers, ecommerce businesses, and developers.

Generate an API Key

  1. Log into your SendGrid account.

  2. From the left side menu, click on "Settings", then on "API Keys".

  3. Click the "Create API Key" button on the top-right of the page.

  4. In the "API Key Name" field, enter a name for your API key (e.g., "My Integration").

  5. For "API Key Permissions", select either "Full Access" or "Restricted Access". If you choose restricted access, ensure the key has access to the "Mail Send" option.

  6. Click the "Create & View" button.

  7. You will be presented with your SendGrid API key. Make sure to copy and save this key securely, as SendGrid does not allow the key value to be displayed for existing API keys.

What can you do with the SendGrid API?

Based on the search results provided, here is a list of data models you can interact with using the SendGrid API, along with what is possible for each:

Email

  • Send transactional emails
  • Test Dynamic Templates for inbox rendering, link validity, and performance against spam filters
  • Generate X-SMTPAPI headers

Marketing Email

  • View and manage SendGrid reports
  • Access Marketing Email API functionality
  • Create and manage bulk messaging campaigns

Events and Tracking

  • Access event data about sent emails (e.g., bounces, clicks)
  • Handle inbound batches from the Event API
  • Simulate events for QA or development environments

Contacts and Lists

  • Manage contact lists and subscriptions
  • Create and manage unsubscribe groups

Templates

  • Create and manage email templates
  • Work with Dynamic Templates

Subusers

  • Manage SendGrid subuser accounts

Parse API

  • Receive and process inbound emails
  • Set up endpoints for the Parse API

Webhooks

  • Configure and manage Event Webhook for outbound email data
  • Set up Inbound Parse Webhook for receiving emails

API Keys and Authentication

  • Generate and manage API keys
  • Implement authentication for API requests

Statistics and Reporting

  • Access and analyze email sending statistics
  • Generate custom reports

Settings

  • Configure domain authentication
  • Manage IP addresses and pools
  • Set up and manage sender identities

This list covers the main data models and functionalities available through the SendGrid API based on the provided search results. The API allows for comprehensive management of email sending, tracking, and related features across various aspects of the SendGrid platform.