Getting Started With FastAPI

image from unsplash

hello welcome to my other blog post, in this post we will discuss another way to build API with python, we will talk about Fast API how to build API with this framework, any better way to learn this framework, and why use this framework, in this post, we will discuss it thoroughly, see the discussion below

what we will discuss

  • Introduction To FastApi
  • Project Preparing
  • Build Simple Project with FastAPI

Introduction To Fast API

Projects Preparing

How To Install FastAPI

FastAPI can download in pip

pip install fastapi

install uvicorn or search on pypi

pip install 'uvicorn[standard]'
fast API install

after all required package has installed, let’s create a sample project

Build Sample Project with FastAPI

python -m venv getting_venv

then the virtual environment has created, activate with this command

source gettting_venv/bin/activate

the second steps create a file named then fill it like this

from fastapi import FastAPI

# init app
app = FastAPI()

# route
def read_root():
return {'hello':'world from fast api'}

now we discuss, what we actually wrote earlier

in the first line, we import class FastApi from fastapi module, then initialize the app in the second line

app = FastAPI()

well, then we just create a route for our application

# route
def read_root():
return {'hello':'world from fast api'}

this route will return {'hello':'world from fast api'} if you run app with uvicorn use this command

uvicorn main:app --reload

you will see

response from the route

if you see the server response in a terminal you will see

server response from the terminal

Passing Dynamic Route

now we will create a new route for passing dynamic route, in some condition we need to add some query in route like it will change dynamically, the solution is to store a URL query in a variable.

let’s create a new route

# new route
def get_item_name(item_id: int, query: Optional[str] = None):
return {
'item_id': item_id,
'query': query

let’s run and see the response, input http://localhost:8000/item/1?query=clotes for example you will see the response

passing dynamic URL

The Amazing Feature of FastAPI

interactive documentation from FastAPI

so that when we connect our API with other applications, we can avoid route errors.


Excited With Python Linux, and Love Code, Writing and Many others

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store