Create a RESTfull API using Python and Flask (Part 2)

Summary From Part 1

In part 1, we created our working Flask Application and we got some knowledge about Flask.

In this part, we’re finally ready to implement a small API with data that we’ll define right in our application.

Let’s Go

Here we’ll add our data as a list of Python dictionaries.

Dictionaries in Python are group pairs of keys and values, like this:

You can check more about this here.

Let’s add some data (entries on ten music tracks) as a list of dictionaries to our code. Each dictionary will contain AlbumId, Bytes, Composer, GenreId, MediaTypeId, Milliseconds, Name, TrackId and UnitPrice for each track. Finally, we’ll add a new function: a route that will allow a visitor to access our data.

Replace our previous code in api.py with the code below:

Run the code (navigate to your api folder in the command line and enter python api.py). Once the server is running, visit our route URL to view the data in the catalog:

http://127.0.0.1:5000/api/v1/resources/tracks/all

You should see JSON output for the three entries in our test catalog like this.

In Flask, jsonify function that allows us to convert lists and dictionaries to JSON format. Here our Music Track entries converted from a list of Python dictionaries to JSON before being returned to a user.

Now, we have created our simple API. But it is limited to get all tracks only. How can we get specific Resources from this API such as “AlbumID”? The next section, we’ll allow users to find track via more specific data, such as an entry’s ID.

Filter Resources

Here users can only view our entire database without filtering any specific data field. This will become a useless thing when we add more because it is not easy to filter thousands of records manually. So, we’ll add a function that allows users to filter their returned results using a more specific request.

Replace our previous code in api.py with the code below:

Run the code (navigate to your api folder in the command line and enter python api.py). Once the server is running, visit the below URLs to test the new filtering capability:

http://127.0.0.1:5000/api/v1/resources/tracks?id=0

This will return you an Empty List, since there is no track for which the “TrackId” value is 0

http://127.0.0.1:5000/api/v1/resources/tracks?id=1

This will return you a JSON data entry like this:

http://127.0.0.1:5000/api/v1/resources/tracks?id=2

This will return you a JSON data entry like this:

http://127.0.0.1:5000/api/v1/resources/tracks?id=3

This will return you a JSON data entry like this:

Each of these should return a different entry. But ,

this should return an empty list: [], since there is no “TrackId” for which the id value is 11.

What is this?

In this code, we create a new function, called api_root, with the @app.route syntax that map the function to the path /api/v1/resources/tracks.

When we access http://127.0.0.1:5000/api/v1/resources/tracks api_root function will run. (If we access this link without providing an ID, it will give an error message like Error: No id field provided. Please specify an id.).

Query parameters are a defined set of parameters attached to the end of a URL. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query parameters to the end of a URL, a ‘?’ Is added followed immediately by a query parameter.

So, the id must be provided like this: ?id=1.

This part of the code determines if there is a query parameter, like ?id=1, and then assigns the provided ID to a variable. If there is not query parameter, it will return an Error Message.

This section checks those tracks that have the provided Track ID, and appends them to the list that will be returned to the user:

Finally, the return jsonify(results) line takes the list of results and renders them in the browser as JSON.

Congratulations!!

You have created an actual API. In this tutorial, you implemented a small API with data using Python.

In Part 3 of this series, you’ll learn somewhat more complex API that uses a database.

Click here if you haven’t read PART 1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kasun Dissanayake

Kasun Dissanayake

Software Engineer at IFS R & D International (Pvt) Ltd || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile