Skip to content

Use python requests library

One could integrate the REST API with different languages, for example Python. Similar to curl in the shell, you can use requests in Python (quickly installable via pip install requests). It's a generic HTTP client library that allows you to send HTTP requests.

Let's look at the test example mentioned above, on how to update/change the firmware section of a station. To do this, one needs to get authorization via the "Application access token":

import requests

headers={'accept': 'application/json', 'Authorization': 'Bearer xxx..'}
# where 'xxx..' is your "Application access token"

# authorization is not needed for GET requests:
#headers={'accept': 'application/json'}

Examples of some basic functions to interact M3G API via requests

As mentioned in the example with curl, to perform this specific task, the M3G API requires the id aka the station 9-characters id and the records that will be changed as key/values pairs: the name of the person who is allowed to change the site log (updateMadeBy), the new firmware version (changedTo) and the date. One could write a couple of functions on the fly using the requests module we have just loaded:

#store the M3G API URL:
def url(path):
    return '' + path
#get info about a site log giventhe 9-char id:
def get_sitelog(station_id):
    return requests.get(url('sitelog/view?id={}'.format(station_id)),headers=headers)
#update a record in the site log (the firmware version in this case) given the 9-char id:
def update_firmware(station_id, updateMadeBy, changedTo, datetimeISO, datetimeISOfuture):
    url = url('sitelog/firmware-change?id={}'.format(station_id))
    return requests.put(url, json={
        'updateMadeBy': updateMadeBy,
        'changedTo': changedTo,
        'endOfTheLastSection': datetimeISO,
        'startOfTheNewSection': datetimeISOfuture,
        }, headers=headers)

Update the sitelog with a new firmware

We then send the request to update the site log with the mandatory information:

my_update=update_firmware('BRUX00BEL','Carine Bruyninx', '5.3.1', '2020-09-04T11:50Z', '2020-09-04T11:51Z')

The latter will return as a response one of HTTP status codes, as we mentioned before:


Retrieve the sitelog

We can now check if the record now stored in M3G is what we expect.
We might want to get the link to the updated site log:


or retrieve the info about the sitelog of our station (BRUX00BEL)


and indeed the request is successful:


Get information about the firmware version stored in the sitelog

We can now have a look at the content of the record we're interested in aka firmwareVersion:


and get:


Download the sitelog of a given station at a specific day

As an alternative, we can use another available GET request /sitelog/day-view. This allows you to download metadata corresponding to a specific day (in the YYYY-MM-DD format) for a given station. For example:

 #get info about a site log at a given date YYYY-MM-DD) and given the 9-char id:
 def get_sitelog_date(station_id, date):
     return requests.get(url('sitelog/day-view?id={}&date={}'.format(station_id,date)),headers=headers)


and get: