Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Data from APIs!
      
    
You have completed Data from APIs!
Preview
    
      
  Learn how to interact with an API using Python.
Terms
- API - Application Programming Interface
 - JSON - JavaScript Object Notation
 
Links
Response Codes
- 200 - OK
 - 202 - Accepted
 - 400 - Bad request
 - 403 - Forbidden
 - 404 - Not found
 - 500 - Internal server error
 
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      Hi there, my name is Megan.
                      0:09
                    
                    
                      And we're going to be looking at how
we can gather some data from an API, or
                      0:10
                    
                    
                      Application Programming Interface
using Python.
                      0:15
                    
                    
                      If you haven't heard of an API before,
I suggest taking a look at the teacher's
                      0:19
                    
                    
                      notes below to get familiar with
the topic before continuing on.
                      0:24
                    
                    
                      APIs allow us to interact
with different systems.
                      0:28
                    
                    
                      For example, the Twitter API
allows us to gather tweets.
                      0:31
                    
                    
                      The Google Calendar API
allows us to create events.
                      0:36
                    
                    
                      And for our purposes, there are many
APIs that return valuable data.
                      0:40
                    
                    
                      To practice, we're going to use
this random user generator API.
                      0:46
                    
                    
                      You can see here that I have a project
folder open in Visual Studio code.
                      0:52
                    
                    
                      I'm going to create a virtual environment.
                      0:57
                    
                    
                      We're going to activate it and
I'm going to install requests.
                      1:02
                    
                    
                      Perfect, and
then I'm just gonna clear our console out.
                      1:10
                    
                    
                      When working with an API, you typically
make a request for information or data.
                      1:14
                    
                    
                      And the API receives your request, and
                      1:20
                    
                    
                      then sends back a response
with the data or information.
                      1:23
                    
                    
                      With the API we have here,
we send a request to it, and
                      1:29
                    
                    
                      it sends back a random user that
they have generated for you.
                      1:34
                    
                    
                      Let's pop into the Python
shell to play around.
                      1:41
                    
                    
                      We're going to import requests, perfect.
                      1:47
                    
                    
                      And we're going to try calling the API,
                      1:53
                    
                    
                      requests.get https://randomuser.me/api/,
                      1:59
                    
                    
                      close it all out.
                      2:07
                    
                    
                      Perfect.
                      2:09
                    
                    
                      That sent a response to the API and
you can see it send us back a response.
                      2:12
                    
                    
                      And it sent it back
pretty much right away.
                      2:17
                    
                    
                      But not every API requests will come
back with a response instantly.
                      2:20
                    
                    
                      Sometimes it takes a little bit.
                      2:25
                    
                    
                      This is a response object and
it's showing us our status code which
                      2:27
                    
                    
                      is 200 which means okay or
your request was successful.
                      2:32
                    
                    
                      Check the teacher's notes for
a list of some common codes.
                      2:37
                    
                    
                      So look at the data from the response,
let's take a quick peek at the docs.
                      2:40
                    
                    
                      This is also linked below
in the teacher's notes.
                      2:45
                    
                    
                      It looks like we can view
the response content by saving
                      2:50
                    
                    
                      the response to a variable and
then calling our attribute .text.
                      2:55
                    
                    
                      So let's give that a try.
                      3:02
                    
                    
                      I'm going to do, instead of,
I know in the example they just did r,
                      3:05
                    
                    
                      I'm gonna do res for response,
cuz that's what we get back.
                      3:10
                    
                    
                      requests.get https,
make sure this is a string,
                      3:15
                    
                    
                      randomuser.me/api/.
                      3:20
                    
                    
                      Perfect.
                      3:25
                    
                    
                      Now let's do our variable,
response, res.txt.
                      3:26
                    
                    
                      And there's the results.
                      3:32
                    
                    
                      This is the fake user that they've
created for us and sent back to us.
                      3:34
                    
                    
                      And now remember, these are random users.
                      3:38
                    
                    
                      So I have someone called Savannah Thomas,
                      3:40
                    
                    
                      you are probably gonna have
a different random user.
                      3:43
                    
                    
                      There's one small issue though.
                      3:47
                    
                    
                      Can you see it?
                      3:49
                    
                    
                      Response.txt is a string.
                      3:51
                    
                    
                      You can see there's a string at
the top and down here at the bottom.
                      3:54
                    
                    
                      So it's returned you a string with
all of this information in it.
                      3:59
                    
                    
                      And we can see there are things like
dictionaries and this is a list.
                      4:02
                    
                    
                      So having it as a string is going to make
it hard to parse or work with the data.
                      4:07
                    
                    
                      Instead, let's make our job a little bit
easier by viewing the data in JSON format.
                      4:13
                    
                    
                      Let's jump back into the docs.
                      4:19
                    
                    
                      And if we scroll down a bit, you can
see we get this JSON response content.
                      4:22
                    
                    
                      And essentially,
                      4:29
                    
                    
                      you're doing the exact same thing except
you're calling .json instead of .text.
                      4:31
                    
                    
                      And don't forget your parentheses here.
                      4:36
                    
                    
                      This is a method call this time.
                      4:38
                    
                    
                      Let's give it a try.
                      4:42
                    
                    
                      So I just did up arrow.
                      4:45
                    
                    
                      So respose.txt, let's do response.json.
                      4:46
                    
                    
                      And you can see it looks
almost exactly the same, but
                      4:51
                    
                    
                      our little symbols that would
make this a string are gone.
                      4:54
                    
                    
                      Now it's just the data as it is,
which is perfect.
                      4:59
                    
                    
                      This will make it a lot
easier to work with.
                      5:03
                    
                    
                      Take a look closely at the data.
                      5:07
                    
                    
                      It's a mixture of dictionaries and
lists, and more dictionaries.
                      5:09
                    
                    
                      So it's going to be a little
tricky to access specific items.
                      5:15
                    
                    
                      But luckily, most APIs have an example
response that is formatted a lot
                      5:20
                    
                    
                      nicer than what you're probably
gonna see in the console.
                      5:24
                    
                    
                      So, let's look at what we get.
                      5:29
                    
                    
                      We get back an initial dictionary,
has a key item of results.
                      5:31
                    
                    
                      And it looks like it has
a second key item of info and
                      5:40
                    
                    
                      then it has all this
information inside of it.
                      5:44
                    
                    
                      So inside of results we have this,
                      5:49
                    
                    
                      if I go from the opening of our list,
to the closing of our list,
                      5:51
                    
                    
                      has all of this information inside of it,
which is a dictionary.
                      5:57
                    
                    
                      If I go from the start of this dictionary,
straight down to where it ends,
                      6:02
                    
                    
                      it includes one single dictionary which
includes all of this information.
                      6:08
                    
                    
                      And it just keeps getting further and
further down into the data.
                      6:13
                    
                    
                      So you can see it's a lot
of mixed information.
                      6:17
                    
                    
                      So what I want you to do right now is I
want you to practice accessing specific
                      6:20
                    
                    
                      information in the result.
                      6:25
                    
                    
                      I want you to try and
gather just the first name, so just for
                      6:26
                    
                    
                      the example here in the docs would
be Brad, so just the first name.
                      6:31
                    
                    
                      Then just the state which would be
Waterford here in this example.
                      6:36
                    
                    
                      And then just the email which
the example would be this as the answer.
                      6:40
                    
                    
                      Okay, so just the first name,
the state and the email.
                      6:45
                    
                    
                      Go ahead and try accessing those
specific values on your own.
                      6:51
                    
                    
                      Pause me.
                      6:55
                    
                    
                      Solution time.
                      6:57
                    
                    
                      Let's do this.
                      6:59
                    
                    
                      I like to incrementally build
on one thing at a time so
                      7:01
                    
                    
                      I can kind of see what I've got going on.
                      7:05
                    
                    
                      So the first thing is everything
is inside of results.
                      7:09
                    
                    
                      Perfect, and
then you can see it's a huge long list.
                      7:17
                    
                    
                      And all of this is a single
dictionary item inside of one list.
                      7:22
                    
                    
                      So it's not gonna look very different but
it is a list, and so
                      7:26
                    
                    
                      I need to access this first
item which is the only item.
                      7:30
                    
                    
                      So it doesn't look much different, but
                      7:35
                    
                    
                      you can see that list symbol is gone
cuz now we're inside of the list.
                      7:37
                    
                    
                      Now we have a dictionary.
                      7:41
                    
                    
                      So you can see we have a key,
and then we have a value.
                      7:43
                    
                    
                      And then we have name, and the value for
                      7:46
                    
                    
                      name is this dictionary,
which then includes our first name.
                      7:50
                    
                    
                      So I need to do our key of name.
                      7:57
                    
                    
                      If I hit Enter,
                      8:00
                    
                    
                      now I have just that dictionary
which is the value for the key name.
                      8:02
                    
                    
                      So now in order to get our first name,
which for
                      8:08
                    
                    
                      me is Savannah,
I need to access the key first.
                      8:12
                    
                    
                      I'm gonna do up arrow first.
                      8:16
                    
                    
                      And I get Savannah.
                      8:21
                    
                    
                      So this is the entire solution
to access just the first name.
                      8:22
                    
                    
                      That's why it's really good to
take some time to practice.
                      8:27
                    
                    
                      Now let's try and do the state.
                      8:30
                    
                    
                      So I'm gonna do up arrow, and
I'm gonna go back through all of this.
                      8:31
                    
                    
                      So we're back here.
                      8:37
                    
                    
                      And let's see, we have key value,
key value, key location.
                      8:38
                    
                    
                      Looks like there's a street and
there's a city, and there's a state.
                      8:44
                    
                    
                      So, because our location contains
a dictionary, let's do location next.
                      8:52
                    
                    
                      Okay.
                      9:04
                    
                    
                      Did that go?
                      9:13
                    
                    
                      There it is, sorry.
                      9:15
                    
                    
                      I was seeing all the information above and
I thought it didn't run but it did.
                      9:17
                    
                    
                      Okay, so this is the response we got.
                      9:23
                    
                    
                      I'll highlight it here so it's a little
bit easier for all of us to see.
                      9:24
                    
                    
                      So you can see we have a street
which has a dictionary.
                      9:27
                    
                    
                      We have a city which has
a string as the value.
                      9:31
                    
                    
                      And then our state which is a key
with a string as its values.
                      9:37
                    
                    
                      So up arrow, state.
                      9:44
                    
                    
                      And you can see that it gives
us this as our state value.
                      9:45
                    
                    
                      So our entire solution to access
just the state is this right here.
                      9:49
                    
                    
                      Now the last one was the email.
                      9:56
                    
                    
                      So same thing, I'm gonna put up arrow and
                      9:58
                    
                    
                      I'm gonna take us all
the way back to here.
                      10:01
                    
                    
                      And we get through our location
which was a dictionary,
                      10:04
                    
                    
                      which included a street and
the city, and the state, country.
                      10:08
                    
                    
                      Postcode, coordinates,
which was another dictionary.
                      10:13
                    
                    
                      Timezone, which was another dictionary,
and you can see this is where that ended.
                      10:16
                    
                    
                      And then we have a key of email and
a value.
                      10:22
                    
                    
                      So this one is a little bit easier cuz
we don't have to dive down so deep.
                      10:26
                    
                    
                      This one is just email,
savanna.thomas@example.com.
                      10:32
                    
                    
                      So this is the entire solution for
the email.
                      10:39
                    
                    
                      How did you do?
                      10:44
                    
                    
                      If you struggled with this,
that's absolutely okay.
                      10:46
                    
                    
                      The first time working with data
from an API is always funky and
                      10:49
                    
                    
                      it's a lot of nested information.
                      10:53
                    
                    
                      I would take some time right now to
practice accessing different pieces
                      10:56
                    
                    
                      of information from this response
until you feel more comfortable.
                      11:00
                    
                    
                      Take it one step at a time.
                      11:04
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up