SDKs overview

Twilio SendGrid provides a suite of official SDKs designed to simplify integration with its Email API. These SDKs abstract the underlying RESTful API calls, enabling developers to send emails, manage contacts, and access analytics using familiar language constructs. The official libraries are maintained by Twilio SendGrid and are available for several popular programming languages, ensuring broad compatibility for a range of application environments. Additionally, a vibrant community contributes and maintains a variety of third-party libraries and extensions, offering specialized functionalities or support for other languages and frameworks.

The SDKs are built to interact with the SendGrid Web API, which handles email sending, email validation, and marketing campaign management. Developers can use the SDKs to programmatically perform tasks such as sending a single email, dispatching bulk emails, managing suppression lists, or retrieving delivery statistics. Each SDK is tailored to follow the best practices and conventions of its respective language ecosystem, aiming to provide a consistent and intuitive developer experience.

Official SDKs by language

Twilio SendGrid offers officially supported SDKs for the following programming languages. These libraries are actively maintained and provide comprehensive access to the SendGrid Email API functionality. Each SDK is designed to be idiomatic to its language, offering a natural way to interact with the API.

Language Package Name Install Command Maturity
Python sendgrid pip install sendgrid Stable
Ruby sendgrid-ruby gem install sendgrid-ruby Stable
PHP sendgrid/sendgrid composer require sendgrid/sendgrid Stable
Node.js @sendgrid/mail npm install @sendgrid/mail Stable
Java com.sendgrid:sendgrid-java Add to pom.xml or build.gradle Stable
C# SendGrid dotnet add package SendGrid Stable
Go github.com/sendgrid/sendgrid-go go get github.com/sendgrid/sendgrid-go Stable

For detailed instructions and specific version requirements, refer to the Twilio SendGrid API Getting Started documentation.

Installation

Installation procedures for the official Twilio SendGrid SDKs typically follow the standard package management practices for each respective language. Below are generalized command-line instructions for the core supported languages. Before installing, ensure you have the appropriate package manager (e.g., pip for Python, npm for Node.js, Composer for PHP) configured in your development environment.

Python

pip install sendgrid

This command installs the latest stable version of the Python SDK, enabling access to the SendGrid API methods. Further details on Python SDK usage are available in the Twilio SendGrid Python examples.

Node.js

npm install @sendgrid/mail

The Node.js SDK, available via npm, provides an asynchronous interface for interacting with the SendGrid API. For more information, consult the Node.js API example documentation.

PHP

composer require sendgrid/sendgrid

PHP developers use Composer to include the SendGrid SDK in their projects. This integrates the library for sending emails and managing other SendGrid resources. The official PHP example guide offers comprehensive usage instructions.

Java

For Java projects, you typically add the SendGrid Java library as a dependency in your build configuration file. For Maven, add the following to your pom.xml:

<dependency>
    <groupId>com.sendgrid</groupId>
    <artifactId>sendgrid-java</artifactId>
    <version>4.10.0</version> <!-- Use the latest version -->
</dependency>

For Gradle, add to your build.gradle:

implementation 'com.sendgrid:sendgrid-java:4.10.0' // Use the latest version

Refer to the Java integration guide for specific versioning and usage patterns.

C# (.NET)

dotnet add package SendGrid

The SendGrid C# SDK is distributed via NuGet. The command above adds the package to your .NET project. Detailed examples are available in the C# API integration documentation.

Go

go get github.com/sendgrid/sendgrid-go

Go developers can fetch the SendGrid Go library using the go get command. Usage instructions and further examples are provided in the Go API examples section.

Ruby

gem install sendgrid-ruby

The Ruby SDK is installed as a gem, providing a Ruby-centric interface for SendGrid functionality. For Ruby-specific usage patterns, refer to the Ruby API integration guide.

Quickstart example

The following example demonstrates how to send a simple email using the Twilio SendGrid Python SDK. This quickstart assumes you have installed the Python SDK and have a SendGrid API Key with Mail Send permissions.

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='[email protected]',
    to_emails='[email protected]',
    subject='Sending with Twilio SendGrid is Fun',
    plain_text_content='and easy to do anywhere, even with Python',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e)

Before running this code, set your SendGrid API Key as an environment variable named SENDGRID_API_KEY. This practice is recommended for security reasons instead of hardcoding API keys directly into your application. Similar quickstart examples for other languages are available in the Twilio SendGrid developer documentation.

Community libraries

Beyond the official SDKs, the developer community has contributed various libraries and tools that integrate with Twilio SendGrid APIs. These community-maintained resources can offer support for additional programming languages or specialized functionalities not directly covered by the official SDKs. While not officially supported by Twilio SendGrid, they can be valuable for specific use cases or development environments. Developers should evaluate these libraries based on their project requirements, maintenance status, and community support.

  • Elixir: While an official Elixir SDK is not provided, libraries like swoosh (a popular email client for Elixir) offer adapters to send emails through SendGrid. Developers often use such generic email libraries that support various backend services, including SendGrid.
  • Ruby on Rails: For Ruby on Rails applications, the official sendgrid-ruby gem works seamlessly. Additionally, Rails' Action Mailer can be configured to use SendGrid as its delivery agent, integrating email sending directly into the framework's email system.
  • Framework-specific integrations: Many web frameworks across different languages have plugins or extensions that facilitate integration with email services like SendGrid. These might include packages for Django, Flask (Python), Laravel (PHP), or Spring Boot (Java), often available through their respective package managers or marketplaces.

When considering a community-contributed library, it's advisable to check its documentation, active development status, and issue tracker. Resources like open-source software repositories and community forums can provide insights into the reliability and long-term viability of these alternative integrations.