Python Example API Call (Get)

An API, or Application Programming Interface, is a library of related programme code available for programmers to use. People and organisations can provide a service, which is available to use via an API. In the case of the example below, weather data is made available via an HTTP Get request, which returns the data in JSON format. This API returns a five-day forecast, for a specific area, at three hourly intervals. Full details of this API can be found on the Rapid API website here, as well as the API's own website here.

A sample of the weather data returned can be seen below, which covers two time periods on a particular day. It should be noted that the data is returned in one long JSON string, rather than nicely formatted, as shown here.

{
  "timezone": "Europe/London",
  "city_name": "Bath",
  "lon": -2.362,
  "state_code": "ENG",
  "lat": 51.3849,
  "data": [
    {
      "wind_cdir": "SW",
      "wind_gust_spd": 8.52,
      "pod": "d",
      "pres": 995,
      "clouds_hi": 0,
      "clouds_low": 100,
      "clouds_mid": 85,
      "vis": 24,
      "wind_spd": 4.22,
      "wind_cdir_full": "southwest",
      "slp": 1006,
      "datetime": "2025-06-07:09",
      "ts": 1749286800,
      "dewpt": 8.7,
      "uv": 2,
      "snow": 0,
      "ghi": 688,
      "dhi": 107,
      "precip": 0.00048828125,
      "dni": 846,
      "temp": 12.6,
      "app_temp": 12.6,
      "pop": 15,
      "timestamp_utc": "2025-06-07T09:00:00",
      "weather": {
        "icon": "c04d",
        "description": "Overcast clouds",
        "code": 804
      },
      "solar_rad": 196.20615,
      "clouds": 99,
      "timestamp_local": "2025-06-07T10:00:00",
      "snow_depth": 0,
      "ozone": 394,
      "rh": 77,
      "wind_dir": 236
    },
    {
      "wind_cdir": "WSW",
      "wind_gust_spd": 10.74,
      "pod": "d",
      "pres": 995,
      "clouds_hi": 100,
      "clouds_low": 70,
      "clouds_mid": 100,
      "vis": 7.4,
      "wind_spd": 4.88,
      "wind_cdir_full": "west-southwest",
      "slp": 1006,
      "datetime": "2025-06-07:12",
      "ts": 1749297600,
      "dewpt": 11,
      "uv": 3,
      "snow": 0,
      "ghi": 905,
      "dhi": 119,
      "precip": 0.4321289,
      "dni": 905,
      "temp": 14,
      "app_temp": 14,
      "pop": 30,
      "timestamp_utc": "2025-06-07T12:00:00",
      "weather": {
        "icon": "r01d",
        "description": "Light rain",
        "code": 500
      },
      "solar_rad": 179.14203,
      "clouds": 100,
      "timestamp_local": "2025-06-07T13:00:00",
      "snow_depth": 0,
      "ozone": 391,
      "rh": 82,
      "wind_dir": 240
    }
  ],
  "country_code": "GB"
}

Once the necessary modules are imported, the API request is constructed, and the weather data is retrieved. The body of the response is then converted from a string into a JSON object so that the individual data elements can be extracted as needed. The name of the city that the data refers to is displayed, then the weather data for each day and time period is processed. Each piece of data is extracted from the JSON object, formatted where necessary, then output as part of a formatted string.

from datetime import datetime
import http.client
import json

# Client for HTTP request and response.
client = http.client.HTTPSConnection("weatherbit-v1-mashape.p.rapidapi.com")

# API headers.
headers = {
    'x-rapidapi-key': "api-key-goes-here",
    'x-rapidapi-host': "weatherbit-v1-mashape.p.rapidapi.com"
}

# API request.
client.request("GET", "/forecast/3hourly?lat=51.38488&lon=-2.36197", headers=headers)

# API response.
ressponse = client.getresponse()
data = ressponse.read()

# Assign the body of the response to a variable and parse the JSON.
json_object = json.loads(data) 
jsonData = json_object["data"]

# Display the city name.
print(json_object['city_name'])

# Display the information for each day and time period.
for dayTime in jsonData:

    # Extract and format the required data.
    date_time = datetime.strptime(dayTime['timestamp_local'], '%Y-%m-%dT%H:%M:%S')
    date_timm_formatted = date_time.strftime('%d/%m/%Y %H:%M:%S')
    temperature = round(dayTime['temp'])
    weather = dayTime['weather']
    weather_description = weather['description']

    # Display as: 'Date Time - Temperature - Weather Description'.
    print(f"{date_timm_formatted} - {temperature}\u00b0C - {weather_description}")

Sample output to the console can be seen below.

Bath
07/06/2025 10:00:00 - 13°C - Overcast clouds
07/06/2025 13:00:00 - 14°C - Light rain
07/06/2025 16:00:00 - 14°C - Light rain
07/06/2025 19:00:00 - 14°C - Light shower rain
07/06/2025 22:00:00 - 12°C - Clear Sky
08/06/2025 01:00:00 - 10°C - Clear Sky
08/06/2025 04:00:00 - 9°C - Clear Sky
08/06/2025 07:00:00 - 10°C - Clear Sky
08/06/2025 10:00:00 - 13°C - Clear Sky
08/06/2025 13:00:00 - 16°C - Clear Sky
08/06/2025 16:00:00 - 16°C - Few clouds
08/06/2025 19:00:00 - 15°C - Clear Sky
08/06/2025 22:00:00 - 12°C - Clear Sky
09/06/2025 01:00:00 - 11°C - Clear Sky
09/06/2025 04:00:00 - 10°C - Clear Sky
09/06/2025 07:00:00 - 11°C - Scattered clouds
09/06/2025 10:00:00 - 15°C - Clear Sky
09/06/2025 13:00:00 - 17°C - Overcast clouds
09/06/2025 16:00:00 - 17°C - Scattered clouds
09/06/2025 19:00:00 - 16°C - Overcast clouds
09/06/2025 22:00:00 - 13°C - Overcast clouds
10/06/2025 01:00:00 - 13°C - Overcast clouds
10/06/2025 04:00:00 - 13°C - Clear Sky
10/06/2025 07:00:00 - 13°C - Overcast clouds
10/06/2025 10:00:00 - 17°C - Overcast clouds
10/06/2025 13:00:00 - 18°C - Overcast clouds
10/06/2025 16:00:00 - 19°C - Scattered clouds
10/06/2025 19:00:00 - 17°C - Scattered clouds
10/06/2025 22:00:00 - 15°C - Clear Sky
11/06/2025 01:00:00 - 13°C - Clear Sky
11/06/2025 04:00:00 - 11°C - Scattered clouds
11/06/2025 07:00:00 - 12°C - Overcast clouds
11/06/2025 10:00:00 - 17°C - Few clouds
11/06/2025 13:00:00 - 22°C - Clear Sky
11/06/2025 16:00:00 - 24°C - Clear Sky
11/06/2025 19:00:00 - 22°C - Scattered clouds
11/06/2025 22:00:00 - 19°C - Few clouds
12/06/2025 01:00:00 - 19°C - Few clouds
12/06/2025 04:00:00 - 16°C - Overcast clouds
12/06/2025 07:00:00 - 16°C - Overcast clouds