Sign inGet Started

Python SDK

Send email with Pyramid

Use Parcel Wing from Pyramid views while reading API keys from application settings.

Install the SDK

The official SDK is published as parcelwing.

Terminal

pip install parcelwing

Settings

Keep the API key in server-side settings or environment-backed configuration.

development.ini

# development.ini
[app:main]
parcelwing.api_key = %(PARCEL_WING_API_KEY)s
parcelwing.from_email = Acme <[email protected]>

View

Create a JSON view that validates input, sends a template email, and returns the Parcel Wing message ID.

myapp/views.py

from parcelwing import ParcelWing, ParcelWingError
from pyramid.httpexceptions import HTTPBadRequest
from pyramid.response import Response
from pyramid.view import view_config
 
 
def get_parcel_wing(request):
return ParcelWing(api_key=request.registry.settings["parcelwing.api_key"])
 
 
@view_config(route_name="send_welcome", request_method="POST", renderer="json")
def send_welcome(request):
body = request.json_body
email = body.get("email")
 
if not email:
raise HTTPBadRequest(json_body={"error": "Email is required"})
 
try:
emails = get_parcel_wing(request).emails.send(
from_=request.registry.settings["parcelwing.from_email"],
to=email,
template_alias="welcome_email",
template_params={
"first_name": body.get("first_name") or "friend",
},
)
return {"id": emails[0]["id"]}
except ParcelWingError as error:
request.response.status_int = error.status
return {"code": error.code, "request_id": error.request_id}

Routes

Register the route and scan your views during Pyramid app setup.

myapp/__init__.py

from pyramid.config import Configurator
 
 
def main(global_config, **settings):
with Configurator(settings=settings) as config:
config.add_route("send_welcome", "/send-welcome")
config.scan("myapp.views")
return config.make_wsgi_app()

Production notes

  • Do not commit PARCEL_WING_API_KEY to configuration files.
  • Validate and rate limit public JSON endpoints before sending.
  • Log request_id from ParcelWingError when troubleshooting.