Check out a free preview of the full Python Fundamentals course:
The "APIs Overview" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina explains what APIs are, responses, status codes, and authentication.

Get Unlimited Access Now

Transcript from the "APIs Overview" Lesson

[00:00:00]
>> Nina Zakharenko: Using the request library we can interact with APIs from Python, and per the dictionary, what is an API? It's a set of functions or procedures that allow accessing features or data of a system somewhere else. API sometimes has authentication, which I won't cover today. We're just gonna use unauthenticated libraries.

[00:00:24] It has rate limiting sometimes, which especially comes into play when you're not authenticating. And there are a lot of free APIs out there for you to use. A little bit about working with APIs. This image came from a really excellent, thorough intro to API guide by Brian Cooksey from Zapier.

[00:00:46] So if you're really interested in how this stuff works, I recommend reading it. But your client, your web browser, your Python program, it sends a request to a server and that server comes back and returns a response. To interact with APIs you use HTTP methods generally. So GET is asking the server for a resource.

[00:01:12] POST is asking the server to create a new resource. PUT updates, DELETE, deletes, kind of the standard ones. Generally, you also attach a header, a body, or parameters along with your request. This is kind of an example of what URL parameters look like. And then, these APIs have different response types, a variety of formats.

[00:01:38] XML was popular a few years ago. JSON is the popular library now. So JSON looks like this, and the request library turns this JSON into a Python dictionary seamlessly. A quick insight into HTTP status codes, they tend to fall into these categories. The ones in the 200 range generally mean success.

[00:02:06] Common ones here are 200 OK, that means everything went fine, kinda the standard response for successful HTTP requests. 201 CREATED means the resource was created successfully. In the 300s is a redirection. 400s is a client error, like a 404 means you gave me an ID but I couldn't find that ID in my system.

[00:02:32] And I couldn't find what you specifically asked for. Whereas the 500 range is an internal server error. That means I'm the server, something went wrong when I tried to generate a response to send back to you. So not anything having to do with the client. And because HTTP status codes are boring, there's a very helpful table here for HTTP status codes with cats.

[00:02:57] So 200 is OK, 301, for example, means moved permanently. 404, not found. And 500 is an internal server error. If you have a cat in your server, you might have a problem. One of my favorite all time Internet pieces of Internet trivia is that in 1998, as an April Fool's joke, someone made HTTP status code 418, I'm a teapot aka the hypertext coffeepot control protocol.

[00:03:34] And this is the implementation of it. So if you have an hour to kill, look up the Wikipedia entry and look at the code for this. It's hysterical. There are several ways of authenticating to APIs, but I won't be covering them.