Introduction to FastAPI

Programming Image

    What is FastAPI?

    FastAPI is a modern, high-perfomance, web framework for building APIs with Python 3.6  or more based on standard Python Type Hints.

    Feature of FastAPI

    • Its also the Fastest Python web frameworks. In fact, its speed is at par with Node.js and Go.
    • Its also detailed and easy to use developer docs
    • Easy to create plugins using dependecy injection 
    • Use of type hint on your code to get free vlidation and conversion

    Requirement to use Fastapi

    Python 3.6 or more 

    FastAPI stands on the shoulders of giants 

    • Starlette - for the web parts
    • Pydantic for the data parts

     

    Installation

    To Install Fastapi run below command on bash or any other terminal you are using.

    pip install fastapi

    if you are behind a proxy 

    pip install --proxy http://proxy-username:proxy-port fastapi

    You will also need an ASGI server, for production such as uvicorn or Hypercorn.

    To install Uvicorn use command 

    pip install "uvicorn[standard]

    To install hypercorn use command

    pip install hypercorn

    Lets do an example.

    Create a file main.py and add code as below:

    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    async def root():
        return {"descrption": "FastAPI 101"}
    

    The run the server with 

    (venv) PS C:\Users\username\PycharmProjects\fastapi> uvicorn main:app --reload
    INFO:     Will watch for changes in these directories: ['C:\\Users\\warui\\PycharmProjects\\fastapi']
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Started reloader process [6704] using watchgod
    WARNING:  The --reload flag should not be used in production on Windows.
    INFO:     Started server process [12412]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    

    Test on the browser:

    open your browser at http://127.0.0.1:8000 

    You will see below 

    Let add  new apis as below.

    from typing import Dict, Any
    
    from fastapi import FastAPI
    
    import uuid
    
    app = FastAPI()
    
    # declare a new dictionary
    animals = dict({})
    
    
    @app.get("/")
    async def root():
        return {"description": "FastAPI 101"}
    
    
    @app.post("/api/v1/add_animal")
    def add_animal(name: str):
        # add new animal with a specific uuid on the dictionary using the post method
        animals[uuid.uuid4()] = name
        return animals
    
    
    @app.put("/api/v1/update/{animalid}")
    def update_animal(animalid: str, name: str):
        # update animal 
        animals[animalid] = name
        return animals
    
    
    @app.get("/api/v1/animals")
    def get_animals():
        # get all animals
        return animals
    
    
    @app.delete("/api/v1/delete/{animal_id}")
    def delete_animal(animal_id: str):
        # delete an animal
        animals.pop(animal_id)
        return animals
    

     

    Then we run our application using uvicorn as below and try access our apis

    (venv) PS C:\Users\username\PycharmProjects\fastapi> uvicorn main:app --reload
    INFO:     Will watch for changes in these directories: ['C:\\Users\\username\\PycharmProjects\\fastapi']
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Started reloader process [8224] using watchgod
    WARNING:  The --reload flag should not be used in production on Windows.
    INFO:     Started server process [13736]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.   
    INFO:     127.0.0.1:51692 - "GET /docs HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51692 - "GET /openapi.json HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51691 - "POST /api/v1/add_animal?name=cow HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51701 - "POST /api/v1/add_animal?name=rabbit HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51703 - "PUT /api/v1/update/7bfae389-2578-4a97-a258-a922c4fdf936?name=calf HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51399 - "GET /api/v1/animals HTTP/1.1" 200 OK
    INFO:     127.0.0.1:51400 - "DELETE /api/v1/delete/7bfae389-2578-4a97-a258-a922c4fdf936 HTTP/1.1" 200 OK
    

    You can see we are able to access all our apis successfully

     

    UtilizatingFastAPI docs

    Go to http://127.0.0.1:8000/docs

    The Interactive API documentation will be automatically updated, including the new  apis as below,

    To execute an apis click as below

    When you hit execute a new have will be added to dictionary 

    You can test the other apis as well for updating an animal, deleting an animal and getting all animals.

    Thats all for today.

    For reference visit thr FastAPI documention page FastAPI docs

    Happy coding :)

    0 Comments

    To add a comment, please Signup or Login

    OR