🌐 Internet Requests Cheatsheet

Fetch data from the internet with requests. Copy any example into the Editor and press ▶ Run.

🚀 Getting started

Ask a website for some data with requests.get(...), then read what comes back.

import requests

response = requests.get("https://catfact.ninja/fact")
data = response.json()

print("🐱", data["fact"])

Most web APIs send back JSON — a bit like a Python dictionary. response.json() turns it into one you can read with data["..."].

💡 catfact.ninja and Open-Meteo both work without any sign-up or password (an "API key"), which makes them great for practising.

📥 Making a request

The two requests you will use most are get (fetch data) and post (send data).

requests.get(url)Ask a website for data.
requests.get(url, params={...})Add options to the web address (a query string).
requests.post(url, json={...})Send some JSON data to a website.
requests.post(url, data={...})Send form data to a website.
import requests

# params become ?latitude=51.51&longitude=-0.13&... on the end of the URL.
url = "https://api.open-meteo.com/v1/forecast"
response = requests.get(url, params={
    "latitude": 51.51,
    "longitude": -0.13,
    "current_weather": True,
})
print(response.json()["current_weather"])

📦 Reading the response

A request gives you back a Response. Here is what you can read from it.

response.json()The data as a dictionary/list (for JSON APIs).
response.textThe raw reply as a string.
response.status_codeThe result number: 200 means OK, 404 means "not found".
response.okTrue if the request worked.
response.contentThe raw reply as bytes.
response.raise_for_status()Raise an error if the request failed.
import requests

response = requests.get("https://catfact.ninja/fact")

if response.ok:
    print("Status:", response.status_code)
    print("Fact:", response.json()["fact"])
else:
    print("Something went wrong:", response.status_code)

🌤️ Example: the weather

Open-Meteo gives free weather data with no API key. Change the latitude and longitude to look up your own town.

import requests

url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 51.51,     # London
    "longitude": -0.13,
    "current_weather": True,
}

data = requests.get(url, params=params).json()
now = data["current_weather"]

print("🌡️ Temperature:", now["temperature"], "°C")
print("💨 Wind speed:", now["windspeed"], "km/h")

🐾 Example: random facts

Some fun, no-key APIs that are great for experimenting. Notice how each one puts the data in a slightly different place, so you have to look at what comes back.

# A dog fact (the text is nested a few levels deep)
import requests
data = requests.get("https://dogapi.dog/api/v2/facts").json()
print("🐶", data["data"][0]["attributes"]["body"])
# A cat fact (simpler — the text is right at the top)
import requests
data = requests.get("https://catfact.ninja/fact").json()
print("🐱", data["fact"])
💡 To explore an API, first print(response.json()) to see the whole reply, then pick out the part you want with data["..."].

📦 The real requests & limits of this version

In this editor, requests is a small stand-in that fetches data using the web browser itself, so there is nothing to install here. The examples above use the same commands as the real library, so your code should run elsewhere too.

Using it outside this editor. On a normal computer (for example in Thonny or IDLE), install the official library first:

pip install requests

The official project lives at pypi.org/project/requests with docs at requests.readthedocs.io. It is the most popular way to make internet requests in Python.

The big one: some websites are blocked here. For safety, web browsers only let a page fetch data from sites that allow it (a rule called CORS). Lots of APIs that work perfectly in Thonny will be refused in this editor. When that happens you will see a friendly message explaining it — it is not a mistake in your code. The APIs on this page (Open-Meteo, cat facts, dog facts) are all CORS-friendly.

What this editor's version does not do. It only copies the most common parts of requests:

get & post onlyOther methods like put, delete, head and patch are not available here.
No custom headersYou cannot set request headers= or send cookies, so APIs that need an API key in a header will not work here.
No sessions / authrequests.Session and auth= (logins) are not supported.
Fewer response bitsYou get status_code, ok, text, json(), content and raise_for_status(). Extras like response.headers and response.cookies are not included.
No file downloadsStreaming large files or downloading images to disk is not supported in the browser editor.
CORS appliesAs above — only sites that allow browser requests will work here. The real requests on your computer has no such limit.
💡 Stick to requests.get / requests.post with CORS-friendly APIs and read the response with .json() / .text, and your program will behave the same here and with the official library.