r/FastAPI • u/koldakov • Jul 20 '24
pip package pycountries lib compatible with pydantic from scratch, supports amount normalization and much more
r/FastAPI • u/koldakov • Jul 20 '24
r/FastAPI • u/No-Question-3229 • Jul 20 '24
Im having an issue setting my auth cookies for some reason. I dont know why fast api wont set these cookies.
async def set_cookies():
# Get cookie header
# Used to tell auth server if it should set the login cookies
set_auth_cookies = request.headers.get("set-auth-cookies")
print(f"set-auth-cookies header: {set_auth_cookies}")
print(f"Type: {type(set_auth_cookies)}")
print(f"Boolean value: {bool(set_auth_cookies)}")
if set_auth_cookies != None and bool(set_auth_cookies) == True:
response.set_cookie(key="LIF_USERNAME", value=username, domain=".lifplatforms.com", path="/")
response.set_cookie(key="LIF_TOKEN", value=token, domain=".lifplatforms.com", path="/")
print("set auth cookies")Ive tried timing to make sure the cookies are being set before the request completes and it looks like thats good. Ive even tried using 'await' on the set cookie functions but complains I cant use await on NoneType. I dont know what else could be causing the issue.
Full Source Code: https://github.com/Lif-Platforms/Lif-Auth-Server/blob/107-make-login-route-set-cookies/src/auth_server.py#L160
r/FastAPI • u/Doggart193 • Jul 19 '24
Hi everyone,
I'm currently developing an email sending API with FastAPI, which supports sending HTML emails and will soon handle attachments.
I'm facing some issues with the POST method, specifically when trying to incorporate the attachment functionality.
Here's what I've implemented so far:
**Problem**: I'm having trouble configuring the POST method to handle optional attachments properly. The method should work both when an attachment is included and when it's just a regular email without attachments. Interestingly, the method works perfectly when I remove the file parameter from the POST request, which means it fails only when trying to include an attachment.
**Current SchemaRequest**:
class EmailSchemaRequest(SQLModel):
from_email: EmailStr = Field(...,
description
="Email address of the sender")
to_email: List[EmailStr] = Field(...,
description
="Email address of the recipient")
to_cc_email: Optional[List[EmailStr]] = Field(None,
nullable
=True,
description
="Email address to be included in the CC field")
to_cco_email: Optional[List[EmailStr]] = Field(None,
nullable
=True,
description
="Email address to be included in the BCC field")
subject: str = Field(...,
description
="Subject of the email",
min_length
=1,
max_length
=50)
html_body: str = Field(...,
description
="HTML content of the email",
min_length
=3)**Current post method**:
u/router.post("/",
status_code
=status.HTTP_202_ACCEPTED,
response_description
="Email scheduled for sending")
def schedule_mail(
background_tasks
: BackgroundTasks,
request
: EmailSchemaRequest,
file
: UploadFile | bytes = File(None),
session
: Session = Depends(get_session),
current_user
: UserModel = Depends(get_current_active_user)
):
email = EmailModel(
from_email
=normalize_email(
request
.from_email),
to_email
=normalize_email(
request
.to_email),
to_cc_email
=normalize_email(
request
.to_cc_email),
to_cco_email
=normalize_email(
request
.to_cco_email),
subject
=
request
.subject.strip(),
html_body
=
request
.html_body,
status
=EmailStatus.PENDING,
owner_id
=
current_user
.id
)
if
file
:
validate_file(
file
)
try:
service_dir = os.path.join(PUBLIC_FOLDER,
current_user
.service_name)
os.makedirs(service_dir,
exist_ok
=True)
file_path = os.path.join(service_dir,
file
.filename)
with open(file_path, "wb") as f:
f.write(
file
.file.read())
email.attachment = file_path
except Exception as e:
logger.error(f"Error saving attachment: {e}")
raise HTTPException(
status_code
=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail
="Failed to save attachment")
email = create_email(
session
, email)
background_tasks
.add_task(send_email_task, email.id, email.owner_id)
return {"id": email.id, "message": "Email scheduled for sending"}Any guidance or resources would be greatly appreciated. Thank you!
NewMethod:
@router.post("/",
status_code
=status.HTTP_202_ACCEPTED,
response_description
="Email scheduled for sending")
async def schedule_mail(
background_tasks
: BackgroundTasks,
payload
: str = Form(...,
description
="JSON payload containing the email details"),
file
: UploadFile = File(None,
description
="Optional file to be attached"),
session
: Session = Depends(get_session),
current_user
: UserModel = Depends(get_current_active_user)
):
try:
# Check if the payload is a valid JSON
email_request = EmailSchemaRequest.model_validate_json(
payload
)
# Create the email object with the data from the request validated
email = EmailModel(
from_email
=normalize_email(email_request.from_email),
to_email
=normalize_email(email_request.to_email),
to_cc_email
=normalize_email(email_request.to_cc_email),
to_cco_email
=normalize_email(email_request.to_cco_email),
subject
=email_request.subject.strip(),
html_body
=email_request.html_body,
status
=EmailStatus.PENDING,
owner_id
=
current_user
.id
)
# Save the attachment if it exists
if
file
:
file_path = await save_attachment(
file
,
current_user
)
if file_path is None:
raise HTTPException(
status_code
=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail
="Failed to save attachment")
email.attachment = file_path
# Create the email in the database
email = create_email(
session
, email)
# Schedule the email for sending
await
background_tasks
.add_task(send_email_task, email.id, email.owner_id)
# Return the response with the email id and a message
return {"id": email.id, "message": "Email scheduled for sending"}
except json.JSONDecodeError:
return {"error": "Invalid JSON format", "status_code": 400}
except ValueError as e:
return {"error": str(e), "status_code": 400}r/FastAPI • u/fbrdm • Jul 18 '24
r/FastAPI • u/rycco • Jul 18 '24
Hey guys,
We have developed our backend using fastapi async, and often we find that the process just freezes forever. We believe that somewhere we are calling a sync function inside a controller, which is blocking the GIL and everything else from that point. It happens like twice a day on the develoment environment and it has happened in production a few times as well.
I recently found out that if you call something sync (blocking) in any async function it blocks the whole GIL, so I assume that's our issue. But I have no idea where exactly that is happening (on which endpoint). What would be the best pragmatic way to find this? Is there any flag I could enable for debugging where it would log me something so I could at least see what was the last thing called before freezing?
Thanks,
r/FastAPI • u/guteira • Jul 18 '24
Hi folks,
I would like to have some advice here if possible. I’m building my app and want to use FastAPI to avoid cloud lock-in.
I will use AWS to host my app, the stack will have API Gateway as frontdoor and Lambda functions as backend.
My question here is:
Or
I see the first approach is easier to maintain, but the cons is the whole API container will go up every time someone want to use a specific part of the code(specific API), which may increase the number of lambda invocations
On the other hand, the second approach provides more segmentation, better scalability, but hard to maintain.
What’s your opinion? And do you recommend something else?
I really appreciate your voice here, thanks and have a good one!!
r/FastAPI • u/Amocon • Jul 18 '24
So i am trying to use redis as cache for my FastAPI app, but i dont get how to get everything working. I am using python 3.12 and it seems that redis and aioredis both do not support this version. Is this correct? And if so, is there any way to implement redis without changing my python version?
r/FastAPI • • Jul 18 '24
Is there any way to (easily) deploy a FastAPI route as a Google Cloud Function? As far as I could grasp from docs, GCF integrate well with Flask, but no so much with FastAPI, and I'd love to be able to leverage FA types, validations and automatic documentation, while not depending on more complex/costly infrastructures such as Google Cloud Run or App Engine.
Thanks for any tips!
r/FastAPI • u/Spanking_daddy69 • Jul 17 '24
I am stuck for past 3 hours trying to deploy my api. It's always 404... file structure
Quantlab-Backend/ ├── app/ │ ├── init.py │ ├── middleware.py │ └── routes/ │ ├── users.py │ ├── problems.py │ └── playlists.py ├── requirements.txt ├── vercel.json └── main.py
All these yt tutorials are showing how to deply a single main.py.
Thanks in advance please help
r/FastAPI • u/Ahmad_Azhar • Jul 17 '24
Hey, fellow developers!
I started with the intention of trying HTMX but ended up expanding the project to achieve a comprehensive BoilerPlate implementation. My objective was to create a robust web application boilerplate that combines the power of 📌 FastAPI, HTMX, and AlpineJS. It's designed for rapid prototyping and development, with built-in user management, roles, groups, and CRUD operations.

🔗 GitHub Repository: https://github.com/Hybridhash/FastAPI-HTMX
🛠 Key Features:
If you find this useful, please give it a ⭐ on GitHub!
Happy coding! 🎉
r/FastAPI • u/anseho • Jul 17 '24
Hello everyone! It's been a while and just put together a new tutorial on how to implement login and how to issue API access tokens using Auth0 and FastAPI. It also explains to how issue refresh and ID tokens.
To clarify the terminology here:
sub property that identifies the user, and claims about their rights to access the API.The tutorial explains how to issue tokens using two of the most common OAuth flow:
The idea is the authorization code flow is designed for traditional web applications like those we'd create with Django or Ruby on Rails. For APIs, the PKCE flow is usually recommended, and it's all handled from the UI. However, nothing prevents us from using the auth code flow in APIs too. It allows us to remove this complexity from the fronted, and as you'll see in the video, it's very easy to implement.
Link to the tutorial: https://youtu.be/ato2S5b27o8
Code for the tutorial: https://github.com/abunuwas/short-tutorials/tree/main/fastapi-auth0
Note: there's a previous tutorial to this one that explains how to set up an Auth0 account if you need help with that.
Hope you enjoy the video and find it useful!
r/FastAPI • u/suquant • Jul 15 '24
Hey everyone,
Check out this Helm chart for deploying FastAPI servers on Kubernetes: Helm Chart for FastAPI.
Key Features:
Getting Started:
helm repo add gbyte https://gbytetech.github.io/helm/
helm repo update gbyte
helm install my-fastapi-app gbyte/fastapi
Give it a try and streamline your FastAPI deployments!
r/FastAPI • u/Sulray250 • Jul 15 '24
Hi,
I'm kind of new to web development and I have a case in which I need to update the objects in my database through an external API. I'm using a react/fastapi/postgresql stack.
Imagine I have two GET endpoints, "/update/objects" and "/update/object/{object_id}"
(not UPDATE type because i'm not sending the data to update objects myself, I just ask my backend to contact the external API to do the update internally)
@router.get("/update/object/{object_id}")
async def update_one_object(object_id: int):
# code to update one object with external API
@router.get("/update/objects")
async def update_objects():
# code to update all objects with external APITo manipulate objects in my database I have a object_crud.py script.
What would be the best practice to implement the "/update/objects" endpoint ?
For the moment, my "/update/object/{object_id}" :
Is this part right ? Should I also find a way to delegate elsewhere the retrievement of variables from external data ?
Thanks !
r/FastAPI • u/TistaMuna • Jul 15 '24
I'm trying to create an API which performs function on an input
from .main import main
@app.on_event("startup")
async def startup_event():
start_time = time.time()
app.state.model = SentenceTransformer('model')
end_time = time.time()
logger.info(f'Models loaded in {end_time - start_time} seconds')
@app.post("/search")
async def search(
question: str = Form(...),
) -> JSONResponse:
logger.info(f'question----> :{question}')
model = app.state.model
start_time_main = time.time()
results, lookup_time = await main(query=question, model=model)
end_time_main = time.time()
logger.info(f'Main func in {end_time_main - start_time_main} seconds')
api_results = [
{
"answer": result[1],
}
for result in results
]
return JSONResponse(content=api_results)To run my server, i'm using uvicorn backend.main:app --reload --port 8000 --log-level debug the Logs are:
INFO:backend.apis:Models loaded in 8.509195804595947 seconds
INFO: Application startup complete.
INFO:backend.apis:question----> :How to get ticket
Batches: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 1.25it/s]
Batches: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 27.80it/s]
INFO:backend.apis:Main func in 13.68946623802185 seconds
INFO: 127.0.0.1:35428 - "POST /lookup?search=How%20to%20get%20ticket HTTP/1.1" 200 OKSearch time is under 2 milliseconds. Earlier, the API latency was under 2 sec, but today after few edits again it is taking 5-10 s to work. I believe the model itself is being loaded again and again on every API search and that is why the API latency is over 5-10s
I already tried reloading the server,text editor, and postman clients on (POST request). But it isn't working. I also edited the startup function but no result is coming
r/FastAPI • u/robo__Dev • Jul 14 '24
Interesting video from Pycon Italy.
r/FastAPI • u/AlexanderBrozov • Jul 12 '24
Hello everyone,
I'm relatively new to backend development, Docker, and working with ML models. I've built a Dockerized FastAPI image that has a single endpoint for serving a saved TensorFlow model (around 100 MB).
Upon exploring my Docker image, I found that the installed dependencies take up most of its memory, with the ranking as follows:
I'm wondering if this image size is normal. Are there ways to optimize it? What can you recommend to reduce the size while maintaining functionality?
Thanks in advance for your help! Open to any new knowledge.
In the docker file you can see crazy lines of installing libhdf5 - I included it because for some reason hd5f pip couldn't install those on its own.
Here is my dockerfile:
FROM python:3.11-slim as requirements-stage
WORKDIR /tmp
RUN pip install --no-cache-dir poetry
COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
FROM python:3.11-slim as build-stage
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libhdf5-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt \
&& rm -rf /root/.cache/pip
FROM python:3.11-slim as runtime-stage
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
libhdf5-103 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
COPY --from=build-stage /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=build-stage /usr/local/bin /usr/local/bin
COPY histology_image_platform_backend /app/histology_image_platform_backend
EXPOSE 8000
CMD ["uvicorn", "histology_image_platform_backend.main:start", "--host", "0.0.0.0"]r/FastAPI • u/No-Question-3229 • Jul 12 '24
For some reason, this code doesn't seem to want to delete my auth cookies. My parent domain is "lifplatforms.com" and the cookie's domain is set to ".lifplatforms.com". So they should be accessible and able to be changed across all sub-domains. They are accessible but not to this server. The subdomain I'm using for testing is "localtesting.lifplatforms.com". For whatever reason the browser decides that it wont send my cookies to this server even tho it's a sub-domain of "lifplatforms.com". Why? It works for the others when it comes to reading them but this particular instance it just doesn't work.
@app.get("/auth/logout")
async def log_out(request: Request):
"""
## Logout Route For Lif Accounts
Handles the logout process for Lif Accounts.
### Parameters:
none
### Returns:
- **STRING:** Status of the operation.
"""
# Create response for client
response = Response()
print(request.cookies.get("LIF_USERNAME"))
print(request.cookies.get("LIF_TOKEN"))
# Delete auth cookies
response.delete_cookie("LIF_USERNAME", domain=".lifplatforms.com", path="/")
response.delete_cookie("LIF_TOKEN", domain=".lifplatforms.com", path="/")
return responseIn this code I've printed the values of the auth cookies for testing but they return with None. I'm not sure why this is cuz it works fine for all the rest of the sub-domains.

Any help with this would be greatly appreciated.
r/FastAPI • • Jun 28 '24
Hey
I am using FastAPI and React for an app. I wanted to ask a few questions:
Thanks!
r/FastAPI • u/hujoooooooooo • Apr 23 '24
A few weeks ago, I started working on an Inertia.js adapter for FastAPI.
For those of you who might not know, Inertia.js is a JSON protocol used to communicate between your SPA and your backend.
As per their words:
Inertia isn't a framework, nor is it a replacement for your existing server-side or client-side frameworks. Rather, it's designed to work with them. Think of Inertia as glue that connects the two. Inertia does this via adapters
Basically, it moves all the routing logic to the backend, and therefore provides a way to "inject" props to your view directly.
I just published it to PyPI, I hope you like it !
https://github.com/hxjo/fastapi-inertia
r/FastAPI • u/_prom_ • Apr 02 '24
Hi everyone
I am new to fastAPI & python, coming from the frontend side of the world and nodejs. I was hoping this community could link me through their past/present fastAPI projects where there is a proper db connection, directory structure etc. The basic stuff. I am tired of googling for blogs and not getting what I want.
Until now, I haven't been able to figure out any common pattern on directory structure, or connection using MySQL, Postgres etc. Some things I am importing from sqlmodel and some from sqlalchemy..
Idk... i am super confused and idk what I am talking about. I just need some good project links from where I can learn and not some blogs that university students wrote (sorry not trying to insult anyone, it's my frustration) Thanks ^^
r/FastAPI • u/kloworizer • Feb 08 '24
previously I used Heroku for fastapi hosting. but now there is no free tier anymore in Heroku. Does anyone know where I can host Fastapi for free like on Heroku again?
r/FastAPI • u/Nehatkhan786 • Feb 05 '24
Hey guys I am learning SqlModel and when I come to advance section there is no docs for migration. So is it available with SqlModel or its upcoming feature?
r/FastAPI • u/Gullible-Insurance63 • Dec 30 '23
Hey FastAPI fam!
Curious if anyone has explored creating a sleek web app using only FastAPI, HTML, and CSS—no JavaScript. 🤔
Question: Any success stories or advice on using FastAPI for both backend and frontend without dipping into JavaScript? Tips, challenges, or examples welcome!
Also, if you've come across any videos or posts about this, please share! 📹📚
Excited to hear your insights! 🌐💻