Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Requesting Specific Data With Query Parameters

Resources mentioned in this lesson:

00:00 To make it easier to find the data you want, APIs let you send extra information to narrow down your search. This bundle of extra information is called query parameters, and you’ll learn how to use them in this lesson.

00:14 Query parameters are filters you can send with your request to narrow down the information you will receive. In this example, you’re going to look at a list of Microsoft’s repositories on GitHub.

00:26 On this slide, you can see the URL you will be using for your request, https://api.github.com/orgs/microsoft/repos, as well as the documentation for this API, https://docs.github.com/en/rest/repos/repos.

00:44 If you visit that documentation link, you’ll see this page. To see the available parameters for this API, you should scroll down to parameters for list organization repositories, and in particular, query parameters.

00:59 Here you can see the different types of parameters that are available to you. For example, type, where you can choose public repositories, private repositories, and so on.

01:09 sort, which lets you sort the repositories by different properties, for example, the date they were created, most recently updated, or pushed, or alphabetically by their full name.

01:20 You can also change the direction of the sorting, either ascending or descending. You can also paginate your results using the page and per_page parameters, which you will look at in another lesson.

01:32 While you’re here, you can also take a look at an example response to see how it’s structured. Here you can see you’re expecting JSON, and you can see from the square brackets that you will receive a list of objects.

01:46 Moving to the REPL, you can try sending these query parameters by yourself. You should start similarly to your previous emojis project by importing requests and then saving the URL, https://api.github.com/orgs/microsoft/repos, as a variable.

02:03 Now, before you make your request, you will create a dictionary called repo_params, which will contain all the parameters you want to include in your request.

02:12 Here you’re using the type parameter and the sort parameter, with the values "public" and "pushed". Feel free to change these values to anything you find in the documentation.

02:23 With your repo_params dictionary ready, you can make a request using the same function you’ve used before, but this time, after the URL, you’re going to include another parameter, params, and you’re going to pass it the repo_params dictionary you made earlier.

02:38 requests can convert JSON responses for you using the .json() method. In this case, the response was a list of repositories, so you can loop over them in a for loop.

02:50 The individual repositories are dictionaries, so you can access the "name" key to print their names. And there you can see a list of Microsoft’s most recent repositories.

03:00 It’s worth playing around with this code and the different query parameters to see what else you can do. You’re now able to interact with APIs and use query parameters to filter and sort the data you ask for.

03:12 If you look at other APIs you want to experiment with, you may notice they are private and require authentication. But don’t worry, in the next lesson you will learn how to authenticate yourself and unlock all these exciting APIs.

Become a Member to join the conversation.