Make Money From API [Part II – Deploying The API]

In our previous tutorial, We discussed How to make an API using Python and FastAPI. In this tutorial, we will deploy our API into a cloud service. There are many free options for that like Render, Vercel, Koyeb, and so on. But for the sake of simplicity, we will be using Render.

To proceed to the next part you need to follow the previous tutorial. So go ahead and read: Make Money From API [Part I – Building An API]

Now let’s get started with this tutorial. To deploy our app we need to follow 3 steps:

  1. Create Necessary Files
  2. Push our project to GitHub
  3. Log into Render and deploy the App

1. Create Necessary Files:

We will need to create a requirements.txt file that will contain the name of the Python packages which the system has to install. We are only using 3 packages, the fastapi, uvicorn, and requests. So, add those names to your requirements.txt file like this:

Creating requirements.txt File
Creating requirements.txt File

 

Keep the app.py and requirements.txt files in the separate directory.

2. Push our project to GitHub

  • Before you move any further it is important to save GIT BASH installed in your system to push your files. If you haven’t set it up yet please read our tutorial on Set Up GIT to Push Projects To GitHub. Check that out first.
  • Alright, now go to GitHub, login, and create a new repository. Fill in the fields or you can follow me along.
Creating A repository on GitHub
Creating A repository on GitHub
  • Click Create Repository. And don’t forget to copy the SSH URL from the next page.
Getting SSH URL of the Repo
Getting the SSH URL of the Repo
  • Now head over to the directory where you have stored the necessary files. And right-click in a blank space and open the command prompt.
  • Initialize git using git init
  • Add your SSH URL to the directory. Type git remote add origin <your-ssh-url>
  • Create a branch. We have created a master branch using git branch -M master
  • git add . to add all the files
  • git commit -am "commit name" to commit the changes
  • And finally git push origin master to push the files to your GitHub repository
Pushing Necessary File to GitHub Repo
Pushing Necessary File to GitHub Repo

3. Log into Render and deploy the App

  • Log into Render using GitHub.
  • Once you’re logged in click on New + dropdown menu and select Web Service.
  • Now Connect the repository you just created. Give your project a name.
  • Type in pip install -r requirements.txt in the Build Command field.
  • Type python app.py in the Start Command command.
  • Hit Create Web Service and boom we are done.

The deployment will start and you see something similar.

Render Deployment Logs
Render Deployment Logs

Once you see Deployed in the logs, you can go to the app URL.

App Successfully deployed on Render
App Successfully deployed on Render

 

This is about it. See you in the next one.

Feel free to use my repository if you’re finding it difficult to follow along https://github.com/TerminalWarlord/eSpaceHub-API-Tutotial

Leave a Reply