{"id":912,"hash":"1ea630dc9344ff51fa56a69a0285eb7c642ee8a4a456dcadb09bcc7128385ea1","pattern":"FastAPI app results in 404 error response when it is started using uvicorn.run","full_message":"New to FastAPI and uvicorn, but I'm wondering why when I run my \"hello world\" service by starting it using uvicorn from the command line, it works fine, but when using the \"uvicorn.run\" method from inside my service, the service starts, but when I send a GET I always get a 404 with a response body of {\"detail\": \"Not Found\"}?\n\nHere is my code:\n\nimport uvicorn\nfrom fastapi import FastAPI\n\napp = FastAPI()\nuvicorn.run(app, host=\"127.0.0.1\", port=5049)\n\n@app.get(\"/\")\nasync def root():\n    return {\"message\": \"Hello World\"}\n\nThat always returns with a 404 as follows:\n\n# curl http://127.0.0.1:5049/\n{\"detail\":\"Not Found\"}\n\nThe output from my service shows:\n\nINFO:     Started server process [28612]\nINFO:     Waiting for application startup.\nINFO:     Application startup complete.\nINFO:     Uvicorn running on http://127.0.0.1:5049 (Press CTRL+C to quit)\nINFO:     127.0.0.1:55446 - \"GET / HTTP/1.1\" 404 Not Found\n\nIf I comment out the \"uvicorn.run\" line and then start the service from the command line with (running on Windows 10):\n\nuvicorn.exe test:app --host=127.0.0.1 --port=5049\n\nI get the correct response:\n\n# curl http://127.0.0.1:5049/\n{\"message\":\"Hello World\"}","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"Because, the statement uvicorn.run(app, host=\"127.0.0.1\", port=5049) is executed before the root(...) function and the execution is never reaching to the root(...) function.\n\nBut, when you run the app using the command line, the app is loaded in a lazy fashion, and hence the root(...) function is getting executed.\n\nSomething like this would surely resolve the issue:\n\nimport uvicorn\nfrom fastapi import FastAPI\n\napp = FastAPI()\n\n@app.get(\"/\")\nasync def root():\n    return {\"message\": \"Hello World\"}\n\n # at last, the bottom of the file/module\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"127.0.0.1\", port=5049)","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/64019054/fastapi-app-results-in-404-error-response-when-it-is-started-using-uvicorn-run","votes":28,"created_at":"2026-04-19T04:51:59.336469+00:00","updated_at":"2026-04-19T04:51:59.336469+00:00"}