Skip to main content

Prerequisites

Installation

Install the SDK along with Flask and python-dotenv.

Storing your API key

Create a .env file in your project root.
.env
Add .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
The startup check fails fast on a missing key. Without it, the first request becomes the place the problem surfaces.

Sending an email from a route

Add a POST route that reads JSON and calls Emails.send().
app/__init__.py
The 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.
Run the application:

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
Register the blueprint in your factory:
app/__init__.py
Validating the payload first saves a round trip to the API and returns a clearer error to the caller.

Sending HTML emails

Add the html parameter for rich content. Keep text as a fallback for clients that block HTML.
app/routes/welcome.py
The 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 under templates/emails/.
templates/emails/welcome.html
Render the template, then pass the result as the html parameter.
app/routes/welcome.py
Keep email templates in their own folder. Email clients support a narrow subset of CSS, so those files rarely share styles with your web pages.

Testing the endpoint

Start the development server.
Then post a request from another terminal:
A successful request returns the message ID:
Open the Email logs page in your SendLayer dashboard to confirm delivery.

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.