How to create Datapoints with node.js

In this guide you will learn how to add data points to DRIVR using JavaScript. We assume that you have already created your own domain and created a suitable data model in DRIVR. You also need a bearer token to authenticate yourself against the DRIVR API. You can find out how to create a bearer token in the "How to create a Connector token for your project" Guide.

There are several options available in DRIVR for creating datapoints. Currently, DRIVR has the following APIs for creating datapoints:

In this guide we will focus on using the HTTP API. This has the advantage that it does not require any additional libraries or dependencies. Additionally, it is possible to directly create multiple data points of different data types directly in one request.

To send HTTP requests we will use the fetch API, which is available by default as of node.js v18.0.0. Since this API still has experimental status within node.js, we still recommend polyfills such as node-fetch for production use.

First, we define a configuration that we will use later to send the data points. This configuration contains the credentials, as well as the UUIDs of the systems, components, and services we created in DRIVR. You can also use the timeStreamUuid or codes to select a datapoint, checkout the HTTP API documentation if you want to know more about these options.

const config = {
    token: '',
    endpoint: 'https://{slug}.api.drivr.cloud/rest/v1',
    systemUuid: '',
    componentUuid: '',
    serviceUuid: '',
    characteristicUuid: ''
};

Then we define a function that sends the data points to DRIVR. The data points are sent to DRIVR as a JSON array. Advanced users can alternatively use MessagePack.

async function sendDatapoint() {
    const res = await fetch(config.endpoint + '/data-points/add', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-type': 'application/json',
            'Authorization': `Bearer ${config.token}`
        },
        body: JSON.stringify([
            {
                system_uuid: config.systemUuid,
                service_uuid: config.serviceUuid,
                component_uuid: config.componentUuid,
                characteristic_uuid: config.characteristicUuid,
                value: Math.random() * 1000
            }
        ])
    });
    if (res.ok) {
        console.log('Datapoints sent successfully');
        return;
    }

    throw new Error(`Unable to execute send datapoints: DRIVR responded with ${response.status} ${response.statusText} - ${await response.text()}`);
}

To ensure that the data points are sent regularly, we can call the function at an interval of one second.

setInterval(() => {
    sendDatapoint().catch(console.error);
}, 1000);

After saving the script (e.g. send-datapoints.js) we can now run the script with node. The script will now send a data point every second.

node send-datapoints.js

If the data points have been sent successfully, the output should look something like this:

Datapoints sent successfully
Datapoints sent successfully
Datapoints sent successfully