Create your first RestAPI using FastAPI

Godlin Hilda J
featurepreneur
Published in
4 min readMay 25, 2021

FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.6+ based on standard Python-type hints. In this article, we will see how to setup FastAPI and create a simple application

Step 1 :

We should first install FastAPI. For this, open your terminal and type the following command

pip install fastapi

Step 2 :

Now, we must install a server to run our app. For this, we can use both uvicorn or hypercorn. In this article, we will be using hypercorn

pip install hypercorn

Hypercorn is an ASGI web server based on the sans-io hyper, h11, h2, and wsproto libraries and inspired by Gunicorn.

Step 3 :

Now that we have installed all the necessary lib. Let’s start with our app.

Create a new python file named main.py . First, we must import the FastAPI we just downloaded

from fastapi import FastAPI

Now create an instance — app

app = FastAPI()

We can then used the app the instance that we just create to use HTTP methods like get, post, delete, etc

@app.get('/')
def Home():
return {"message" : "Hello"}

Here we are using .get method and returning a simple message to check if our application is running.

In order to run our application, open the terminal and type the following command

hypercorn main:app --reload

Let’s understand the above line — Hypercorn is the server used to run FastAPI, main is the name of our file, app is the instance that we created. We are using --reload so that the server will automatically reload itself if it detects any changes in the code.

If you see this, it seems your server is up and running. We can check it by going to localhost:8000 and you will see the following output

Now that we know that our application is working properly, Let us create different routes that use different methods.

For the simplicity of the article, we will use a list in place of DB. Create a new list called fruits .

fruits = []

Let us now create another route to add fruits to our empty list. For this, we use the .post method

@app.post('/add_fruits')
def add_fruits(fruit):
fruits.append(fruit)
return {"fruit added" : fruits[-1]}

As it is a post method, we are getting input in our case it is a fruit and appending it to our fruits list. Finally, we are returning a dict that has its value as the fruit that we just added.

Now is the perfect time to talk about the most useful and important feature of FastAPI. If we are using other frameworks, we would need to depend on a third-party application like Postman to check if our post method is working correctly. Whereas in FastAPI, they have provided us with not one but two pieces of ddocumentation to test our API calls.

In order to use their documentation, go to localhost:8000/docs (make sure that your server is running)

This is the Swagger UI. We can also access the second docs by going to localhost:8000/redoc . For this article, we are gonna use the Swagger UI.

We can see that we have a post request that we just defined. In order to check it, click on the post and select Try it Out .

Enter the name of the fruit and click on Execute

If we scroll down, we can see our result that is returned by our add_fruits function

Now, we will create a route to return all the fruits in our list

@app.get('/get_fruits')
def get_fruits:
return fruits

As we are now comfortable with the post and get method, let us try out the delete method to delete the fruit in the given index

@app.delete('/delete_fruit/{fruit_index}')
def delete_fruit(fruit_index :int ):
fruits.pop(fruit_index)
return fruits

To delete we use the .delete method. We are passing the index as a URL parameter. In the function delete_fruit, we are specifying that the index that we are receiving is an int cause by default it will be a string and for pop() we only need a int

We are deleting the fruit in the given index and finally returning our entire list.

Let’s now start our server and test out all the routes in Swagger UI

Add many fruits to crowd our list and using the get_fruits the route, display all our fruits.

Then you can delete the fruit in a particular index by specifying the index.

Here is the completed code

Hope you found this article useful.

Keep Exploring!!!

--

--