Creating a virtual environment

To create a virtual environment run the below command

py -3 -m venv <name-of-your-choice>

To make sure the terminal also is using the virtual environment run the following command to activate

Installation

To install FastAPI run the following command

pip install fastapi

  1. Getting started with the small beginner snippet. Create a main.py file and put the code below and then run the server using fastapi dev main.py
from fastapi import FastAPI

app = FastAPI();

@app.get("/hello")
def helloWorld():
    return {"Message": "Hey hope you will enjoy the fastAPI learning"}

Path Parameters

from fastapi import FastAPI;

app = FastAPI()  #FastAPI is a class that provides all the functionality for your API

items = {
    1:{
        "id": "1",
        "name": "glass"
    },
    2:{
        "id": "2",
        "name": "bottle"
    },
    3:{
        "id": "3",
        "name": "mic"
    },
}

@app.get("/item/{itemId}")
def hello(itemId:int):
     if itemId in items:
        return items[itemId]
     else:
        return {"Message": "Item not found"}

In the above example, the value of the path parameter itemId will be passed to the function as the argument itemId.