The Same Request, Now from Code
In APIs for Beginners, requests to web services were typed by hand with curl. Here the same kind of request is sent by a program, and the answer arrives as the lists and dictionaries of Chapter 6, ready to use. The example is a question with a real, changing answer: at today's exchange rate, what was the €86.40 spent on a trip abroad worth in dollars?
It is the same phone call as in that book. The difference is that now the program dials the number, listens to the answer, and writes it down for you.
One-Line Recap
A web service that programs talk to is an API. A program sends it a GET request for an address, a URL, optionally with query parameters after a question mark that say exactly what it wants. The service replies with a status code, where 200 means success, and a body, usually JSON. If any of that is new, APIs for Beginners explains each part properly; this page only needs the one-line version.
requests.get
The requests package installed in Chapter 9 sends the request. requests.get takes the URL, and the query parameters as a dictionary passed as params, so requests builds the question-mark part correctly for you. The timeout argument says how many seconds to wait at most; the next topic explains why it must always be there. The service used here publishes daily reference exchange rates and needs no key. Its address and answer format are as of 2026, and the page on it at frankfurter.dev is the place to check if anything below stops matching.
from decimal import Decimal import requests response = requests.get( "https://api.frankfurter.dev/v2/rates", params={"base": "EUR", "quotes": "USD"}, timeout=5, ) print("Status:", response.status_code) data = response.json() print(data) rate = Decimal(str(data[0]["rate"])) spent = Decimal("86.40") print(f"EUR {spent} was USD {spent * rate:.2f} at the rate of {data[0]['date']}")
Status: 200
[{'date': '2026-09-23', 'base': 'EUR', 'quote': 'USD', 'rate': 1.1452}]
EUR 86.40 was USD 98.95 at the rate of 2026-09-23
Reading the Response
response.status_code is the status code: 200, success. response.json() reads the JSON body and gives back Python data, already parsed. For this service it is a list containing one dictionary per currency asked for, with the date, the base currency, the quoted currency and the rate. Reading the rate is Chapter 6's skill: the first item of the list, then its rate key, data[0]["rate"].
The Conversion
The rate arrives as a float, because JSON numbers become floats. Money in this book is Decimal, so the program turns the rate into text with str and then into a Decimal, which keeps exactly the digits the service sent. Multiplying the amount by the rate gives the dollar value, formatted to two places only when printed.
The Part That May Change
Everything on this page about Python, requests.get, params, status_code and json(), works the same with any web service. The address, the parameter names and the shape of the answer belong to this one service, and services change them from time to time. If the service changes, the code needs a new URL and a new line to find the rate in the answer; nothing else.
- "
response.json()is JSON text." It is already Python data: lists, dictionaries, numbers and strings, parsed for you. - "Building the question-mark part of the URL by hand is fine." It works until a value contains a space or an ampersand. Pass a params dictionary and requests encodes it correctly.
- "A 200 status means the data is exactly what I expected." It means the service answered. Check that the shape you read is the shape the documentation promises.
- Weather, maps, payments, AI models: nearly every service a program uses is reached exactly this way.
- It connects this book to APIs for Beginners: the same promise between programs, now kept by your own code.
Knowledge Check
What does the params argument of requests.get do?
- Sets how long to wait for the service to send its answer
- Builds the query part of the URL from a dictionary
- Chooses which service on the internet the request goes to
- Converts the answer into Python data automatically
What does response.json() give back?
- The raw text of the answer, exactly as the service sent it
- The status code of the answer, such as 200
- The answer's JSON, already turned into Python data
- A file on disk containing the whole answer
How does the program reach the rate in [{'date': ..., 'rate': 1.1452}]?
data["rate"], reading the key straight awaydata[1]["rate"], the second item in the listdata.rate, using a dot, the way you would call a methoddata[0]["rate"], the first item and then its key
Why is the rate turned into a Decimal with Decimal(str(...))?
- It is used for money, and going through text keeps its exact digits
- requests refuses to return any number unless it is already a Decimal
- The multiplication would fail with a float and an int
- Decimal values are sent back to the service more quickly
You got correct