FastAPI simple block of code not working

Question: I don’t have the most experience with FastAPI in Python and I would like to know why this block of code just instantly stops after it is ran

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

If you could just explain why and leave a block of code for me underneath thanks!

You never start the app, so it will immediately exit due to there being no code to run. After your last line of code setting up routes, you need a line like app.run() (which is flask, I don’t know if that’s right for FastAPI).

1 Like

try after adding

import uvicorn 

if __name__ == "__main__":
    uvicorn.run("main.app:app", host="0.0.0.0", reload=True)
3 Likes

Thank you