- read

Create a Simple RESTful API in Five minutes

Balaji Yaswanth Vankala 3

⚙ API ⚙
API — Application Programming Interface

Before getting started let’s understand what an API means, As you all might know API stands for Application Programming Interface. Wait, What’s that..?

Let’s take a look at the Wikipedia-level definition — An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, that offers a service to other pieces of software.

Didn’t understand much? Don’t worry! Here’s the simplified version of the above definition — An API is a medium through which two computer programs can “talk”. Here, “talk” refers to the exchanging of data.

There are a plethora of APIs in the world. Today we will be talking about RESTful APIs.

Analogy of an API
An analogy of API, Image-source — fireship.io

We will be creating a RESTful API. A RESTful API is one of the many possible ways that programs, servers, and websites can share data. A RESTful API organizes data into a bunch of unique URLs( URIs ). The data can be anything (eg. form-data, weather data, product details) pretty much any meaningful data.

Let’s take a look at an example for better understanding:-

A URL that GETs data of burgers available

The above URL ( API endpoint ) gets the data of all the burgers. If sandwiches are also available at the store, then by changing the endpoint we can get desired results.

Now you should have a good understanding of what RESTful APIs are. I am so thrilled to have you reached here.

Yay! Now let’s start coding. 🚀

We will build our REST API in Python using FAST API⚡. FastAPI is a modern Web framework for developing RESTful APIs in Python.

Requirements:-

I am using PyCharm. You can use any Text editor or IDE of your preference.

1. Installing FAST API and Uvicorn:-

Create a New Project.

As I said earlier we will be using the FAST API web framework for building API. So let’s install it in our project using the following command:

pip install fastapi

After it is installed, we need to install Uvicorn — A web server implementation for python.

pip install uvicorn

2. Create and Initialize file:-

Create a new file api.py in your project. Include the following code to initialize the file.

from fastapi import FastAPI

# creating API
app = FastAPI()

3. Creating menu routes:-

Now that we have initialized the API we need to create routes. We will be creating 3 routes.

  1. Default route
  2. Burgers route
  3. Sandwiches route
# default route
@app.get("/")
async def root():
return {
"welcome_message": "Hello hungry Zucky, welcome!",
"menu": [
{
"burgers": "http://localhost:8000/api/v1/menu/burgers"
},
{
"sandwiches": "http://localhost:8000/api/v1/menu/sandwiches"
}
]
};

# burgers route
@app.get("/api/v1/menu/burgers")
async def burgers():
return {
"spicy burger": "10$",
"cheesy burger": "12$",
"Extra cheesy spicy burger": "18$"
}

# sandwiches route
@app.get("/api/v1/menu/sandwiches")
async def burgers():
return {
"Egg sandwich": "10$",
"cheesy sandwich": "11$",
"Chicken sandwich": "13$"
}

Congrats, Your REST API is created.

4. Spinning up server🚀

Finally, our REST API is created. To check out the results enter the following command.

uvicorn api:app --reload

This command starts the server. With the usage of --reload flag, the server restarts when it observes any new changes in your file i.e api.py

Enter the URL in your browser:- http://127.0.0.1:8000

Final result in browser in JSON
The final result in JSON

Conclusion:-

In this article, we created a simple REST API in Python using the FAST API web framework. It has three routes and each route returns JSON data. Further, We can send the JSON data to a front-end as an HTTP response.

I hope this gives you a good understanding of what APIs are and how a REST API is created in Python.

Happy coding!😁

- csg33k