Master Python Requests In 15 Minutes. Call Any API
Skills:
Tool Use & Function Calling80%
Key Takeaways
Mastering Python Requests to call any API, covering setup, basic requests, query parameters, body, error handling, and authorization
Full Transcript
In the next 15 minutes, you'll learn everything you need to know about requests in Python. I'll talk to you about how to call APIs, all of the relevant code, and cover all of the core theory and terminology that you need to understand. So, let's get started. Okay, so in front of me I have a quick Miro board here. What I'm going to do is just break down some key terminology, and then once we get through this, we'll get into the code editor and we'll start writing some code. So, a lot of times you'll see URLs or endpoints, so you'll hear that term. Now, a URL or endpoint is made up of various different components, and these are important to understand because if you want to send a request, you need to send that to some type of URL. Okay, so we have a URL, right? So, like this whole thing is the URL, and you'll see that we have some domain. Now, the domain is the first part of the URL. It's typically the thing that ends in like .us or .com or .ca, and then after that you typically have some slash or multiple slashes, and those slash refer to the path or endpoint on that URL. So, in this case we have /tim, but we might have /privacy-policy or /refund-policy or /whatever coming after this, which denotes kind of like the page or the path that you're going to on this URL. Okay? Now, after that you may see a question mark. This is optional, you don't have to see this. If you do see a question mark, all of the things that come after that question mark are referred to as the query parameters. So, in this case it's something like, "Okay, we're going to /tim, and maybe we came from this particular video ID." So, when we talk about sending requests, we can add different variables to our requests in the form of query parameters to get different data in return. Okay, now let's keep going here. Here's another example of a URL. You can see this is the domain, so techwithtim.net. We have a longer path this time, so /courses/python. That's kind of the resource or the endpoint that we're trying to access from this domain. And then we have a few other query parameters where we're saying, "Okay, we want to go to page two, for example, and we want the UTM source to be YouTube," which means maybe we came to this page from YouTube. Okay? So, these are query parameters, this is our path or endpoint, and this is our domain. Just keep that in mind when we are kind of sending requests. Now, let's move over here and talk about the request and the response structure. So, anytime you send a request, you're typically doing that from what we refer to as the client or some kind of front end or website. So, you as the user here, you go to some kind of client. Now, the client could be a website, so something like Facebook, for example, and maybe you want to load all of your posts or you want to sign into your account, whatever. What's going to happen is behind the scenes a request is going to be sent from this, so whatever you're accessing, to some kind of back end server or API. Now, I'm not going to dive into APIs too much in this video, but the APIs are going to store all of the different paths and routes and handle all of the secure logic, so like creating a post, signing into account, deleting something, you know, all of that data that should be protected and maybe is only viewable to a certain person. So, you're going to send some request, and then this server is going to give a response back. Okay? That's literally all that you need to know. Send a request, you get a response back. So, this is kind of the flow of information, and that's why when I'm talking about requests in this video, we need to understand what a request is, what it's made up of, and what a response is made up of because that's the information we're requesting. Now, in a request, we have a few different components. We have the type or the method. We have the path, which you just saw before, so like /courses or /home or something like that. We potentially have a body. This is some information we send alongside with the request. And then we have headers, and headers will contain things like the content type, maybe an authorization token if we need a certain kind of identifier to access some resource. Now, once we send this request, we get the response back, and the response is made up of a status code. So, maybe you've seen something like, you know, 200 before, which means success, or 404, right? You know, not found. These are status codes, some body, which is typically the information that we requested, and then some headers. And same thing, the headers can have, you know, all kinds of other kind of details and metadata inside of them. And just to give you a quick example here, here's a look at kind of a sample request and response. So, let's say a user wants to update the post that they made. The request would be of type patch, we'll talk about that in a second. The path would be /api/post, and then maybe the ID of the post. The body we would send would be the new title and the description that we want to send, and then we would have headers where we have some kind of token who identifies this user, and then the content type application/json. And then the response would come back 204, which means updated, that's the status code, and then we would have this kind of body that says, "Okay, this is the new post information." And then same thing for the headers, we have this content type, which is application/json. I know there's a lot of content here. Let's quickly go through the last bit of theory and then we'll jump onto the computer. Okay, so let's have a look at HTTP status codes. Anytime you send a request, like I said, the response will contain a status code. Here is a table of very common status codes, you do not need to memorize them. 200, okay. 201, created, right? Common other ones that you get is like 404, you know, not found. 403, forbidden. You've probably seen some of these status codes before, but it's just useful to kind of know what they are and the fact that you get a status code, you can kind of understand what's happening with that resource that you're trying to access based on what's returned to you. And then lastly, we're just going to quickly talk about HTTP methods. Anytime you send a request, you can send a type of request. Now, the types that you see right here are the common HTTP types. So, you have get, delete, post, and put. A get request means you're accessing or asking for some type of information. A delete request means you want to delete something. A post request means you want to create something. A put request means you usually want to update something, and there's a few other types as well, but these are the most common. Now, I know that was a lot of information, but it's important just to have a quick general grasp of some of these concepts. So, when we talk about sending a request, what we're trying to do is trigger some kind of action or grab some kind of resource or information from some other location, right? So, in the case of maybe like a Twitter API, we're trying to grab a post or delete a post or update a post. Maybe a weather API, we want to get the current weather. So, by understanding these different methods and understanding these different components, you can more accurately use the request module and understand what's actually happening when you send this request. So, with that said, let's hop over to the computer and let me show you how to send requests. So, I'm on the computer right now, and I've just opened up PyCharm. I've created a new folder, and I've just opened a file called main.py. Now, in order to use the request module in Python to send a request and access a resource on an API, you need to install it. So, in order to install it, you can use the command pip install and then requests, or you can create a virtual environment like I'm about to and then install it inside of the virtual environment. Now, I typically prefer to use uv, so I'm going to use the command uv init and then dot. This is going to create a new uv project inside of my current directory. I'm then going to say uv add requests. When I do that, it's going to add the request module to my virtual environment. And now if I want to run my Python file, I can type uv run and then main.py. If you don't know anything about uv and you want to learn, I'll put a video on screen which will explain how to use it and how to set it up. All right, so let's close the terminal and go in here in PyCharm and start writing the code. Now, you may be wondering why I'm using PyCharm. Well, PyCharm is just the best editor when it comes to writing Python code, and I actually have a long-term partnership with them where you can access and use it completely for free if you click the link in the description. If you do any heavy work in Python, especially with network requests, web applications, AI, etc., then PyCharm is definitely a good editor to check out. And again, you can use it for free and see if you like it or not. Okay. So, in order to get started here and to send a request, we need to import the request module. So, we can import requests. We then need to have some kind of URL that we want to send the request to. So, I'm actually just going to copy in one here, which is kind of like a fake URL that you can use when you're testing things. So, it's called jsonplaceholder.typicode.com /posts/1. So, this is the domain, and then this is the endpoint or the resource that we're accessing and that we want to send the request to. We're then going to say our response is equal to request.get. Now, notice that I used request, and then I put the type of the method that I'm trying to use. So, in this case I want to send a get request to get some information, and then I'm going to put the URL. Now, once I get the response, which is going to be stored in this variable right here, I can print out some information related to it. So, I'm just going to paste in some code, it's faster than me typing it, and I'm able to get the status code, so what the status code was from this response, and then I'm able to get the header, and I can get, for example, something like the content type, so I can access all of the headers, which will be a Python dictionary, and then get all of the keys outside of that. And then what I'm also able to do is get the data that was returned to me, which I can access using this .json property. This .json is going to get all of the JSON data that came from this request, and then I'm able to access various fields in the JSON by printing out something like the title, for example. So, what I'm able to do to test this is simply to go here and type uv run main.py, and notice I get status code 200, which means okay. I have a header of this, and then it says the post title is sunt odd, whatever, some language that I don't know how to read, and that's the title of that post. There you go. We just sent a request, and we got a response back. And we also can just print out all of the data here. So, rather than just looking at the title, we can do something like this, and you'll see that we get kind of this Python dictionary-like object, which has a user ID, ID, title, and then body. So, that's the information associated with this post. Okay, so here's just another quick example of sending a get request. This is another kind of fake API that you can hit that will just return some fake demo data to you. What I'm doing is I'm setting now that I actually want to send a query parameter. So, excuse me, we'll look at the error handling later, but for now, if we want to send query parameters, what we can do is we can put them inside of something like a dictionary. So, in this case, maybe I want to specify, okay, you know, page is equal to two or three or whatever here. Rather than me manually having to put that inside of the URL, I can actually specify that in this parameters dictionary. I then can send a request and I can pass those parameters. This means query parameters. And what I'm able to do is send the get request, it will automatically add the parameters to the URL for me, which I can print out like this. I then I'm going to run this line, which says, okay, if we get a 400 or 500 status code, that means there was an error, so we will simply raise the error rather than trying to do this portion. If we don't get an error, then we will go into this code, it will print out some of the information like the users' emails that we got from this response. So, what I'm going to do is just run the code, and you're going to see that the final URL now has the added query parameter to it because the params. And then we get a few different emails that came from page two of this API. Let's have a look at a few other examples. I'm just going to copy one in here, so let's remove this and paste this. Now, this is an example of sending a post request. So, remember that there's different types of requests. We can send a get request, which means we're getting information, or we can send a post request, which means we're creating information. Now, you usually would do this on a API that you actually control or that you've written yourself. However, this is just a demo for the purpose of this video. So, if you want to send a post request, you have the ability to include body data, or kind of like a payload of information that is not going to be inside of the query parameter. So, what we've done is we've specified the URL, we specify a payload, which is in a Python dictionary. Whenever you're working with requests in Python, you're always going to be using Python dictionaries to represent something called JSON, which means JavaScript Object Notation, which is the type of data that is exchanged by APIs or by these types of requests. And what we do now is rather than using dot get, we use dot post. We send a post request to this URL, and then we include this as the data that we want to create. Similar to before, I'm able to print out the status code and get the response. Let's quickly have a look at this, and you'll see it says 201, meaning this was created successfully, and then we get the information here saying, you know, hello from Python, this is the body, this is the user ID, which is the information we submitted. Now, let's have a look at another example where we can handle errors that will occur in our requests. So, you can see here that we have some kind of response. We say response is equal to request dot get, and we send it to this URL right here, and we specify a timeout of 1 second. This is something you can do when you send a request. Now, we also have response dot raise for status. That means if we get a 400 or 500 status code, we will raise an error. And then if nothing goes wrong here, we will simply print out the response. Now, what we can do is we can accept specific exceptions, so we can see if the request timed out, for example, or if the request failed. So, I recommend that you accept specific exceptions when you're trying to handle errors that can occur in your request by using this except structure. So, again, timeout and then the request exception. And if there is an error, we can print it. So, let's simply run the code here, and we should see that we get a timeout, right? Because it took more than 1 second to reply. Now, if we had something, I don't know, like 30, and we run this here, and you can see now that we do actually get the response. It didn't time out, it just took a while for us to get it. And then if there was an error, so let's just change the URL to something random, and run this, we should see that it gets an error, and it says request failed, 404, it could not find this URL. Okay, and then lastly, I just want to quickly show you an example of sending an authorization token in a request. Sometimes when you're working with an API, you will generate something called an API token or an authorization token. Now, this is something that needs to be sent alongside your request to essentially verify that you are who you say you are, so that you can perform maybe restricted actions. For example, on my Twitter account, I shouldn't be able to delete a post unless I'm the owner of that Twitter account, right? So, in order to prove that, I send along a token with my request that says, hey, this is me, I'm the owner, and then it allows me to perform this action. Not going to get into too many more details there, but if you are sending a request to an API that requires some kind of token, you typically will embed that token in a header. So, if you want to have a custom header, you specify headers. This case, we're doing an authorization header, and we're saying we're going to have a bearer token, and that token is this right here, which I'm not going to completely expose to you. Now, what I'm showing you right here is an example of actually sending a request to the Twitter or to the X API, where I'm attempting to get my Twitter profile and just get the details about it. This is something that anyone can do. It does require creating an X developer account, which I'm not going to walk you through right now. I'm just showing you a quick demo. So, we generate a response by saying request dot get, we put our base URL, which is this API URL right here, which comes directly from X or from Twitter. We include our headers, which has our authorization token, which allows us to send this request, and then we can print out the information, and we can print out all of the data that we get back. So, if I go here and I run the code, you will see that I get status code 200. This is the URL that I sent to, and then this is the data I received, where this is my Twitter ID, this is the name of the account, and then this was the username. So, just showing you a quick example of sending a bearer or authorization token. Anyways, that is everything for this video. Now, quickly, a word from our sponsor, Brilliant. Brilliant is where you learn by doing with thousands of interactive lessons in math, data analysis, programming, and AI. They adopt a first principles approach, ensuring you understand the why behind each concept. Every lesson is interactive, engaging you in hands-on problem-solving, which is proven to be six times more effective than simply watching lectures. The content is developed by top-notch educators, researchers, and professionals from renowned institutions like MIT, Caltech, and Google. Brilliant emphasizes enhancing your critical thinking abilities through active problem-solving rather than memorization. As you learn specific subjects, you're simultaneously training your mind to think more effectively. Consistent daily learning is crucial, and Brilliant makes it effortless with their bite-sized lessons, allowing you to acquire meaningful knowledge in just a few minutes each day, which is perfect for replacing idle screen time. Additionally, Brilliant offers a comprehensive range of computer science and Python courses, as well as extensive AI workshops, guiding you from a complete beginner to an expert through practical, hands-on lessons. To learn for free on Brilliant, go to brilliant.org/techwithtim, scan the QR code on screen, or click the link in the description. Brilliant has also given our viewers 20% off an annual premium subscription, which gives you unlimited daily access to everything on Brilliant. >> [music]
Original Description
👉 To learn for free on Brilliant, go to https://brilliant.org/techwithtim . Brilliant’s also given our viewers 20% off an annual Premium subscription, which gives you unlimited daily access to everything on Brilliant.
Check out PyCharm, the only Python IDE you need. Built for web, data, and AI/ML professionals.
Download now. Free forever, plus one month of Pro included: https://jb.gg/PyCharm_for_Tim
In the next 15 minutes, you'll learn everything you need to know about requests in Python. I'll talk to you about how to call APIs, all of the relevant code, and cover all of the core theory and terminology that you need to understand.
🎞 Video Resources 🎞
Uv Tutorial: https://www.youtube.com/watch?v=6pttmsBSi8M
⏳ Timestamps ⏳
00:00 | Requests Theory
06:33 | Setup/Install
07:50 | Basic Requests Demo
09:50 | Query Parameters
11:10 | Body (POST)
12:21 | Error Handling
13:29 | Authorization
Hashtags
#pythoncourse #learningpython #callingapis #requestsinpython #python #pythontutorial #masteringpython
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
Why I Ditched Socket.IO for Raw WebSockets (And What I Learned)
Dev.to · Nikhil Sharma
atob() can't decode a JWT — the Base64URL gotcha (and the fix)
Dev.to · Daniel Cheong
Why Debugging Made Me a Better Developer
Medium · JavaScript
Mapping Go Domain Errors to HTTP Status Codes at the Boundary
Dev.to · Gabriel Anhaia
Chapters (7)
| Requests Theory
6:33
| Setup/Install
7:50
| Basic Requests Demo
9:50
| Query Parameters
11:10
| Body (POST)
12:21
| Error Handling
13:29
| Authorization
🎓
Tutor Explanation
DeepCamp AI