API Calls

Application Programming Interface (API)

It's a "contract" that defines how separate software interacts with each other.

HTTP Requests

GET - get data (what are the available timings tomorrow)

POST - creating or inserting new data (making a lunch reservation)

PUT - update existing data (updating the timing for a lunch reservation)

DELETE - deletes data (removing your lunch reservation)

Let's Start

First, we import an external library that helps us make API calls

import requests

Next, we will query an API of your choice.

  • Google the API you are looking for (e.g. searching "random cat facts API" )

  • We will then make use of this API: https://catfact.ninja/

The Cat Facts API

To obtain a random fact, we call .get on /fact

  • When running this, you should get <Response [200]> .

  • A status code of 200 means the API call was successful.

Let's call the data we received

We should get a random fact about cats in a dictionary

API responses are typically encoded as JSON (JavaScript Object Notation). For us to use it in python, we can call the .json() method to convert it into a dictionary.

Call the fact and print the String

Piecing it all together

Another Example

Finding the parking lots available at a carpark

Description of the API can be found here

This sends a query to the API to obtain the carpark availability

Retreives the number number of carparks with data

Last updated