FastAPI Overview

fastapi tutorial

In this article, we’ll see the FastAPI Overview.

What is FastAPI?

It is a web framework for developing RESTful APIs with Python 3.6+ based on standard Python type-hints that is current and quick (high-performance).

RESTful APIs enable you to develop the back end separately from the front end and still have them work harmoniously together even if separately hosted.

FastAPI was Created by developer Sebastián Ramírez and initially released on December 8, 2018, and stable release on January 23, 2022.

The primary focus of it is, in

  1. Fast development
  2. Fewer bugs
  3. High and fast performance.

Companies like Netflix and Uber are using this framework which fully supports asynchronous programming and can run with WSGI(Web Server Gateway Interface) and ASGI(Asynchronous Server Gateway Interface).

Benefits:

The followings are some of the benefits:

  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. *
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *
  • Intuitive: Great editor support. Completion everywhere. Less time debugging.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust: Get production-ready code. With automatic interactive documentation.

Installation:

Before proceeding with installation, you should have python 3.6+ preinstalled. Follow below steps:

step 1. Run “pip install fastapi” command in terminal

fast api installation step 1

step 2: Once fastapi is installed as per the above step, we need to set up “uvicorn” as a server. so, run “pip install “uvicorn[standard]” –user” command in terminal

fast api install step 2

step 3: now, uvicorn and fastapi is installed successfully. Let’s create a “main.py” file with the following code and save it

from typing import Union

from fastapi import FastAPI

app = FastAPI()

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

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

 

Step 4: Open a terminal and go to the path where the above file was created. after then, run the “python -m uvicorn main:app –reload” command in your terminal. your application is now running. Now, open “http://127.0.0.1:8000/items/5?q=somequery.” URL in your browser and you will see the following output:

fast api example

You already created an API that:

  • Receives HTTP requests in the paths / and /items/{item_id}.
  • Both paths take GET operations (also known as HTTP methods).
  • The path /items/{item_id} has a path parameter item_id that should be an int.
  • The path /items/{item_id} has an optional str query parameter q.

Your application is ready.

Now go to http://127.0.0.1:8000/docs. You will see the automatic interactive API documentation (provided by Swagger UI)

And now, go to http://127.0.0.1:8000/redoc. You will see the alternative automatic documentation (provided by ReDoc)

You can follow below for video for installation process if you need:

Advantages and Disadvantages:

Advantages:

  • It helps in validating the datatype from the developer even in nested JSON requests.
  • It gives the autocomplete feature which helps in producing applications with less amount of effort and also less time in debugging. 
  • It is based on and also compatible with the OpenAPI.
  • It makes it easy to build a GraphQL API with a Python library called graphene-python.
  • It integrates well with OAuth 2.0 and external providers.

Disadvantages:

  • It is a relatively new framework so the guideline community is small. We have lower external educational information like books, courses, or tutorials.
  • Since in the development of the applications we need to tie everything together in the FastAPI application which causes the main file to become very long or crowded.

References:

Hope this article helps!

Write a Reply or Comment

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