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:API Key
Create an API key in the Lettr dashboard
Verified Domain
Add and verify your sending domain
- 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.
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