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 usingpip.
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 thehtml 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 messageSending to multiple recipients
You can send emails to multiple recipients including"Cc" and "BCC" email addresses.
send_email.py
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 theattachments parameter to the email payload.
send_email.py
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 theWebhooks.create() method and specify the required parameters. This method requires the url and event parameters.
webhooks.py
event parameter is constrained to the following options:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
Getting all webhooks
To view all the webhooks you’ve created, use theWebhooks.get() method.
webhooks.py
Deleting a webhook
To delete a specific webhook, use theWebhooks.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 theWebhooks module.
Retrieving email events
You can view all events connected to your API key.Getting all events
To get started, import theSendLayer 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
Filtering events
TheEvents.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 theEvents module.
Error handling
The SDK provides custom exceptions for better error handling. You can import theSendLayerError or SendLayerAPIError modules, then use Python’s try & exception syntax when sending your emails:
More examples
SendLayer Python SDK
View more details and examples on GitHub.