Create your first API using FastAPI

Being a frontend developer I was always fascinated by how the backend work and these APIs are so cool, they let us fetch data just by sending a request. So, to quench my thirst for learning I tried to make an API by myself using Python and it turned super amazing experience to see the API work that is made on my own.

So, without any further delay let's start.


Setup

  • Create a new folder name "PyAPI" or whatever you wish to name it

  • Go to the folder and open VSCode or any editor of your choice.

  • Run the following command in the terminal to install fastAPI:

  •   pip install "fastapi[all]"
    

    Note: The `all` in the square brackets means that we want to install everything from fastapi.

  • Now, in your folder create a new file name "main.py"

In the main.py file, we will write all our API code.


Code

  • Import fastapi :

      from fastapi import FastAPI
      import random
    
  • Initialize fastapi:

      app = FastAPI()
    
  • Create our main endpoint

      @app.get("/")
      async def root():
          return {"example": "This is an example", "data": 0}
    

    The path of the endpoint is "/". It means if anyone sends a fetch request to the domain without any path specified they will get the data that is returned from the root function.

  • Create second endpoint:

      @app.get('/random')
      async def get_random():
          rn:int = random.randint(0,100)
          return {"number": rn, "limit": 100}
    

    If anyone goes to the path: `127.0.0.1:8000/random` then they will get the data that is returned from get_random.

  • Create third endpoint

      @app.get('/random/{limit}')
      async def get_random(limit: int):
          rn:int = random.randint(0,limit)
          return {"number": rn, "limit": limit}
    

    Here, we are using dynamic parameters to return data based on the URL.


Test API

To run the server, if you are on windows run the following command in the terminal:

python -m uvicorn main:app --reload

For others:

uvicorn main:app --reload

Note: --reload flag will automatically refresh our API data on any change and we will not need to do this manually

  • In the browser go to "http://127.0.0.1:8000"

  • You will see the data returned from our main endpoint

  • Change URL to "127.0.0.1:8000/random" and you will get data from our second endpoint

  • Change to "127.0.0.1:8000/random/23" or any number you would like and you will see that data is returned based on the number you have added.

  • If you pass a parameter other than an integer you will get an error as the API was expecting an integer

  • If you go to any other path that is unknown then, you will get not found from API.


Plus Point

FastAPI also automatically generate docs for your API and you can get them by going to "127.0.0.1:8000/docs"


Source Code

You can get the source code from GitHub and it also contain many useful comments that will help you understand the code motive easily:

https://github.com/Palakkgoyal/PythonAPI


Credit

If you want to learn this in video format then, you can check out this amazing video tutorial by Indently

https://www.youtube.com/watch?v=F43rgxq4CKw


If you have any queries, let me know in the comment section.

Thanks for reading :)