Python SDK
SendLayer Python SDK installation and usage
Prerequisites
Before getting started, you’ll need to:
- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer Python SDK using the command:
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.
Sending HTML Emails
To send HTML emails, simply include the html
parameter to the send()
method.
text
parameter to have a plain text and HTML version of your email messageSending to Multiple Recipients
You can send emails to multiple recipients including "Cc"
and "BCC"
email addresses.
Each recipient field is an array containing the recipient’s name
and email
as objects.
Including Attachments
You can include attachments to your email message by adding the attachments
parameter to the email payload.
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.
Parameter | Type | Required | Description |
---|---|---|---|
to | string or list | Yes | Email address(es) of the recipient(s). Can be a single email string or a list of dictionaries with ‘email’ and ‘name’ keys |
sender | string or dict | Yes | Email address of the sender. Can be a single email string or a dictionary with ‘email’ and ‘name’ keys |
subject | string | Yes | Subject line of the email |
text | string | No | Plain text version of the email content |
html | string | No | HTML version of the email content |
cc | list | No | List of dictionaries containing CC recipient email addresses and names |
bcc | list | No | List of dictionaries containing BCC recipient email addresses and names |
reply_to | list | No | List of dictionaries containing reply-to email addresses and names |
attachments | list | No | List of dictionaries containing file paths and MIME types for attachments |
tags | list | No | List of strings used to add tags to email messages |
headers | dict | No | Dictionary with key-value pairs for adding custom email headers |
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.
The event
parameter is constrained to the following options:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
Example success response for new wehbook:
Getting All Webhooks
To view all the webhooks you’ve created, use the Webhooks.get()
method.
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.
Webhooks Parameters
The table below contains details about the supported parameters in the Webhooks
module.
Parameter | Type | Required | Description |
---|---|---|---|
url | string | Yes | The webhook endpoint URL where events will be sent |
event | string | Yes | The type of event to listen for. Options: bounce, click, open, unsubscribe, complaint, delivery |
webhook_id | integer | Yes | Unique identifier for the webhook (used in delete operation) |
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.
Events.get()
method retrieves the top 5 events in your account if no filter parameter is specified.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 Parameters
The table below contains details about the available parameters in the Events
module.
Parameter | Type | Required | Description |
---|---|---|---|
start_date | datetime | No | Start date for filtering events |
end_date | datetime | No | End date for filtering events |
event | string | No | Filter the API request by the type of event. Supported events include: accepted , rejected , delivered , opened , clicked , unsubscribed , complained , failed |
message_id | string | No | Filter by the email MessageId |
start_from | integer | No | Specify a starting number for the email filter |
retrieve_count | integer | No | This parameter controls the number of event records that’ll be displayed. It defaults to 5 if none is specified |
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.