Google Cloud Functions

An example integration where a Data Connector forwards events to a Google Cloud Function.


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


The following points are assumed.

Google Cloud Platform

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

Create a Cloud Function

Following this guide, create a new Cloud Function with the following configurations.

(1) Configuration


  • Environment: 2nd gen

  • Function name: As desired.

  • Region: As desired.

Trigger HTTPS

  • Authentication: Allow unauthenticated invocations.

Runtime, build, connections, and security settings

Add a new runtime environment variable with the following values.


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

(2) Code

  • Runtime: Python 3.11

  • Entry point: dataconnector_endpoint

In the Source Code, edit with the following snippet. The implementation is explained in detail on the Data Connector Advanced Configurations page.
import os
import hashlib

import jwt
import functions_framework

# Fetch environment variable secret.

def verify_request(body, token):
    Verifies that the request originated from DT and that the body
    hasn't been modified since it was sent. This is done by verifying
    that the checksum field of the JWT token matches the checksum of the
    request body and that the JWT is signed with the signature secret.

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

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

    return True

def dataconnector_endpoint(request):
    # Extract the body as a byte string and the signed JWT.
    # We'll use these values to verify the request.
    payload = request.get_data()
    token = request.headers['x-dt-signature']

    # Verify request origin and content integrity.
    if not verify_request(payload, token):
        return ('Could not verify request.', 400)

    # Decode the body as JSON
    body = request.get_json(silent=True)

    # Fetch some information about the event, then print.
    # You can implement your own logic here as desired.
    event_type: str = body['event']['eventType']
    device_type: str = body['metadata']['deviceType']
    device_id: str = body['metadata']['deviceId']
    print(f'Got {event_type} event from {device_type} device {device_id}.')

    return ('OK', 200)

Edit requirements.txt with the following entries.


When configured, deploy your function.

Post Deployment

Your function is now ready to receive requests, but we need to know the target URL. In your function, locate the TRIGGER tab and copy the Trigger URL. Save this for later.

Create a Data Connector

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

  • Endpoint URL: The Trigger URL we found in the previous step.

  • Signature Secret: The value of the DT_SIGNATURE_SECRET environment variable.

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

Test the Integration

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

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

  • Verify that the Data Connector endpoint URL is correct.

  • Google provides a host of tools that monitor Cloud Functions. Check the logs for any tracebacks that could explain why an error is returned.

Next steps

Your sensor data is now in the Google Cloud environment, and you can start using it in their various services. Fortunately, Google 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 Function to execute queries.


Once your Database has been set up, the following guide shows you how to connect it to DataStudio for continuous data visualization and analytics.

Last updated