Python SDK
Send email with FastAPI
Use Parcel Wing from FastAPI endpoints with Pydantic validation and dependency-injected clients.
Install the SDK
The official SDK is published as parcelwing.
Terminal
pip install parcelwing
Endpoint
Create a typed endpoint that validates request bodies and sends a saved template.
main.py
import osfrom typing import Annotatedfrom fastapi import Depends, FastAPI, HTTPExceptionfrom pydantic import BaseModel, EmailStrfrom parcelwing import ParcelWing, ParcelWingErrorapp = FastAPI()class WelcomeRequest(BaseModel):email: EmailStrfirst_name: str | None = Nonedef get_parcel_wing() -> ParcelWing:return ParcelWing(api_key=os.environ["PARCEL_WING_API_KEY"])@app.post("/send-welcome")def send_welcome(payload: WelcomeRequest,parcel_wing: Annotated[ParcelWing, Depends(get_parcel_wing)],):try:emails = parcel_wing.emails.send(from_="Acme <[email protected]>",to=payload.email,template_alias="welcome_email",template_params={"first_name": payload.first_name or "friend",},)return {"id": emails[0]["id"]}except ParcelWingError as error:raise HTTPException(status_code=error.status,detail={"code": error.code, "request_id": error.request_id},) from error
Production notes
- Keep
PARCEL_WING_API_KEYin server-side environment variables. - Validate inputs with Pydantic before sending email.
- Return or log
request_idfromParcelWingErrorfor debugging.