Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

APIs

  • Application Programming Interface

  • A standardised interface for access to:

    • data and transfers,

    • programs and services,

    • general communication between apps/programs.

  • The “hidden” version of a User Interface letting computers and programs communicate.

  • Can be limited (by owners) on:

    • number of requests per time unit,

    • access codes/credentials.

Web APIs

  • Hyper Text Transfer Protocol (HTTP) based queries and answers using POST or GET methods.

  • Each API has its own hierachy and possibilites for querying.

  • URLs used for querying using the GET method typically consist of:

    • a server address: http://api.openweathermap.org,

    • a hierarchy with descriptive names: /data/2.5/forecast, and

    • a question mark marking the beginning of user supplied named variables with
      contents joined by ampersands: ?q=London&appid=MY_API_KEY

  • Variations in naming include data / api, q / query.

  • Some APIs use time-limited access tokens, e.g., see BarentsWatch tutorial and GitHub example:

    • First POST to the API includes a client ID and client “secret”.

    • A token (temporary password) is returned and can be used for subsequent requests (typically expiring after 3600 s).

  • APIs are not eternal.

    • Formats are changed over time.

    • Sometimes different formatting can be accessed by the 1.0/1.1, etc. part of the URL.

JSON

  • The query can be a JSON string, e.g., {‘city’: ‘London’, ‘year’: ‘2000’} which can be sent separately,
    see example below using POST.

  • Also the returned contents are often JSON formatted.

Example with JSON query and JSON-stat return

  • Statistics Norway (SSB)

  • Traffic accident data

<Response [200]>
(250, 6)
Loading...
<Response [200]>
(47520, 6)
Loading...

Exercise

  • Visit Statistics Norway’s Ready-made datasets.

  • Select a different dataset, download through the API and inspect the results.