Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling internal api from another api function

I have implemented two endpoints:

Post -  /users/  #endpoint to add a user
Post - /confirmemail/  #endpoint to confirm email

Now I have function implemented for both endpoints, But I am thinking of calling the email endpoint after adding the user, directly. How can I achieve this in Fastapi?

like image 277
Ashwani Avatar asked Aug 10 '26 07:08

Ashwani


1 Answers

If one of your functionality will be used by multiple endpoints, you may need to extract it into a separate function (decoupling), for example:

def send_confirm_email():
    pass

Then call it in different endpoints:

from .utils import send_confirm_email

@app.post("/users")
def add_user():
    # ...
    send_confirm_email()
    return {"message": "User added, confirm email sent."}

@app.post("/confirmemail")
def confirm_email():
    send_confirm_email()
    return {"message": "confirm email sent."}
like image 93
Grey Li Avatar answered Aug 12 '26 04:08

Grey Li