"Unlocking the Power of FastAPI: A Comprehensive Introduction and Step-by-Step Tutorial for Building Your First FastAPI Application"

"FastAPI: The Modern API Framework"

What is FastAPI?

  • FastAPI is a Python framework for building APIs and it is fast as it supports async programming.

  • It was launched in 2018 and hence it is relatively new. To use Fast API you need to use Python 3.6 and above.

Features of FastAPI

Performance

  • Fast API is a speed-oriented framework and performance is the priority.

  • Its speed is on par with NodeJS and GO. It performs far better than DjangoRestFramework and Flask.

Flexibility

  • It is also more flexible as compared to DRF as in DRF we need to do things in a certain way

  • FastAPI is flexible as it doesn't restrict user to a particular code layout.

Production Ready

  • Fast API can be used to create production-ready APIs that could be deployed instantly.

  • It provides interactive, automatic documentation

Faster code and intuitive

  • It is much faster to build/develop an API as compared to other frameworks

  • It is also Intuitive as it provides editor support for VS code as well as PyCharm, therefore it becomes easy for developers to write code with an auto-completion feature.

Other Features

  • Fast API has an additional feature i.e. it validates developers' data type even in a deeply nested JSON request.

  • While creating a schema for your API, you can also define the data types of every field.

  • You can build GraphQL API using FastAPI

  • FastAPI is built on standards like JSON, OAuth and Open API

Creating a Simple API

Installing FastAPI

Before starting to write API it's a good practice to have a virtual environment created and install dependencies in that environment. Hence therefore I'll first create a virtual environment and thereafter install FastAPI.

  • Start off by creating a virtual environment named .venv.

  • Then activate the environment using the command source .venv/bin/activate .

  • Install fastAPI once the environment is activated using the command pip3 install fastapi.

  • Once fastAPI is installed a server is also needed to run the api hence install uvicorn using the command pip3 install uvicorn.

python -m venv .venv
source .venv/bin/activate
pip3 install fastapi
pip3 install uvicorn

Creating Simple API

  • Create a file named main.py . This file will contain API code.

  • We start by importing FastAPI class from the fastapi module so that we can create an instance.

      from fastapi import FastAPI
    
  • We create an "app" instance. This "app" variable will be the main point of interaction to create our api.

      app=FastAPI()
    
  • We need to create an API that will handle the request from the client in our case web browser and send back some response.

  • The question now arises how exactly does the browser send a request?

  • Whenever we type a URL in a browser that particular URL is nothing but it's a request and the API is responsible for taking the request and sending back an API response. The question is how will API do that?

  • To do that we create a function in an API. A simple regular Python function named index. This function will return something in this case say "Hello World!".

      def index():  
          return "Hello World!"
    
  • Whenever we get some request this function needs to be executed. But how does this happen? We add a decorator in Python and make use of app instance. We want to send "GET" request from end-user or client.\

      @app.get('/') 
      def index(): 
          return "Hello World !"
    
  • Save the file and then run the API using uvicorn. The --reload flag ensures whenever you make changes to the API it'll automatically restart your server.

       uvicorn main:app --reload
    
  • Once you run this command the server is started and will provide you a specific address. When you go to that address you'll see "Hello World!" displayed in your web browser.

  • We can similarly create different functions and link them to different routes and see the content that function returns hitting that endpoint.

Complete Python Code and Output

from fastapi import FastAPI #class
app=FastAPI() #class instance

@app.get('/new')
def new():
    return "This is a new page"

@app.get("/fruits")
def fruits():
    return  {"Fruits list":{"Mango","Apple"}}

Output of hitting /new endpoint.

Output of /fruits endpoint.