DT Developer Docs
REST APIDT StudioStatus Page
  • Getting Started
  • Overview
  • Concepts
    • Devices
    • Events
    • Topics
      • Temperature Measurement Interval
      • Motion Sensor Activity Timer
  • Data Connectors
    • Introduction to Data Connectors
    • Creating a Data Connector
    • Configuring a Data Connector
    • Receiving Events
    • Best Practices
    • Example Integrations
      • Heroku
      • Google Cloud Functions
      • AWS Lambda
      • Azure HTTP Triggers
      • IBM Cloud Actions
    • Development Guides
      • Local Development with ngrok
  • REST API
  • Introduction to REST API
  • Explore Our Endpoints
    • with cURL
    • with Python API
    • with Postman
  • Authentication
    • OAuth2
    • Basic Auth
  • Error Codes
  • Emulator API
  • Examples
    • Pagination
    • Streaming Events
    • Touch to Identify
    • Refreshing Access Token
  • Reference
  • Status Page
  • Service Accounts
    • Introduction to Service Accounts
    • Creating a Service Account
    • Managing Access Rights
    • Permissions
    • Organizational Structures
  • Other
    • Application Notes
      • Generating a Room Temperature Heatmap
      • Modeling Fridge Content Temperatures
      • Outlier Detection on Multiple Temperature Sensors
      • Simple Temperature Forecasting for Substation Transformers
      • Sensor Data Insight with Power BI and Azure
      • Third-Party Sensor Data in DT Cloud
    • Frequently Asked Question
Powered by GitBook
On this page
  • Overview
  • Prerequisites
  • IBM Cloud Platform
  • Create a Cloud Action
  • Edit Source Code
  • Add Parameter
  • Configure Endpoint
  • Data Connector
  • Test the Integration
  • Next steps
  • PostgreSQL Database

Was this helpful?

  1. Data Connectors
  2. Example Integrations

IBM Cloud Actions

An example integration where a Data Connector forwards events to an IBM Cloud Action.

Last updated 1 year ago

Was this helpful?

Overview

This example uses a Data Connector to forward the events of all devices in a project to an . When receiving the HTTPS POST request, our action will verify both the origin and content of the request using a , then decode the data.

Prerequisites

The following points are assumed.

  • You have a of Project Developer or higher in your DT Studio project.

  • You are familiar with the and know how to .

  • You are familiar with the documentation.

IBM Cloud Platform

While there are many advantages to using a local environment for development, this guide will use the browser portal to minimize setup requirements.

Create a Cloud Action

In the , create and deploy a new Action with one of the following runtimes.

  • Python 3.11

  • Node.js 16

Edit Source Code

import base64
import hashlib
import jwt


def verify_request(body, token, secret):
    # Decode the token using signature secret.
    try:
        payload = jwt.decode(token, secret, algorithms=["HS256"])
    except Exception as err:
        print(err)
        return False

    # Verify the request body checksum.
    m = hashlib.sha1()
    m.update(base64.b64decode(body.encode('ascii')))
    checksum = m.digest().hex()
    if payload["checksum"] != checksum:
        print('Checksum Mismatch')
        return False

    return True


def main(request):
    # Extract necessary request information.
    token = request['__ow_headers']['x-dt-signature']
    body = request['__ow_body']
    secret = request['DT_SIGNATURE_SECRET']

    # Validate request origin and content integrity.
    if not verify_request(body, token, secret):
        return {'statusCode': 400}
    #
    # Further processing here.
    #

    return {'statusCode': 200}
const crypto = require('crypto')
const jwt = require('jsonwebtoken')
  
function verifyRequest(body, token, secret) {
    // Decode the token using signature secret.
    let decoded
    try {
        decoded = jwt.verify(token, secret)
    } catch(err) {
        console.log(err)
        return false
    }
    
    // Verify the request body checksum.
    let shasum = crypto.createHash('sha1')
    let checksum = shasum.update(JSON.stringify(body)).digest('hex')
    if (checksum !== decoded.checksum) {
        console.log('Checksum Mismatch')
        return false
    }
    
    return true
}

function main(params) {
    // Extract necessary request information.
    let buff   = new Buffer.from(params['__ow_body'], 'base64')
    let body   = JSON.parse(buff)
    let token  = params['__ow_headers']['x-dt-signature']
    let secret = params['DT_SIGNATURE_SECRET']
    
    // Validate request origin and content integrity.
    if (verifyRequest(body, token, secret) === false) {
        return {statusCode: 400}
    }
    
    //
    // Further processing here.
    //

    return { statusCode: 200 }
}

Add Parameter

Parameters are default values stored safely on IBM's servers. Once a request is received, it is merged with the incoming dictionary. Add a new parameter with the following Name and Value.

  • Name: DT_SIGNATURE_SECRET

  • Value: A unique password. We will use it later, so write it down.

Configure Endpoint

Under the Endpoints tab, toggle Enable as Web Action and Raw HTTP handling. Copy the HTTP Method URL and save it for later as we will use it when configuring our Data Connector.

Data Connector

  • Endpoint URL: The HTTP Method URL found in the previous step.

  • Signature Secret: The value of DT_SIGNATURE_SECRET parameter set earlier.

Depending on your integration, it can also be smart to disable the event types you are not interested in. For instance, the NetworkStatusEvent is sent every Periodic Heartbeat and will by default be forwarded by the Data Connector if not explicitly unticked.

Test the Integration

If instead the Error counter increments, a response containing a non-200 status code is returned.

  • Verify that the Data Connector endpoint URL is correct.

Next steps

Your sensor data is now in the IBM Cloud environment, and you can start using it in their various services. Fortunately, IBM has some well-documented guides to get you started.

PostgreSQL Database

A database should be tailored to each specific use case. However, if you're uncertain, PostgreSQL (Postgres) is a good place to get started. The following guides will show you how to create a new Postgres database, then connect your Cloud Action to execute queries.

Once deployed, replace the code of your new Action with that matching your runtime. The implementation is explained in detail on the page.

To continuously forward the data to our newly created Cloud Action, a Data Connector with almost all default settings is sufficient. If you are unfamiliar with how Data Connectors can be created, refer to our guide. The following configurations should be set.

If the integration was correctly implemented, the Success counter for your Data Connector should increment for each new event forwarded. This happens each or by touching a sensor to force a new event.

IBM Cloud provides a host of tools that can be used to . Check the logs for any tracebacks that could explain why an error is returned.

.

.

Creating a Data Connector
Periodic Heartbeat
monitor Functions
Getting Started with a PostgreSQL Database
Connecting an External Application
IBM Cloud Action
Introduction to Data Connectors
Create a Data Connector
IBM Cloud Functions
cloud functions dashboard
Data Connector Receiving Events
Role
Signature Secret