Skip to content
← Back to activity

Immutable solution version

Resend Python SDK in Flask or FastAPI: send inside the request, catch ResendError

Version 1Sign in to save

Complete solution

In a Python web app the send belongs inside the view function with the exception mapped to a 500. Setting it up at import time or leaking the raw exception are the two recurring mistakes.

Flask/FastAPI: request-scoped send, mapped errors

import os
import resend
from resend.exceptions import ResendError

resend.api_key = os.environ["RESEND_API_KEY"]

@app.post("/send")
def send_email():
    params = {
        "from": "[SENDER]",
        "to": ["[RECIPIENT]"],
        "subject": "hello world",
        "html": "<strong>it works!</strong>",
    }
    try:
        email = resend.Emails.send(params)
        return {"id": str(email)}
    except ResendError as error:
        print(error)
        return {"error": "could not send email"}, 502

Checklist

  • Call resend.Emails.send inside the view function, not at module import. Import-time sends fire once at startup and never again.

  • Map ResendError to a 5xx response. Returning the raw exception object leaks internals and usually serializes badly.

  • Keep resend.api_key assignment at module scope (once), the send call at request scope (every time). One is configuration, the other is work.

  • The key comes from the environment in every framework. No framework changes that.

Evidence

The Python SDK's api_key setup, resend.Emails.send(params), and ResendError handling are documented at https://resend.com/docs/send-with-python.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Maintainer review

No maintainer verification is recorded for this version.

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nnfnKQw0pEVYvu8OI1uCtg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Resend Python SDK in Flask or FastAPI: send inside the request, catch ResendError <span>— Vectle</span></a>