Deploying FastAPI application to Render
Tags: python, beginners, programming, productivity
Published: 2022-11-02 06:09:37

Recently, I came to know that Heroku is going to stop supporting free services. I have almost all the projects running in Heroku and I never tried any services. Many people pointed out that [Render](https://render.com/) is the best free alternative to the Heroku. So I am giving it a try by hosting a FastAPI application. Render seems to directly support python frameworks like Flask, Django etc as their documentation mentions them. But we should be able to host FastAPI app as it supports building any python app, we just need to change the starting command. Let's get into it without wasting another minute.

## Creating [Render](https://render.com/) Account
To host on Render, you'll need to sign up or create an account. It's free and supports direct login using google, GitHub and GitLab account. Or you can use email and password to signup. You need to verify your email to get full access to the platform. Just head to [render/register](https://dashboard.render.com/register).


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667276301389/gt22ZUVp5.png)

## Need for GitHub or GitLab account
It's easier to deploy an application that has its source code hosted on GitHub or GitLab. Render connects with GitHub or GitLab to deploy your apps and websites automatically on every push to your project. This is very useful, as you don't have to manually deploy every time you make a change. You can either connect [GitHub](https://github.com/signup) or [GitLab](https://gitlab.com/users/sign_up) or both. 


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667288724821/nJSpN9UyJ.png)

## Let's make a simple API using FastAPI
We need an application to deploy to the Render. Let's build a simple API app which echos `Hello World`.

### Setting up environment
You'll need to download and install [Python3](https://www.python.org/) to your local machine. It's better to set up a virtual environment for the application.
#### Windows
For windows, just head to [python.org](https://www.python.org/downloads/) and download the latest EXE file and install. Installation is pretty well interactive, you just need to follow the instructions shown.

Next, create a folder in your preferred directory and name `simple-api`. Next to set up virtual environment enter the command opening terminal or CMD.
```bash
py -3 -m venv .venv
.venv\scripts\activate
```
#### Linux or Mac OS
> Most of the Linux Distributions come with python3 be installed by default, just check if it exists by using command
 
```
python3 --version
```
For mac users just head to [python.org](https://www.python.org/downloads) and download the .pkg file and start installing.

Next to set up virtual environment, on Linux sometimes we need to install extra library. To do the same, just enter the command in your terminal. 
```bash
sudo apt-get install python3-venv
```
Next, to initialize and activate a virtual environment on Mac and Linux, use the commands below.
```bash
python3 -m venv .venv
source .venv/bin/activate
```
## Installing requirements
Make sure your package manager that is `pip` is up-to-date.
 ```bash
python -m pip install --upgrade pip
 ```
Now install FastAPI, using `pip`
```bash
pip install fastapi
```
FastAPI also requires an ASGI server, for production, such as Uvicorn or Hypercorn. I personally just use Uvicorn. 
```bash
pip install "uvicorn[standard]"
```

## Coding the API
Open the folder in any text editors, you can use notepad or advanced IDE's like VS Code or Sublime Text 4. Make a new file called `main.py` and we use this file to write the code in.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667297849372/ABQUYVOTQ.png)

Just copy and paste the below lines to the file. If you're good with python or professional, then you can add or modify, making the API better.

```python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}
```
Save the file. Let's try out the simple-api using the command: 
```bash
uvicorn main:app --reload
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667298287004/x2tXl1-jA.png)

Head to `localhost:8000` or `http://127.0.0.1:8000`. You should see the `Hello World`.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667298527955/vWaggp7uH.png)

All done. Now we just need to create some extra files which are need by Render to identify it as a python application.

## Creating Requirements.txt
Usually I use external library [pipreqs](https://pypi.org/project/pipreqs/), but you can just simply type  `fastapi` and `uvicorn` in the requirements.txt file. When deploying, render will install the latest version of these modules.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667299816413/yHl-oQCm4.png)

## Creating a .gitignore file
`.gitignore` file helps to avoid uploading of unwanted or private files to the git clients such as GitHub or GitLab. When sharing your code with others, there are often files or parts of your project, you do not want to share. So these files or folders can be mentioned in the .gitignore file.

For this simple-api we don't want to upload `.venv` and `__pycache__` to the GitHub. So we mention them in the .gitignore file like this.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667300636574/GVe_fPtPG.png)

## Upload to GitHub or GitLab
I am using the GitHub here, but you can use GitLab as well. I have installed [git](https://git-scm.com/download/), and I have connected it to my GitHub account. You'll find many articles on how to do it, just google it. If you're using Visual Studio then it has in built GitHub plugin which makes it way easier.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667300190867/CSsAfm85e.png)

I have successfully uploaded to GitHub, you can find mine at [akashrchandran/simple-api](https://github.com/akashrchandran/simple-api). You should definitely add a readme.md to the repo, I will do it later.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667300972758/ehibPeKFM.png)

## Deploying to Render.com
As we are not deploying a static site, we should choose the option `Web Service`.


![firefox_6Ko2SbNN6r.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667302637873/HPSesfyBd.png)

If you have connected your GitHub account, then you can just search the repository name and find it there.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667302811886/LFMSurQ_s.png)

Give a name to your web-service, then to select the region which is nearest to you, for me is Singapore. Leave all other values default. For the Start command, copy and paste the below: 
```bash
uvicorn main:app --host 0.0.0.0 --port 10000
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667303284903/-M7LNMQEs.png)

Deploying may take some time, so patiently wait. After successful deployment, you should see the message like this below.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1667303740868/ZbGt5Czak.png)

Congratulations, you have successfully deployed your FastAPI application to the render platform. You can view the site I deployed [here](https://simple-api-hr3q.onrender.com/).

## Summary
It was so simple to deploy a FastAPI based application to render. If you don't want to code or waste time on making a FastAPI based app, then you can try deploying mine. It is available on [GitHub](https://github.com/akashrchandran/simple-api).

[Render](render.com) may the Heroku alternative thing you're looking for, as it gives out almost the same free specifications as Heroku free tier. Do give it a try!

Thanks for reading...