Exposing FastAPI
Hi, I would like to expose API from my FastAPI project but I'm failing to make calls to it.
Service id - 29800e78-3c36-43a7-99b3-823055a3d0b4
Under Settings -> Domain, I have exposed this service to the public internet
In my main.py
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000)
Service Deploy Logs
INFO: Started server process [1]
INFO: Waiting for application startup.
startup event log
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
Solution:Jump to solution
Since you are listening specifically on port 9000, you would need to set a PORT environment variable in your service dashboard to be 9000
10 Replies
Project ID:
29800e78-3c36-43a7-99b3-823055a3d0b4
Solution
Since you are listening specifically on port 9000, you would need to set a PORT environment variable in your service dashboard to be 9000
Otherwise you can modify your code to listen on the PORT environment variable and you won’t need to specify the variable in your service as it has one injected by default
In your uvicorn.run() function instead of specifying 9000 as the port use
os.getenv(“PORT”)
That should solve it for youHi Vin, thanks for getting on so quickly.
By that do you mean I need to create a new environment variable named "PORT" and set the value to 9000?
I can see that this is mentioned in the docs
https://docs.railway.app/deploy/exposing-your-app, but Im kinda new to networking bits
That would be correct!
Yes, that is what you would do
Or you can just change the port in your code to use the environment variable that is injected in your deploy
In your uvicorn.run() function instead of specifying 9000 as the port use os.getenv(“PORT”) That should solve it for youAh ok thank you very much So once I've set the PORT env variable, would I be able to make api call without any further changes Say if I have a GET "/message/" endpoint that returns a text. If I fire a GET request from Insomnia or Postman to {service-name}-production-up.railway.app/message/, would that be it?
Yep!
make sure you use https when calling your service
Thanks all on the clarification. It helped me and I really appreciate it 🙇♂️