Prerequisites
- Python 3.8 or later
- A Flask 2 or Flask 3 project
- Authorize your sending domain
- Create or retrieve your SendLayer API key
Installation
Install the SDK along with Flask andpython-dotenv.
Storing your API key
Create a.env file in your project root.
.env
.env to .gitignore so the key stays out of version control.
Creating the email client
Initialize the client once when the application starts and share it across routes. A module-level instance avoids rebuilding the client on every request.app/email_client.py
Sending an email from a route
Add a POST route that reads JSON and callsEmails.send().
app/__init__.py
reply_to field points at the person who filled in the form, so support agents can reply from their inbox.
Use a
sender address on a domain you authorized in SendLayer. Requests from an unauthorized domain fail.Organizing routes with a blueprint
A blueprint keeps email routes separate from the rest of the application. This structure scales better once you add more endpoints.app/routes/email.py
app/__init__.py
Sending HTML emails
Add thehtml parameter for rich content. Keep text as a fallback for clients that block HTML.
app/routes/welcome.py
tags list labels the message. Tags appear in your event history, which helps you separate welcome emails from other traffic.
Rendering emails with Jinja templates
Flask already renders Jinja templates, so you can reuse that engine for email bodies. Place the template undertemplates/emails/.
templates/emails/welcome.html
html parameter.
app/routes/welcome.py
Testing the endpoint
Start the development server.Next steps
Python SDK Reference
Review every method and parameter in the Python SDK.
Managing Webhooks
Create, list, and delete webhook subscriptions.
Retrieving Events
Query delivery events for any message.
Rate Limits
Understand recipient and request limits.