Skip to content

Commit 2d55202

Browse files
committed
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.
1 parent 0b8363c commit 2d55202

5 files changed

Lines changed: 61 additions & 7 deletions

File tree

app/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""NX AI - FastAPI, Python, Postgres, tsvector"""
1+
"""Python - FastAPI, Postgres, tsvector"""
22

33
# Current Version
4-
__version__ = "2.0.5"
4+
__version__ = "2.0.6"

app/api/resend/resend.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
from app import __version__
33
import os
44
from app.utils.make_meta import make_meta
5-
65
from fastapi import APIRouter, Query, Path, Body, HTTPException
7-
86
from app.utils.db import get_db_connection
9-
7+
from .utils.send_email import send_email_resend
108

119
router = APIRouter()
1210
base_url = os.getenv("BASE_URL", "http://localhost:8000")
@@ -21,6 +19,35 @@ def root() -> dict:
2119
return {"meta": meta}
2220
meta = make_meta("success", "Resend endpoint")
2321
data = [
24-
{"action,": f"send email"},
22+
{"action": "send email"},
2523
]
2624
return {"meta": meta, "data": data}
25+
26+
27+
# POST endpoint to send email
28+
from fastapi import status
29+
from pydantic import BaseModel, EmailStr
30+
31+
class EmailRequest(BaseModel):
32+
to: EmailStr
33+
subject: str
34+
html: str
35+
sender: EmailStr
36+
37+
@router.post("/resend", status_code=status.HTTP_202_ACCEPTED)
38+
def send_email(request: EmailRequest):
39+
"""POST /resend endpoint to send email via Resend API."""
40+
if not RESEND_API_KEY:
41+
meta = make_meta("error", "RESEND_API_KEY missing. Please set it in your .env file.")
42+
return {"meta": meta}
43+
result = send_email_resend(
44+
to=request.to,
45+
subject=request.subject,
46+
html=request.html,
47+
sender=request.sender
48+
)
49+
if "error" in result:
50+
meta = make_meta("error", result["error"])
51+
return {"meta": meta}
52+
meta = make_meta("success", "Email sent successfully.")
53+
return {"meta": meta, "data": result}

app/api/resend/utils/send_email.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Utility to send email using Resend API
2+
import httpx
3+
import os
4+
5+
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
6+
RESEND_API_URL = "https://api.resend.com/emails"
7+
8+
def send_email_resend(to: str, subject: str, html: str, sender: str) -> dict:
9+
if not RESEND_API_KEY:
10+
return {"error": "Missing RESEND_API_KEY"}
11+
headers = {
12+
"Authorization": f"Bearer {RESEND_API_KEY}",
13+
"Content-Type": "application/json"
14+
}
15+
payload = {
16+
"from": sender,
17+
"to": [to],
18+
"subject": subject,
19+
"html": html
20+
}
21+
try:
22+
response = httpx.post(RESEND_API_URL, headers=headers, json=payload, timeout=10)
23+
response.raise_for_status()
24+
return response.json()
25+
except Exception as e:
26+
return {"error": str(e)}

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
app = FastAPI(
1010
title="Python",
11-
description="FastAPI, Python, Postgres, tsvector",
11+
description="FastAPI, Postgres, tsvector",
1212
version=__version__,
1313
)
1414

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ python-dotenv>=1.0.0
66
psycopg2-binary>=2.9.0
77
python-multipart>=0.0.20
88
Faker>=25.2.0
9+
pydantic[email]>=2.0.0

0 commit comments

Comments
 (0)