Creating Python API Using FastAPI Framework.

First you need to install FastAPI using pip.

pip install fastapi

If you are getting any error like No matching distribution found for fastapi you are probably using pip from Python2. Check this post on how to resolve the issue.

Now once you have installed fastapi, you need to install uvicorn.

pip install uvicorn

Create a file called app.py. Import the FastAPI.

from fastapi import FastAPI

Create an app using fastapi.

from fastapi import FastAPI
my_app = FastAPI()

Using the my_app define an endpoint /getData.

from fastapi import FastAPI
my_app = FastAPI()

@my_app.get("/getData")
def getData():
    return {"employees": [{
      "firstName" : "Roy"
    },{
      "firstName" : "Sam"
    }]}

Now save app.py file. From you command prompt using the uvicorn server, run the app.

uvicorn app:my_app

app is the file name and my_app is the app name that we used inside the file. Once the app runs, you’ll be able to access the endpoint from http://localhost:8000/getData.