Mastering API Access with Python's Requests Library

Use Python's requests library to access APIs and handle HTTP status codes effectively.

Gain practical insights into accessing APIs using Python's requests library, along with understanding HTTP status codes effectively. Learn how to handle common API response scenarios to enhance your coding efficiency and error handling skills.

Key Insights

  • Utilize Python's widely-used requests library to effectively access APIs, demonstrated clearly through interactions with Alpha Vantage's API documentation.
  • Understand critical HTTP status codes like "200 OK," "201 Created," and "404 Not Found," and leverage resources such as HTTP Status Dogs and Wikipedia for quick reference.
  • Implement practical error-handling techniques in Python code by evaluating HTTP response status codes (e.g., printing "failed to retrieve data" when the status code differs from 200).

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Accessing the API. Once we've got our URL set up—and this is not a small amount of work—is that learning an API means learning its interface. What can I hit up to get the data? Once you have that, the Python code to actually access it is very simple. We're going to use the very common requests library.

In fact, this is the one that's so common that it's in the documentation at Alpha Vantage. We're going to say response equals requests.get and give it a URL. You don't have to save the URL into a variable like this, but it's a little more readable this way.

Now, that response from requests.get—this function will hit up this URL, get a response from the server, and put it into whatever variable we save it as. In this case, it's response. And that response will have some things on it.

For example, if we print it out, response has a property called status_code. Let's hit that one up and see what happens. Its code was 200.

All status codes are easily looked up, and it's pretty good to know your status codes. One of the best sites I like for learning status codes is HTTP Status Dogs. We got back 200 OK, which is this adorable dog.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

He looks OK. If we had instead created some data on the database by making this request, we would have gotten back a 201 Created code. And there are lots of them, and HTTP Status Codes does a great job showing you all of this information in a very cute, entertaining way.

One that people commonly know is 404 Not Found. You went somewhere on the internet that didn't exist, and the response from the server was “that's not a thing”—404 Not Found.

HTTP Status Codes is great. HTTP Status Dogs is great. It'll tell you, if you click on one of the pictures, exactly what it is.

And if you click here, you'll get the Wikipedia page about all the status codes. And you can read more, of course, in much more detail elsewhere. But for a quick check, HTTP Status Dogs is a great way to familiarize yourself quickly with the basic status codes.

It's also very cute. And you can always access HTTP Status Cats if that's your kind of thing. Now, this status code 200 means everything went well.

If, on the other hand, let's say I have a typo here and run this again, I get this big old error. If I hit up this URL, but let's say I misspell part of it, I now get back an error because I misspelled the domain name.

That's why I got that big error—there was simply no response from any server. This time I contacted the correct server, but I misspelled the interface. This is like clicking on the wrong thing. I misunderstood how to access the interface.

It was like “execute the code block, ” but instead, I clicked over here instead of here, right? I've misunderstood it and, instead of query, maybe I do search. And again, that's not a thing. So I get back a 404.

Again, “Not Found” is the status code. So a common thing to do here is to have some kind of indication from the code that there is some kind of error. We can say if response.status_code!= 200: print("Failed to retrieve data.")

That's fine—it's very general, but there's one too many equal signs. All right, so if I run this, I get “Failed to retrieve data.” A little better than just a number.

Okay, but let's fix that. Instead of search, let's make it query. And now we don't get any printing—no “Failed to retrieve data” message at all.

Next, we'll look at what happens when we do get a 200 OK, and what we can do to actually get our data.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram