Introduction: Why Serve Individual Track Details?

Welcome back! In the previous lesson, you learned how to set up a backend for a music player app and serve all track data through a single API endpoint. Now, let’s take the next step: serving details for a single track.

Imagine you are building a music app where users can click on a song to see more information — such as the artist, album, or duration. To make this possible, your backend needs to provide a way to fetch details for just one track at a time. This is a common feature in many apps, and it helps keep things fast and efficient for users.

In this lesson, you will learn how to create an API endpoint that returns the details of a specific track when given its ID.

Quick Recap: App and Data Setup

Before we dive in, let’s quickly remind ourselves how the app and data are set up. You already have a Flask app that loads track data from a JSON file using Pandas. Here’s a short code block to refresh your memory:

This function loads all the track data into a Pandas DataFrame, which makes it easy to search and filter tracks later.

How the Single Track Endpoint Works

Now, let’s look at how to serve the details for a single track. In Flask, you can create a route with a variable part in the URL, like this:

Here’s what’s happening:

  • The route /api/tracks/<track_id> means that when someone visits a URL like /api/tracks/123, Flask will call this function and pass 123 as the track_id.
  • The function get_track_by_id(track_id) is used to find the track with the given ID.

Let’s see how get_track_by_id works:

Explanation:

  • get_all_tracks() loads all tracks into a DataFrame.
  • It checks if the DataFrame is empty. If so, it returns None.
  • It filters the DataFrame to find the row where the id matches the given .
Handling Missing Tracks and Errors

It’s important to handle cases where the requested track does not exist. In the endpoint, you see this code:

This does two things:

  • Returns a JSON object with an error message.
  • Sets the HTTP status code to 404, which means “Not Found.”

Example Output:
If you visit /api/tracks/999 and there is no track with ID 999, you will get:

And the HTTP status code will be 404.

Handling errors like this helps users and other developers know exactly what went wrong.

Filtering Tracks by Artist Name

So far, you’ve seen how to return a single track by its ID. But what if you want to get all tracks by a particular artist?

This is common in music apps — users often tap an artist’s name to explore more of their songs. To support this, let’s add a new function in database.py that filters the dataset based on the artist's name.

This function:

  • Loads all track data.
  • Filters rows where the artist column matches the name (ignoring case).
  • Returns a list of dictionary objects representing each matching track.

Now let’s create a route in app.py that uses this function:

Now when visiting /api/tracks/artist/Synth Weaver, we'll have something like:

Note: If you write curl -s http://localhost:5001/api/tracks/artist/Synth Weaver, The shell will interpet Synth and Weaver as two separate arguments, so curl will only receive http://localhost:5001/api/tracks/artist/Synth. You can solve this by quoting the URL and using %20 for the space:

Try Your Own Filters!

Now that you've seen how to serve tracks by ID and filter by artist or genre, you're welcome to explore further!

Feel free to add your own functions in database.py — for example, try filtering by mood, energy, or tempo. Just make sure to:

  • Implement the filtering logic in database.py
  • Create a matching route in app.py
  • Test it with a cURL request like:

This is a great way to get creative and practice building real-world APIs from your own ideas. Have fun experimenting!

Summary and What’s Next

In this lesson, you learned how to serve details for a single track using its ID — a crucial feature for any interactive music app.

Then you took it further by:

  • Creating a route to filter tracks by artist name, returning all matching songs.
  • Seeing how simple it is to build custom filtering logic, like searching by genre or mood.
  • Learning how to define new functions in database.py and expose them with new API routes in app.py.
  • Testing your endpoints using curl or your browser.

Finally, you were encouraged to try your own ideas: filter by energy, tempo, or anything else in the dataset. Next, you’ll get hands-on practice implementing these features yourself. You’re one step closer to building a fully functional audio experience — let’s keep going!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal