r/FastAPI • • May 17 '24

Question How to deploy Fast api on Render ??

Hi,I am completely new to this technology. I have built an API using FastAPI and a MySQL database. However, I am unable to understand how to deploy it. Could someone please explain the deployment process or assist me in resolving this issue?

Thank you

1 Upvotes

10 comments sorted by

View all comments

10

u/[deleted] May 17 '24

[removed] — view removed comment

1

u/LiarsEverywhere May 17 '24

To be fair, the link to the example repository is broken. Anyway, having a quick look at an old service that seems to be working still, only two files are needed in the repository:

  1. requirements.txt in the following format, replaced with whatever you're using of course (search for pip freeze if you don't know how to create this automatically):

    beautifulsoup4==4.11.1 fastapi==0.88.0 uvicorn==0.12.3

  2. main.py with this line:

    app = FastAPI(docs_url=None, redoc_url=None)

Note that this is from an old project and I haven't used FastAPI in a while, so the way to disable docs might have changed, I wouldn't know. Also, uvicorn version is probably outdated.

After that, set up the Web Service.

Build Command: pip install -r requirements.txt

Start Command: uvicorn main:app --host 0.0.0.0 --port 10000

After that, if it's working locally, it should work on render.

Extra - proceed with caution:

Looking at my code, I added the following to main.py, apparently to solve CORS problems. Maybe it was for local development and not Render, so maybe it's not needed at all, but I don't remember. (Don't judge me, it wasn't anything close to production, I was just experimenting)

allow_all = ['*']
app.add_middleware(
   CORSMiddleware,
   allow_origins=allow_all,
   allow_credentials=True,
   allow_methods=allow_all,
   allow_headers=allow_all
)