Getting Started with FastAPI

Vaidhyanathan S M
Nerd For Tech
Published in
3 min readOct 27, 2021

In this tutorial, we are going to explore FastAPI, more specifically building RESTful APIs and deploying the application on server.

FastAPI is a Web framework for developing RESTful APIs in Python. It fully supports asynchronous programming and can run with Uvicorn and Gunicorn. Uvicorn is a lightning-fast ASGI server implementation.

We’ll first start by creating a basic FastAPI application and then move on to deploying it on Deta.

Create a basic FastAPI app

Create a directory for your application and create a new file named main.py. This is the entry point of your application. We’ll be creating the endpoints in this file.

Before creating endpoints, we need to define data. Let’s consider a simple example of students. Let’s create a JSON representation of the details of students. After that, let’s create the endpoints for retrieving the data based on different scenarios.

As it can be seen from above, the data is stored in students, which consists of two elements with the id and corresponding object. The object consists of details such as name, age and class.

Let’s consider all the scenarios. Look at the first scenario; data to be retrieved when hitting the index page. “@app.get()” specifies that we are using GET method here. Also, the type of parameter here is path, and the path for this scenario is “/”. The second scenario is retrieving the data of a student with particular id. The path here is “/get-students/{student_id}” where student_id is an integer. The third scenario is retrieving the data of a student with a specific name. Here we make use of the query parameter. The path “/get-by-name” is appended to the URL and the query name is passed. In the third method, we are traversing the list of students and checking for a student whose name matches with the one provided by the user in the query. When a match is found, that particular object is returned. In the final scenario, the whole list of students is returned. The path here is “/get-students/”.

Now that we have implemented the endpoints, let’s move on to the next step.

Requirements

Now, in the same directory create a file requirements.txt with :

fastapi

Deployment

The first step in this is to create an account on Deta. The next step is to install the CLI version of Deta. If you are on a Linux machine, you can execute the following command on terminal.

curl -fsSL https://get.deta.dev/cli.sh | sh

After installing it, open a new terminal so that the installed CLI is detected. In the new terminal, confirm that it was correctly installed with:

deta — help

The next step is to login to Deta from the CLI.

deta login

This will open a web browser and authenticate automatically.

The final step is to deploy the application. Execute the following command inside the project directory.

deta new

You’ll see a JSON message similar to :

Now, open your browser with the endpoint URL. In the above example, it was https://hwzlsi.deta.dev/, but yours will be different. You can test the API using different scenarios as discussed before. You can also perform interactive testing by going to /docs of your URL. It’ll show your docs like :

Congrats! You deployed your FastAPI app to Deta! 🎉 🍰

We come to the end of this tutorial. Thanks for reading ! Happy Coding !

If you have any queries please post in the comment section below. Connect with me on LinkedIn. Also, if you want to look at my amazing collection of apps developed, don’t forget to check Google Play Store.

Know more about me here.

--

--

Vaidhyanathan S M
Nerd For Tech

Systems Engineer @TCS | Native Android Developer | Enthusiastic Programmer | Skilled in Python, C/C++, Java, Flutter and Flask.