Skip to content

(D). Your First REST API Endpoint

Zamathuli Luthuli edited this page Oct 30, 2023 · 3 revisions

Welcome back!

To create our first REST API endpoint, we will follow these steps:

Step 1: Define the Data Storage

  • We will use a Python list to store our data.
  • Each store will be represented as a dictionary with a name and an item list.
  • The item list will contain dictionaries for each item, including the item's name and price.

Here is a sample code snippet that we shall use as a starting point:

from flask import Flask

app = Flask(__name__)

stores = [
    {
        "name": "LaDivine Creations",
        "items": [
            {
                "name": "iMac",
                "price": 37.99
                }
            ]
        }
    ]

Note:

  • This code creates a list called 'stores' that contain one store for now.
  • This store has a name and an items list. Each item on the items list has a name and a price.
  • Later, we will make this code dynamic and let the user add data.
  • We will use this data structure to simulate our data storage until we introduce a database in later steps.

Step 2: Create Your First REST API Endpoint:

We can create our first endpoint now that we have the stored data:

  • To create our first Flask endpoint, we will define a function called 'get_stores' and associate it with the '/store' endpoint using the '@app.get' decorator.
  • This function will return the list of stores when the client requests it.

Here's an example code snippet that demonstrates the Flask endpoint definition:

from flask import Flask
app = Flask(__name__)

@app.get('/store')
def get_stores():
    return {'stores': stores}

Note:

  • In this code, we import the Flask module and create a Flask application.
  • We then use the '@app.get' decorator to associate the 'get_stores' function with the '/store' endpoint.
  • Inside the function, we return the list of stores as a dictionary with the key 'stores.'
  • Please note that the stores variable should be defined earlier, as shown in the previous step.
from flask import Flask

app = Flask(__name__)

stores = [
    {
        "name": "LaDivine Creations",
        "items": [
            {
                "name": "iMac",
                "price": 37.99
                }
            ]
        }
    ]


# Our first endpoint
@app.get("/store")   # http://127.0.0.1:5000/store
def get_stores():
    return {"stores": stores}

Step 3: Run the Flask app

To run the Flask app and access the '/store' endpoint, follow these steps:

  1. Run the Flask app
  • Make sure you have saved the code and restarted the app if you made any changes.
  • Execute the script containing the Flask app.
  • The app will run on 127.0.0.1:5000.
  1. Access the endpoint:
  • Open a web browser.
  • Enter 'http://127.0.0.1:5000/store' in the address bar.
  • Press Enter to access the specified endpoint.

Note:

  • At this point, Flask will execute the get_stores function associated with the /store endpoint, returning the data in the browser.
  • The appearance of the returned data may vary depending on the browser you are using. It will likely be in JSON format, which can be displayed differently depending on the browser's rendering.
  • That brings us to another subject of discussion, namely 'JSON.' Let us expand on this topic in the next section.

Here is a brief video showing the steps to make an endpoint.

Clone this wiki locally