Calling Basic API with Python

An Application Programming Interface (API) is an essential part of the data gathering process as it is a popular way of how data can be shared across many users and interfaces. In order to utilize the data found from using an API, one must be able to call it using a programming language such as Python.

So, let’s go through the process by using the Weather API. Firstly, we will use the Requests module to be able to call using Python.

import requests

Once we have the Requests library imported, we will call the API endpoint URL: https://api.weather.gov/points/{lat},{lon}, where the latitude and longitude is dynamic by using curly braces and an f-string. Let’s use the coordinates of the Space Needle in Seattle (47.620422, -122.349358) for this example.

lat = 47.620422
lon = -122.349358
url = f'https://api.weather.gov/points/{lat},{lon}'

Now let’s create a call on the API in JSON format:

response = requests.get(url).json()

Printing the response:

{
  '@context': [
    'https://geojson.org/geojson-ld/geojson-context.jsonld',
    {
      '@version': '1.1',
      'wx': 'https://api.weather.gov/ontology#',
      's': 'https://schema.org/',
      'geo': 'http://www.opengis.net/ont/geosparql#',
      'unit': 'http://codes.wmo.int/common/unit/',
      '@vocab': 'https://api.weather.gov/ontology#',
      'geometry': {
        '@id': 's:GeoCoordinates',
        '@type': 'geo:wktLiteral'
      },
      'city': 's:addressLocality',
      'state': 's:addressRegion',
      'distance': {
        '@id': 's:Distance',
        '@type': 's:QuantitativeValue'
      },
      'bearing': {
        '@type': 's:QuantitativeValue'
      },
      'value': {
        '@id': 's:value'
      },
      'unitCode': {
        '@id': 's:unitCode',
        '@type': '@id'
      },
      'forecastOffice': {
        '@type': '@id'
      },
      'forecastGridData': {
        '@type': '@id'
      },
      'publicZone': {
        '@type': '@id'
      },
      'county': {
        '@type': '@id'
      }
    }
  ],
  'id': 'https://api.weather.gov/points/47.6203999,-122.3494',
  'type': 'Feature',
  'geometry': {
    'type': 'Point',
    'coordinates': [
      -122.3494,
      47.6203999
    ]
  },
  'properties': {
    '@id': 'https://api.weather.gov/points/47.6203999,-122.3494',
    '@type': 'wx:Point',
    'cwa': 'SEW',
    'forecastOffice': 'https://api.weather.gov/offices/SEW',
    'gridId': 'SEW',
    'gridX': 124,
    'gridY': 69,
    'forecast': 'https://api.weather.gov/gridpoints/SEW/124,69/forecast',
    'forecastHourly': 'https://api.weather.gov/gridpoints/SEW/124,69/forecast/hourly',
    'forecastGridData': 'https://api.weather.gov/gridpoints/SEW/124,69',
    'observationStations': 'https://api.weather.gov/gridpoints/SEW/124,69/stations',
    'relativeLocation': {
      'type': 'Feature',
      'geometry': {
        'type': 'Point',
        'coordinates': [
          -122.350876,
          47.620499
        ]
      },
      'properties': {
        'city': 'Seattle',
        'state': 'WA',
        'distance': {
          'unitCode': 'wmoUnit:m',
          'value': 111.17337060888
        },
        'bearing': {
          'unitCode': 'wmoUnit:degree_(angle)',
          'value': 95
        }
      }
    },
    'forecastZone': 'https://api.weather.gov/zones/forecast/WAZ558',
    'county': 'https://api.weather.gov/zones/county/WAC033',
    'fireWeatherZone': 'https://api.weather.gov/zones/fire/WAZ654',
    'timeZone': 'America/Los_Angeles',
    'radarStation': 'KATX'
  }
}

Wow, that’s a lot of information! However, let’s just look at the city and state to make sure out coordinates are right. We can filter to only see a part of the JSON (location) by inputting:

response['properties']['relativeLocation']['properties']['city'], response['properties']['relativeLocation']['properties']['state']

Our results should return:

('Seattle', 'WA')

Yes! The Space Needle is indeed in Seattle, Washington. Therefore, we were able to use a coordinate to bring in data about the weather from the Weather API.