Flask | Create Your First Flask API

felixd38v_

felixd38v_

Posted on February 19, 2024

Flask | Create Your First Flask API

A Restaurant API

Let's set up a simple Flask API that returns a list of restaurants and their menu items in JSON format.

What is an API?

In case you would like to learn what APIs are, feel free to visit the following excellent resources:

Now, let's begin.

Steps:

Before starting,make sure you have Flask installed on your machine

For installation steps go to Flask's documentation

Also, make sure you create and activate a new virtual environment

For steps go to any of the following great resources:

1. Open your terminal and create a directory for storing your code,

mkdir restaurant-api
Enter fullscreen mode Exit fullscreen mode

2. Now, cd into the directory you just created

cd restaurant-api
Enter fullscreen mode Exit fullscreen mode

3. Within that directory, create a file named app.py

touch app.py
Enter fullscreen mode Exit fullscreen mode

4. Open your code editor (e.g. Visual Studio Code) and add the following code to the app.py file:


from flask import Flask # Here we are importing the Flask class. This class is used to create the app.

app = Flask(__name__) # Here we are creating an instance of the Flask app.


"""
Below, you will see the `restaurants` list, which contains a few dictionaries.
Each dictionary within the `restaurants` list has a name and a list of items,
where each item is a dictionary with a name and a price.
"""

restaurants = [ 
    {
        "name": "Tasty Burgers",
        "items": [
            {
                "name": "Classic Burger",
                "price": 8.99
            },
            {
                "name": "Cheeseburger",
                "price": 9.99
            }
        ]
    },
    {
        "name": "Pizza Palace",
        "items": [
            {
                "name": "Pepperoni Pizza",
                "price": 11.99
            },
            {
                "name": "Margherita Pizza",
                "price": 10.99
            }
        ]
    }
]


@app.get("/restaurant")
# The above is a decorator that defines a route for handling GET requests
# to the `/restaurant` endpoint.

def get_restaurants():
    return {"restaurants": restaurants}
# The above is a function that returns a dictionary that contains the `restaurants` list
Enter fullscreen mode Exit fullscreen mode

5. Now, let's start the app

flask run
Enter fullscreen mode Exit fullscreen mode

Flask will start a development server and host our application application, making it accessible at the following port http://127.0.0.1:5000/ by default.

6. Open the browser of your preference and type in the following:

http://127.0.0.1:5000/restaurant

This is what you should see in your browser:

{
  "restaurants": [
    {
      "items": [
        {
          "name": "Classic Burger",
          "price": 8.99
        },
        {
          "name": "Cheeseburger",
          "price": 9.99
        }
      ],
      "name": "Tasty Burgers"
    },
    {
      "items": [
        {
          "name": "Pepperoni Pizza",
          "price": 11.99
        },
        {
          "name": "Margherita Pizza",
          "price": 10.99
        }
      ],
      "name": "Pizza Palace"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

This JSON represents a data structure containing a list of restaurants, where each restaurant has a name and a list of items on its menu. Here's a breakdown of the structure:

  • The outermost curly braces {} indicate an object.
  • The key "restaurants" maps to a value that is an array [] containing two objects, each representing a restaurant.
  • Each restaurant object has two key-value pairs:
  • The key "name" maps to the name of the restaurant (e.g., "Tasty Burgers" or "Pizza Palace").
  • The key "items" maps to a value that is an array [] containing objects, each representing an item on the restaurant's menu.
  • Each item object has two key-value pairs:
  • The key "name" maps to the name of the item (e.g., "Classic Burger" or "Pepperoni Pizza").
  • The key "price" maps to the price of the item (e.g., 8.99 or 11.99).

Further Reading

References

πŸ’– πŸ’ͺ πŸ™… 🚩
felixd38v_
felixd38v_

Posted on February 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related