Python SDK
Send email with CherryPy
Use Parcel Wing from CherryPy exposed methods and JSON tools while keeping API keys server-side.
Install the SDK
The official SDK is published as parcelwing.
Terminal
pip install parcelwing
JSON endpoint
Use CherryPy's JSON tools to parse the request body and serialize the response.
app.py
import osimport cherrypyfrom parcelwing import ParcelWing, ParcelWingErrorparcel_wing = ParcelWing(api_key=os.environ["PARCEL_WING_API_KEY"])class EmailApi:@cherrypy.expose@cherrypy.tools.json_in()@cherrypy.tools.json_out()def send_welcome(self):body = cherrypy.request.jsonemail = body.get("email")if not email:cherrypy.response.status = 400return {"error": "Email is required"}try:emails = parcel_wing.emails.send(from_=os.environ.get("PARCEL_WING_FROM_EMAIL","Acme <[email protected]>",),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:cherrypy.response.status = error.statusreturn {"code": error.code, "request_id": error.request_id}if __name__ == "__main__":cherrypy.quickstart(EmailApi(), "/")
Server config
For a small standalone CherryPy service, set the bind host, port, and thread pool in config.
cherrypy.conf
[global]server.socket_host = "0.0.0.0"server.socket_port = 8080server.thread_pool = 10
Production notes
- Keep
PARCEL_WING_API_KEYin environment variables or secret storage. - Validate request bodies before sending email from public methods.
- Return or log
request_idfromParcelWingErrorfor tracing.