In this first post of a new series, we will get started with FastAPI. We will create our first FastAPI application from scratch. In doing so, we will understand the basic moving parts of a typical FastAPI application.

Pre-requisite for learning FastAPI is to have basic knowledge of Python. Also, you should have Python 3.6+ installed on your system. In case you are new to Python or need a refresher, you can check out this post on the basics of Python for reference.

1 – What is FastAPI?

FastAPI is a high-performance framework for building web APIs using Python3.6+. As per the claims by the creator of the framework, FastAPI performance is on par with NodeJS or Golang.

Over the last year, it has started gaining good amount of traction in the industry and development community. Basically, FastAPI is being adopted as an alternative to Flask. The aim of FastAPI is to help developers build APIs quickly at a production-ready level.

2 – Installation

The first requirement of FastAPI is Python 3.6+.

Next, we have to install the FastAPI package itself. See below command:

$ pip install fastapi

Internally, FastAPI uses libraries such as Starlette and Pydantic. However, we don’t have to install them separately.

We also need an ASGI server to serve our application. The choices are Uvicorn or Hypercorn. Personally, I have seen Uvicorn being more widely used for FastAPI applications.

To install Uvicorn, we use the below command:

$ pip install "uvicorn[standard]"

3 – The First FastAPI Application

With the installation out of the way, we can create our first FastAPI application.

Create a project folder and inside that folder create a file main.py. See below:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def greeting():
    return {"Hello": "World"}

And that’s about it.

Let us understand what’s going on in this tiny application. In the first statement, we basically import the fastapi library (specifically the FastAPI function). Then, we use it to create an app object.

In the next step, we use the app object to create request handlers. The function greeting() is a request handler. Basically, all the requests to the root path of our application will be handled by this function. Within the function, we simply return a JSON object. In other words, we have built our first API using FastAPI.

4 – Run FastAPI Application

Running the application is quite straightforward. We use the below command:

$  uvicorn main:app --reload

The command refers to main.py file. Basically, main is the name of python file. The app in the main:app is the object created by calling the FastAPI() function. The flag –reload makes the server restart after any code changes. Basically, this makes it easy for developers to quickly test changes to the application. We should use this only during development phase.

Also, by default the server starts up on port 8000. You can access the application at http://localhost:8000/

5 – Interactive API Docs

The great part of FastAPI is that it takes care of API documentation out-of-the-box. In other words, we can simply go to http://localhost:8000/docs to view the API documentation. See below:

fastapi first application

As you can see, the automatic documentation uses SwaggerUI.

There is also the option of using ReDoc. We can access the same on http://localhost:8000/redoc. See below screenshot:

fastapi redoc

Conclusion

With this, we have successfully created our first FastAPI application.

Basically, we installed the requisite packages and also created our first application that contains a single API endpoint. Also, we looked at how to access the automatically generated API documents.

If you have any comments or queries, please do write in the comments section below.

Categories: BlogFastAPI

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *