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:
Since you are listening specifically on port 9000, you would need to set a PORT environment variable in your service dashboard to be 9000
Jump to solution
10 Replies
Percy
Percy16mo ago
Project ID: 29800e78-3c36-43a7-99b3-823055a3d0b4
Solution
MantisInABox
MantisInABox16mo ago
Since you are listening specifically on port 9000, you would need to set a PORT environment variable in your service dashboard to be 9000
MantisInABox
MantisInABox16mo ago
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 you
squidwarddab1111
squidwarddab1111OP16mo ago
Hi 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
root
root16mo ago
That would be correct!
MantisInABox
MantisInABox16mo ago
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
squidwarddab1111
squidwarddab1111OP16mo ago
In your uvicorn.run() function instead of specifying 9000 as the port use os.getenv(“PORT”) That should solve it for you
Ah 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?
root
root16mo ago
Yep!
Brody
Brody16mo ago
make sure you use https when calling your service
squidwarddab1111
squidwarddab1111OP16mo ago
Thanks all on the clarification. It helped me and I really appreciate it 🙇‍♂️
Want results from more Discord servers?
Add your server