Skip to content

AronAyub/Sigfox-GET-POST-request-API---Python-

Repository files navigation

Working With GET Requests/ API In Sigfox Backend

Introduction

APIs access in Sigfox Backend are restricted to authenticated API user, the first step to access API is to generate API credentials. Generating API can be done by your SO in the Sigfox Backend. API credentials are name, & applicable time zone. Accessible API methods and output response will be determined by the roles which are set for API credential. E,g Read only will allow you to access reading of the data only. API with Device Manger[W] is one of useful API for device management which allows registration or moving devices, creating and editing callbacks and accessing a device's PAC, etc.

More on REST request for POST, GET, DELETE, PUT HTTP requests used are widely covered in the full documentation

Useful Documentation

Highlight:

  • No pooling message and device sync status through API
  • Reasonable API calls according to fleet
  • Retrieve message and device status by Callback
  • Reasonable callbacks per Device type

HTTPS GET REQUEST PYTHON SCRIPT

This API is very much useful for data consumption.

Consuming open Source data

Start by consuming unauthenticated APIs. In python, a few lines of code are used:

# Consuming open source APIs
# import requests module

import requests
response  = requests.get("https://datausa.io/api/data?drilldowns=Nation&measures=Population")
print(response) # Get 200 as response for a correct Request.
print(response.json()) # Print JSON data from the requested URL

GET REQUEST Using SigfoxAPI

  • To demonstrate the GET Request, i am retriving a list of messages , this can be found in API documentation.

screen1

So far there are two methods i found when using Python.

Direct Method

Involves having the auth parameters directly embedded in the code.

Python Scripts to obtain JSON Data using auth module

Install and Import Requests

pip install requests

Import Request

import requests

Define your auntentication variables from your API

login = "yourapiusername"
password = "yourapipassword"

Assign your login and password a variable.

authentication = (login, password)

From the sample we are using our API format is: https://api.sigfox.com/v2/devices/{id}/messages, rreplace id with your device ID, should be in Hex.

Below is the url format and how you pass the authentication details. I am using auth module to pass my authentication details.

response = requests.get("https://api.sigfox.com/v2/devices/id/messages",auth=authentication) 
print(response.json())

Enconded method

Using encoded method to hide your password in the code.

Code explained.

Import dependent packages

import requests
import requests
from requests.auth import HTTPBasicAuth
import base64
import time

Url defination

url = "url does not change, just as from the previous method"

The token is generated by encoding login:password in Base 64

print("----TOKEN----")
login = 'apiusername'
password = 'apipassword'
token = login+":"+password
print("Token:", base64.b64encode(token.encode("ascii")).decode("ascii"))
print()
  • Once the token is generated, you can use the token directly in your code, it is safer in case your code leaks.
headers = {
  'Authorization': 'Basic xxxyoutgeneratedtockenherexxx'
}
response = requests.get(url, headers=headers)
print(response.text)
print()
  • Because I do 2 API requests in a row, Sigfox limits to 1 request per second, so I need to slow my strict down, use sleep time
time.sleep(2) 
Sample returned data with my device ID and API defined

SCREEN2

Same auth parsing should apply for other HTTPs Request, unless specified otherwise in the documentations. Enjoy -

Java Experts can explore Axios library in Javascript to get data using API

const params = {}
if (limit) {
  params.limit = limit
}
if (start) {
  params.since = start
}
if (end) {
  params.before = end
}
const myurl = `https://api.sigfox.com/v2/devices/${DEVICE_ID}/messages`;
const resulting = await axios.get(myurl, {
    params: params,
    auth: {
    username: UNAME,
    password: UPASS
  }
});
  • The variables UNAME and UPASS correspond to authentication credentials, which can be obtained in your Sigfox account page as you can see in the Sigfox API documentation. The parameters limit, since and before allows to control the time range of the response.

⚡Aron Ayub

About

scripts to get data using API from SigfoxBackend

Resources

License

Stars

Watchers

Forks