From 2d55202d2a702617f136ec3a6180d1199dbe9b24 Mon Sep 17 00:00:00 2001 From: Wei Zang Date: Thu, 2 Apr 2026 19:51:44 +0100 Subject: [PATCH 1/2] Add Resend email endpoint and utility Introduce a POST /resend endpoint and backing utility to send emails via the Resend API. Adds app/api/resend/utils/send_email.py (httpx-based sender), a Pydantic EmailRequest model and handler in app/api/resend/resend.py, and imports the new send helper. Also fixes a small typo in the root data response, updates app metadata/description and bumps __version__ to 2.0.6. Note: RESEND_API_KEY must be set in the environment. Adds pydantic[email] to requirements. --- app/__init__.py | 4 ++-- app/api/resend/resend.py | 35 ++++++++++++++++++++++++++---- app/api/resend/utils/send_email.py | 26 ++++++++++++++++++++++ app/main.py | 2 +- requirements.txt | 1 + 5 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 app/api/resend/utils/send_email.py diff --git a/app/__init__.py b/app/__init__.py index 6c74be7..6e2a60f 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,4 +1,4 @@ -"""NX AI - FastAPI, Python, Postgres, tsvector""" +"""Python - FastAPI, Postgres, tsvector""" # Current Version -__version__ = "2.0.5" +__version__ = "2.0.6" diff --git a/app/api/resend/resend.py b/app/api/resend/resend.py index e81dc3e..9a89b00 100644 --- a/app/api/resend/resend.py +++ b/app/api/resend/resend.py @@ -2,11 +2,9 @@ from app import __version__ import os from app.utils.make_meta import make_meta - from fastapi import APIRouter, Query, Path, Body, HTTPException - from app.utils.db import get_db_connection - +from .utils.send_email import send_email_resend router = APIRouter() base_url = os.getenv("BASE_URL", "http://localhost:8000") @@ -21,6 +19,35 @@ def root() -> dict: return {"meta": meta} meta = make_meta("success", "Resend endpoint") data = [ - {"action,": f"send email"}, + {"action": "send email"}, ] return {"meta": meta, "data": data} + + +# POST endpoint to send email +from fastapi import status +from pydantic import BaseModel, EmailStr + +class EmailRequest(BaseModel): + to: EmailStr + subject: str + html: str + sender: EmailStr + +@router.post("/resend", status_code=status.HTTP_202_ACCEPTED) +def send_email(request: EmailRequest): + """POST /resend endpoint to send email via Resend API.""" + if not RESEND_API_KEY: + meta = make_meta("error", "RESEND_API_KEY missing. Please set it in your .env file.") + return {"meta": meta} + result = send_email_resend( + to=request.to, + subject=request.subject, + html=request.html, + sender=request.sender + ) + if "error" in result: + meta = make_meta("error", result["error"]) + return {"meta": meta} + meta = make_meta("success", "Email sent successfully.") + return {"meta": meta, "data": result} diff --git a/app/api/resend/utils/send_email.py b/app/api/resend/utils/send_email.py new file mode 100644 index 0000000..2a96bbd --- /dev/null +++ b/app/api/resend/utils/send_email.py @@ -0,0 +1,26 @@ +# Utility to send email using Resend API +import httpx +import os + +RESEND_API_KEY = os.getenv("RESEND_API_KEY") +RESEND_API_URL = "https://api.resend.com/emails" + +def send_email_resend(to: str, subject: str, html: str, sender: str) -> dict: + if not RESEND_API_KEY: + return {"error": "Missing RESEND_API_KEY"} + headers = { + "Authorization": f"Bearer {RESEND_API_KEY}", + "Content-Type": "application/json" + } + payload = { + "from": sender, + "to": [to], + "subject": subject, + "html": html + } + try: + response = httpx.post(RESEND_API_URL, headers=headers, json=payload, timeout=10) + response.raise_for_status() + return response.json() + except Exception as e: + return {"error": str(e)} diff --git a/app/main.py b/app/main.py index 94c10a5..ceb0698 100644 --- a/app/main.py +++ b/app/main.py @@ -8,7 +8,7 @@ app = FastAPI( title="Python", - description="FastAPI, Python, Postgres, tsvector", + description="FastAPI, Postgres, tsvector", version=__version__, ) diff --git a/requirements.txt b/requirements.txt index 5518972..64b216d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ python-dotenv>=1.0.0 psycopg2-binary>=2.9.0 python-multipart>=0.0.20 Faker>=25.2.0 +pydantic[email]>=2.0.0 From 75afd686f6bfd24a3a4c4136dc49ddcbc71fdaca Mon Sep 17 00:00:00 2001 From: Goldlabel Date: Thu, 2 Apr 2026 19:58:27 +0100 Subject: [PATCH 2/2] Update app/api/resend/utils/send_email.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- app/api/resend/utils/send_email.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api/resend/utils/send_email.py b/app/api/resend/utils/send_email.py index 2a96bbd..b292192 100644 --- a/app/api/resend/utils/send_email.py +++ b/app/api/resend/utils/send_email.py @@ -2,14 +2,14 @@ import httpx import os -RESEND_API_KEY = os.getenv("RESEND_API_KEY") RESEND_API_URL = "https://api.resend.com/emails" def send_email_resend(to: str, subject: str, html: str, sender: str) -> dict: - if not RESEND_API_KEY: + resend_api_key = os.getenv("RESEND_API_KEY") + if not resend_api_key: return {"error": "Missing RESEND_API_KEY"} headers = { - "Authorization": f"Bearer {RESEND_API_KEY}", + "Authorization": f"Bearer {resend_api_key}", "Content-Type": "application/json" } payload = {