Skip to main content

Prerequisites

Before getting started, you’ll need to:
  1. Authorize your sending domain
  2. Create and retrieve your SendLayer API key

Installation

Install the SendLayer Python SDK using pip.

Usage

After the installation completes, you’re ready to start integrating the SDK to your code. The SDK includes modules to send emails, manage webhooks and events.

Sending an email

Create a new .py file or edit an existing one. Then import the SendLayer module. Once done, you’ll need to initialize the module with your SendLayer API key.
send_email.py

Sending HTML emails

To send HTML emails, simply include the html parameter to the send() method.
send_email.py
You can include the text parameter to have a plain text and HTML version of your email message

Sending to multiple recipients

You can send emails to multiple recipients including "Cc" and "BCC" email addresses.
send_email.py
Each recipient field is an array containing the recipient’s name and email as objects.
There are limits to the number of email recipients you can add to a single request. See our rate limiting guide to learn more.

Including attachments

You can include attachments to your email message by adding the attachments parameter to the email payload.
send_email.py
The maximum email size SendLayer allows is 10MB. This includes both the message as well as attachment files.
Simply replace the path parameter with the path to the attachment file you wish to attach and specify the correct type. SendLayer SDK supports both local and remote file attachments and allows you to add multiple attachment files. See our tutorial to learn more about attaching files to emails.

Email parameters

Below is a table containing the supported parameters for sending emails in Python using SendLayer SDK.

Managing webhooks

With the SendLayer Python SDK, you can create new webhook, view all webhooks you’ve created and also delete a specific webhook.

Creating a new webhook

To create a new webhook, you’ll need to first import the SendLayer module and initialize it with your API key. Then call the Webhooks.create() method and specify the required parameters. This method requires the url and event parameters.
webhooks.py
The event parameter is constrained to the following options:
  • bounce
  • click
  • open
  • unsubscribe
  • complaint
  • delivery
Example success response for new webhook:

Getting all webhooks

To view all the webhooks you’ve created, use the Webhooks.get() method.
webhooks.py
Here is an example response:

Deleting a webhook

To delete a specific webhook, use the Webhooks.delete() method. This method accepts one required parameter webhook_id that needs to be a number.
Deleting a webhook cannot be undone. You won’t be able to recover or access your webhook after deleting it.
webhooks.py

Webhooks parameters

The table below contains details about the supported parameters in the Webhooks module.

Retrieving email events

You can view all events connected to your API key.

Getting all events

To get started, import the SendLayer module and initialize it with your API key. Then call the Events.get() method to retrieve all events.
The Events.get() method retrieves the top 5 events in your account if no filter parameter is specified.
events.py
Example response

Filtering events

The Events.get() method in the SendLayer module accepts some optional parameters. These parameters can be used to filter the API response. Here is an example:
events.py

Events parameters

The table below contains details about the available parameters in the Events module.

Error handling

The SDK provides custom exceptions for better error handling. You can import the SendLayerError or SendLayerAPIError modules, then use Python’s try & exception syntax when sending your emails:
Here is an example error response:

More examples

SendLayer Python SDK

View more details and examples on GitHub.