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. FastAPI’s async-first architecture pairs perfectly with Lettr’s async client for high-performance email delivery in modern Python applications.

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 a validation error.

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

Send multiple emails concurrently using asyncio.gather:

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