"What's the exchange rate today?" "Will it rain tomorrow?" These are questions many of us look up online several times a day. But you don't have to keep refreshing pages — a short Python script can pull the latest weather, exchange rates and more for you automatically, and even assemble them into a "morning briefing" you see as soon as you switch on your computer. In this article we'll start from zero and learn how to fetch live data from an API.
What is an API?
An API (Application Programming Interface) is essentially a doorway that lets your program "ask questions". To get the weather, you send a web address out, and the other side returns a bunch of structured data (usually in JSON format). Unlike you clicking into a browser, a program can read that data directly — and automate the whole thing, which is the whole point.
By the way, JSON is a very common data format that looks a lot like a Python dict: wrapped in braces and made up of "key : value" pairs. The vast majority of APIs return JSON, which is why r.json() is the step you'll use constantly — it turns that JSON into a Python dict for you automatically, so you can grab whichever value you need.
Step 1: Install requests
Python has a built-in library called urllib, but few people use it directly because requests is ten times friendlier. Install it from your terminal:
Step 2: Get Hong Kong weather (Open-Meteo, free, no key)
Open-Meteo is a free weather API — no sign-up, no API key. Let's grab Hong Kong's current temperature, humidity and wind speed:
Three things to note: params keeps the query parameters separate and requests builds the URL for you (handling special characters automatically); r.json() turns the returned JSON straight into a Python dict; and data["current"] is the block holding the current conditions.
Step 3: Get an exchange rate (Frankfurter, free, no key)
For exchange rates I use Frankfurter, also free with no key. For example, US dollars to Hong Kong dollars:
from is the base currency and to the target; Frankfurter also lets you fetch several at once (e.g. to=HKD,CNY,JPY). The returned rates is a dict, so ["HKD"] gives you the rate directly.
Step 4: Combine them into a morning briefing
With those two techniques in hand, you can build a small tool that shows both weather and exchange rate in one run:
Each function fetches one kind of data, then the main program combines the output. The f"..." syntax is an f-string, which lets you drop variables straight into a string and saves a lot of typing.
What if it goes wrong? Add a timeout and try/except
In the real world an API isn't always cooperative — the network can be slow, the server busy, or the URL temporarily down. So for anything you use seriously, add a timeout and try/except so your program doesn't hang or crash outright:
timeout=10 means wait at most 10 seconds; raise_for_status() raises an error automatically when the HTTP status isn't 200, so you don't have to guess whether something went wrong.
Tip: when do you need an API key?
Free APIs usually come with limits — a few calls per minute, or only basic data on the free tier. Open-Meteo and Frankfurter need no key at all, but many commercial APIs (Google, OpenAI, licensed data) require you to sign up, get a key, and put it in the request header. We'll cover that another time.
Final step: run it on a schedule
Want it to run every morning automatically? On Windows use Task Scheduler and set it to "Daily 08:00", pointing at your script; on Mac, launchd does much the same. The simplest option is Python's schedule library in an infinite loop — a few lines and you're done. We'll cover those in a future article.
Conclusion
This Python tutorial summary: Fetching live data from an API is nowhere near as hard as it sounds. Once you've mastered the basic requests move, weather, exchange rates, stock prices, transport, news — basically anything you can "look up online" — can be pulled automatically by a script, saving you the daily grind of manual checking. And there's a bonus: what you get back is structured data, so you can crunch numbers, draw charts, or even auto-forward it to WhatsApp or email. Next time you need some data, first ask yourself: "Is there an API for this?"