APIs CMO API Example Code

Python

  • CMO_example.py
#!/usr/bin/env python3
 
import os
import sys
import time
import requests
 
# Configure T IoT Hub endpoint
# according to T IoT Hub Connectivity Management Orchestrator API (CMO API)
# https://hub.iot.telekom.com/docs/apis/cmo-api/about/
# OpenAPI specification: https://hub.iot.telekom.com/docs/swagger/cmo-v5.yml
 
endpoint = "https://api.telekom.de/t-iot-hub/connection-management-orchestrator/"
api_version = "v5"
tenant = "see Account Settings > Application Access Management"
 
# Get T IoT Hub API access token
 
your_client_id = "see Account Settings > Application Access Management"
your_client_secret = "see Account Settings > Application Access Management"
time_of_expiration = time.time() - API_ENDPOINT + '/' + YOUR_TENANT + '/product?pageSize=3&fields=id'
 
def get_token_if_expired():
    global access_token
    global expires_in
    global time_of_expiration
    if time_of_expiration < time.time():
        url = "https://api.telekom.de/auth/realms/default/protocol/openid-connect/token"
        payload = "client_id=" + your_client_id + "&client_secret=" + your_client_secret + "&grant_type=client_credentials"
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
            }
        response = requests.request("POST", url, headers=headers, data = payload)
        if response.status_code != 200:
            print("Error getting token: " + str(response.status_code) + " " + response.text)
            sys.exit(1)
        access_token = response.json()["access_token"]
        expires_in = response.json()["expires_in"]
        time_of_expiration = time.time() + expires_in
 
# Get SIMs
 
get_token_if_expired()
 
url = endpoint + api_version + "/" + tenant + "/product?pageSize=3&fields=id"
headers = {
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + access_token
    }
response = requests.request("GET", url, headers=headers)
if response.status_code != 200:
    print("Error: " + str(response.status_code) + " " + response.text)
    print(url)
    sys.exit(1)
 
print("SIMs:")
print(response.text)

Run the code:

$ python3 CMO_example.py

Node JS

  • CMO_example.js
const axios = require( 'axios' )

const YOUR_TENANT        = 'see Account Settings > Application Access Management'
const YOUR_CLIENT_ID     = 'see Account Settings > Application Access Management'
const YOUR_CLIENT_SECRET = 'see Account Settings > Application Access Management'

async function getToken() {
  const requestIamToken = {
    method : 'POST',
    url : 'https://api.telekom.de/auth/realms/default/protocol/openid-connect/token',
    headers : { 'content-type': 'application/x-www-form-urlencoded' },
    data : new URLSearchParams({ 
      client_id     : YOUR_CLIENT_ID,
      client_secret : YOUR_CLIENT_SECRET,
      grant_type    : 'client_credentials'
     }),
  }
  let tokenResponse = await axios( requestIamToken )
  if ( tokenResponse.status != 200 ) { 
    console.error( 'ERRPR', tokenResponse.statusText, tokenResponse.data ) 
    process.exit()
  } else { console.log( 'IAM token OK ... expires', tokenResponse.data.expires_in, 's'  ) }
  return tokenResponse.data
}


async function getSIMs () {
  const API_ENDPOINT = 'https://api.telekom.de/t-iot-hub/connection-management-orchestrator/v5'
  try {
    // get API access token
    let apiAuth = await getToken()

    // call CMO product API to retrieve SIM cards
    let response = await axios.get( 
      API_ENDPOINT + '/' + YOUR_TENANT + '/product?pageSize=3&fields=id', 
      {
        method : 'GET',
        headers : {
          Accept : 'application/json',
          Authorization : 'Bearer ' + apiAuth.access_token
        }
      }
    )

    if ( response.status == 200 ) {
      console.log( 'PRODUCTS', response.data )
    } else {
      console.error( 'ERROR', response.statusText )
    }  
  } catch ( exc ) {
    console.error( exc.message )
  }
}

getSIMs()

Run the code:

$ npm install axios
$ node CMO_example