New to the Lettr Python SDK? Start with the Python Quickstart to learn the basics, then return here for FastAPI-specific integration patterns.
Prerequisites
Before you begin, make sure you have: 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.Install dependencies
FastAPI Application Structure
For production applications, organize your code with a proper structure:Configuration
Create aconfig.py file using Pydantic settings:
Pydantic Models
Create email models inmodels/email.py:
Email Service
Create an email service inservices/email.py:
Routers with Dependency Injection
Create authentication routes inrouters/auth.py:
Main Application
Bring it all together inmain.py:
Advanced Features
Using Templates
Send emails using Lettr-managed templates: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.