Sometimes a problem that you want to write code to solve may involve real-time data. For example, using weather data to plan an outfit for the day, maybe while you're on your dream vacation. In order to access data such as the current weather or stock prices or news, or coding a web search engine to find relevant web pages. You need to use something called an API or application programming interface. An API gives your computer a way to talk a second computer to get that second computer to do something for you, such as pull up a city's current weather and return that to your computer. There are lots of APIs on the internet. Some are free and many that might require a fee, since it often cost someone else something to run a computer and operate that API service. But in contrast to downloading a package to run on your computer, APIs let you get someone else's computer with permission to do work. To do something for you. Let's take a look at how APIs work. Using a fun example of getting real-time weather data using an API to plan an outfit for the day. APIs are a way for your computer to get a different computer to fetch some data, or to do some work for you. Think of this as akin if you are sitting in a restaurant and you would like some food. You would talk to the nice waiter who then kindly go to the kitchen to get and deliver the meal that you requested to you. So the waiter is the Go-Between between you and the kitchen. And that's what the API does as a go between, between you and someone else's computer that provides data or provides a service. So let's go through an example. I'm going to import OS import requests. And now I'm going to from AI setup rather than helper functions import print LLM response. And I'm also going to use a package called dotenv which I'll see a little bit more about later. I'm going to use the API provided by this website. Openweathermap.org and I'm in Palo Alto, California, USA right now. And so the weather right here at this moment is 24°C. Pretty nice out here. But rather than using the web interface to look up weather, I'm going to see how to use an API for my computer to top to their computer to fetch the weather automatically. It turns out most APIs will ask It turns out most APIs will ask for something called a key or an API key. You can think of an API key as a password that is unique to you. This way, the computer that is delivering on your request knows who the request is coming from. So here's a snippet code to load the API key that is unique to me. Don't worry too much for now. What this code is doing. I'll come back and say a little bit more about this later and what API keys is. I'm going to specify the latitude and longitude of where I am, and then here's some code. So here's a URL f-string. This is a URL to the API. Insert in the f-string the latitude, longitude and then also the API key. And then I'm going to get a response by fetching something to that URL. And here's a syntax for printing out the data that we just got back from the response. It turns out that this is a dictionary with a lot of values. Temperature is 24.02 degrees. It feels like 23.98 degrees. And then this is a description which is the sky is clear. Here's the wind speed degrees and so on. So this is a pretty complicated dictionary. In order to extract out maybe the key information I want, the temperature, the description as well as the wind speed. Let me just ask my chatbot to write the code for me. I'm going to ask it how do I get the temperature description and wind speed from this data variable? And then let me go back and copy this and give all this to my chatbot and have it figured out for me. Let's see. Okay, it gives me a long explanation, but I'm just going to copy the code here and run it. And there you go. It has extracted the temperature and the temperature of verbal description and wind speed. And if you want a nice weather report, we can take this data that we extracted and print it out like this. And when you run this code, feel free to look up the latitude and longitude for your own city and plug it in and look up the current weather for your own city. And one fun thing we can say based on the following weather suggests an appropriate outdoor outfit. And again, if you are specific clothing preferences, you can add to the prompts by just to keep a big generic, you know, lightweight breathable t-shirt and shorts. Sunglasses and sunscreen. Sunscreen is actually a good idea. And so this is how you can call an API over the internet to get information from this other computer, from this web server API dot open weather map.org. For different types of data services. There will be different APIs and you may need to look online for documentation or answer AI chatbot for help to figure out how to call different APIs. The AI chatbot is more likely to know how to call the more popular APIs on the internet. For the less popular ones, you need to look at the documentation yourself. One last thing, for many APIs, you need to provide a value for an API key, which is a secret string of numbers and letters that lets the website know that it is you making this API request call. So we've already set up this Jupyter notebook environment to use this API. But if you actually want to do this yourself on your computer, you need to go to this website, register for an account and get a secret API key to then set the API key variable to be equal to. One way to do that would be to have a line of code that says API key equals whatever that secret string of characters and numbers. And if you run this line of code with an actual API key and then call the API, that will work. But it turns out that even though this works, most programmers don't have a line of code like this. Because if you put your API key right there into your code, then if your code ever leaks to someone else, then others will have access to your secret API key. And so what most programmers will do is use the dotenv packages the dotenv function, which goes through a couple extra steps to securely load and use the API key. And if you want to understand what these two lines of code do, you can ask the AI chatbot to explain how to store API keys a bit more securely if you're interested. So that's it. In this lesson we saw how to get real-time data. That is weather data. It turns out there are also many APIs that let you access advanced AI models. In fact, Print LLM response actually uses an API on the internet to access Open AI's ChatGPT large language model. In the next lesson, we'll dive into the guts of how print LLM response actually works and how you can write code to access advanced AI models over the internet. Let's go see that in the next video.