Overview of the Requests Package in Python to Communicate with APIs
This is going to be a really fun guide. In this lesson, we're going to walk through how to implement the requests library in python and what this is going to allow us to do is to reach out and communicate with outside API's.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So if you've never worked with an API before it stands for application programming interface. Essentially it is a language for our application to use and to communicate with another third-party application and the application we're going to be communicating with is the daily smarty app and so this is a spot where you can go and add posts you can add links content tutorials anything like that.

It makes an API available so that we can communicate with it and the API looks much different than the web application. So if you go to the API which is an api.dailysmarty.com/posts this is what it's going to look like.

Note

DailySmarty is not currently functioning. You will need to use https://jsonplaceholder.typicode.com/ for API requests.

Now, this is called JSON which stands for a javascript object notation. Now if you are familiar with working with Python dictionaries then this is going to feel rather intuitive to you because what JSON is, is a set of key-value pairs. So right here we have a key of posts and then we have all of these posts inside of it and then nested inside of those posts. Is this list that contains a key of an id value of 434 a key of a title of value of speed reading application.

large

When you're going through this you're going to have a fully different set of values here because these change as new posts get added and as you can see what this is is just a structured way of organizing the data that we have right here.

large

So what the request package is going to allow us to do is to reach out and communicate with that application. So if you want to install this on your own system you can switch over to the terminal or the command prompt if you're on Windows and then simply type in pip install requests and if you do not have it on your system it'll run it and It'll install it. As you can see I already have it installed and it says requirement already satisfied.

large

Now that it's installed on your system, let's open up a python repl session and import it. So I'm gonna say import requests just like this:

import requests

and hit return and as long as you don't get an error right there that means that that worked for you and I'm also going to import another library it's called pprint which stands for pretty print and so I can say import pprint and then run that. If you do not have that on your system and it didn't come with your version of Python then you can install manually with pip but it should already be on your system.

So now that we have that now we can actually make a request so I can say r equals so I'm gonna store our request in this variable and I'll say requests and make sure you spell it with the plural because that is the name of the package .get and then we're going to pass in the API endpoint. And so this is going to be https://api.dailysmarty.com/posts

r = requests.get('https://api.dailysmarty.com/posts')

and then after you close the parens off hit return and then it is going to go out and it's going to communicate with that API. If you don't get an error right there, that means that this part of it worked.

Now if you want to see what the JSON looks like I can use the variable and then pass a method that is provided inside of the request library called JSON because I'm working with JSON data here and I can call this and this is all of the code that we saw in the browser.

large

So if you switch back to the browser and you go to that same endpoint you can see this is our full set of key-value pairs.

large

Now by default, it's not formatted when the requests library brings it back to us so this is very hard to read which is why I installed the pretty print library and why I imported it so we can make this a little bit easier to read by calling pprint.pprint and remember the reason for that is because this is the package name and then this is the function inside of it. And now I can call r.json and call this function. And now if I run this you can see we get a fully formatted response and this looks a lot closer to what we had in the browser.

large

This is the full list of live posts and this is incredibly cool. This is working on your system. This means that you have successfully built a program that goes out on the Internet and communicates with another program and pulls this in.

This is going to be something that you're going to be using constantly regardless of the type of field that you're in. Imagine that you're a machine learning data scientist. You're going to have to go out and grab data from outside sources and this is a way you can do it or if your a back-end developer building out APIs this is going to be what you're doing all day long. This is going to be the way that you can communicate with outside services.

So that is helpful, now let's see what we can do with the data. So if I want to grab the very first post then I can use the same code here but I can treat it like a dictionary. So I'm going to use bracket syntax. Now the way that you can do it is I'm going to use the last command and then use our dictionary querying syntax. I'm gonna start with brackets and then from there passing a string of posts and let's see if that works for us. So I run this you can see that that now has brought back the posts

large

That may not seem like a big deal because that's what we had before but I wanted to do that to make sure that we didn't run into any errors. Remember making small steps as we build out our programs is critical.

So now that I have our list of posts I can actually treat these like a traditional list so I can pass in the bracket syntax and grab the zero-width index and you can see that I have access to the very first post.

large

I can have access to its content, the date that it was created, it's id, any links inside of it, which you can see is another nested list, its title, or even the URL. So say that I want to grab just the URL. Once again I can treat this like a dictionary and I'll chain on one additional bracket and say url_for_post run this and there you go. We have a string with the URL.

large

Just to make sure that that is working the way that we'd expect. I'm just going to copy this come into the browser paste it in, make sure you remove the strings there and as you can see that worked perfectly.

large

This was a real live URL passed through the daily smarty API that we accessed on our local machine and we were able to work with that data.

Code

import requests
import pprint

r = requests.get('https://api.dailysmarty.com/posts')
r.json()
pprint.pprint(r.json()['posts'][0])
pprint.pprint(r.json()['posts'][0]['url_for_post'])