Hosting your fast-API server : Really fast

Qrious Kamal
3 min readFeb 12, 2023

--

This is a story of a search that lead me to many dead-ends on a Sunday. Oh, the quest?? Quest is to host a API server based on Python’s fast-API framework that I worked in the past week. Following is the snapshot of the API server with multiple end-points, that is hosted here

fastapi server endpoints hosted on glitch
Above API can be at https://pyrite-ethereal-soccer.glitch.me/docs

World of Options:

The best solution to host the API server for free is Glitch.com. I have listed top 5 options that I tried along with the brief outcome. There were other options/services which are not feasible, so I did not include them here. Exploring them, provided opportunity to learn more about a particular service/system/product

Options tried along with the outcome submitted

Major Challenge: Getting Compute Server

Storage servers are abundant and most of them are free(Github, Google Drive). Getting hands on a Compute Server, that is free is almost non-existent today. This scarcity can be attributed primarily to the mis-use of the compute resources. Second the Cloud providers have completed their Free-offer period as most of the customers have converted to paying customers.

Secondary Challenges:

The servers that can be hosted in Cloud platforms like Digital Ocean/GCP or AWS will require the following

— OS and API server administration

— Network management

— DNS and SSL certificate management

These tasks are important, but takes away precious time that can be used for improving the API server. What is the way out then?

Glitch : A solution by Chance

Exploring the options were useful in their own way. Each option above helped me in fine-tuning and debugging the API code. I checked if Kaggle notebook can be used for hosting the server, only to realize that each Kaggle notebook is a docker instance that can’t open any port to the internet. Streamlit library makes it very easy to host a “single url” API along with its front-end. The multi-url APIs might be possible to host with the “multi-pages” option, which will require additional time to research and implement. Github pages themselves are API end-points. The markdown or json files in the repo are rendered as JSONs when page is called by a browser.

I had used Glitch with Node Express and Svelte-kit servers in the past. So I casually searched in Google, “python fastapi on glitch”. The first result was below GitHub repo

https://github.com/cprinos/glitch-fastapi

It shared the “Glitch.json” configuration file. The configuration, takes care of running the server and install python libraries. Bang, I got the API server running and serving data as programmed.

{
"install": "pip3 install --user -r requirements.txt",
"start": "uvicorn main:app",
"watch": {
"ignore": [
"\\.pyc$"
],
"install": {
"include": [
"^requirements\\.txt$"
]
},
"restart": {
"include": [
"\\.py$",
"^start\\.sh"
]
},
"throttle": 1000
}
}
/biker/v1/pickupd endpoint in action: delivering the data

The first request to the API server will be slow, since the underlying server has to be “woken up”(read provisioned) and then the API has to be served. In production the speed can be improved by using best practices, and dedicated servers.

Conclusion:

There are multiple ways to solve a problem. The biggest challenge is to find the cost-effective and efficient way to show case the solution. Researching multiple options and learning the tech might be overwhelming at the beginning. Once many tech stacks are explored, the underlying pattern becomes evident, like the case of Glitch.com. Even though Glitch is touted as solution for hosting Javascript APIs and servers, it is still a “Linux Server” that can be configured if we understand how its configuration works.

--

--

Qrious Kamal

Name says it. Ever qrious and searching for getting thing done in the easiest possible route. And then document the challenges.