Skip to main content
New to the Lettr Python SDK? Start with the Python Quickstart to learn the basics, then return here for FastAPI-specific integration patterns.
Send transactional emails from your FastAPI applications using the official Lettr Python SDK.
The Lettr Python SDK is synchronous. Calling it directly inside an async def route blocks FastAPI’s event loop and will stall every other request in flight. Every example on this page wraps SDK calls in asyncio.to_thread, which hands the blocking call to a worker thread and awaits the result.

Prerequisites

Before you begin, make sure you have:

API Key

Create an API key in the Lettr dashboard

Verified Domain

Add and verify your sending domain
You’ll also need:
  • Python 3.8 or later installed
  • FastAPI web framework
  • A verified sending domain in your Lettr dashboard

Quick Setup

Get started in three quick steps: install dependencies, configure FastAPI, and send.
1

Install dependencies

This installs the Lettr SDK, FastAPI framework, Uvicorn ASGI server, and python-dotenv for environment variable management.
2

Configure environment

Create a .env file in your project root:
Add .env to your .gitignore to prevent committing your API key to version control.
3

Create FastAPI application

Run with python main.py or uvicorn main:app --reload.
The sender domain must be verified in your Lettr dashboard before you can send emails. Sending from an unverified domain returns 400 with error_code: unconfigured_domain.

FastAPI Application Structure

For production applications, organize your code with a proper structure:

Configuration

Create a config.py file using Pydantic settings:

Pydantic Models

Create email models in models/email.py:

Email Service

Create an email service in services/email.py:

Routers with Dependency Injection

Create authentication routes in routers/auth.py:

Main Application

Bring it all together in main.py:

Advanced Features

Using Templates

Send emails using Lettr-managed templates:
See the Python Quickstart for more details on templates, attachments, and other SDK features.

Background Tasks

Use FastAPI’s background tasks for async email sending:

Batch Sending with asyncio

Fan several sends out across worker threads with asyncio.gather. Note the API allows 3 requests/second per team, so bound this for large lists — and if every recipient gets identical content, prefer a single call with up to 50 addresses in to over N concurrent calls:

Error Handling

Implement error handling using FastAPI’s HTTPException:
See Error Handling in the Python Quickstart for comprehensive error handling patterns.

Testing

Create tests using pytest and httpx:

Best Practices

Use Pydantic Models

Validate all input with Pydantic:

Structured Logging

Use structured logging for better observability:

Rate Limiting

Use slowapi for rate limiting:

What’s Next

Python SDK

Complete Python SDK documentation

Flask Integration

Use Lettr with Flask

API Reference

Complete API documentation

Templates

Use Lettr-managed templates