# Node RED to Datacake

In this tutorial we will show you how you bring your devices into the Datacake Portal using Node-RED.

Node-RED is a popular tool that can be installed on smaller Linux Computers and help you create things without the need to write code.

## Node-RED Nodes

Datacake does provide custom Node-RED extension:

{% embed url="<https://flows.nodered.org/node/node-red-contrib-datacake>" %}

### Installing the Extension

* Navigate into the "Manage Palette" setting on your Node-RED

![](/files/-MEvVBLUNXCoZD3ba9Ok)

* Select "Install" and enter "Datacake" as the search term
* If not yet installed press "Install" on the element in the List
* The Datacake Nodes will now automatically installed

### Configuring the Nodes

* To start with Datcake Node-RED Nodes simply drag one of the Nodes onto an empty flow
* Double-Click on the Node to open up Configuration:

![](/files/-MEvVmzUM9IJomrVnBsd)

#### Add Datacake Node Configuration

* Each Datacake Node can access a Device
* To access Devices the Node needs to communicate with a Workspace
* You do this by providing an Access Key
* Now click on the "Edit"-Icon next to "Add new Datacake-Configuration":

![](/files/-MEvXbEgFCCcU7tOUg2b)

* Now you have to enter an API Key which you need to create in your Workspace
* This API Key needs all rights so that you can access all devices
* Also it makes sense to give this API Key permission for all Devices in Workspace so that all current and future devices will be automatically added to the Node-RED Node.

Learn More about creating API Keys here:

{% content-ref url="/pages/-M4isfwhIv2qppzqVYI-" %}
[API Users](/portal/members/apiuser.md)
{% endcontent-ref %}

* The key should look like:

![](/files/-MEvY9QrXRAxLYrhjMqM)

#### Add Key to Node-RED Node

* Now you add the Key to your Node-RED configuration in your Node-RED

![](/files/-MEvYX8Pxi3Ox1OGs7Mc)

* Shortly after entering you can select the Workspace
* Select one Workspace and press "Add"
* This will bring you back to the configuration of the Node and here you can now select the Device + Field from which you want to receive data:

![](/files/-MEvYgHt7UDMEUTpRHC4)

### Sending Data

* If you want to Send Data from Node-RED into Datacake via Nodes simply drag the Output Node onto your flow and configure the Node like the input node:

![](/files/-MEvZ54DNiiOnmrMiEAT)

## Node-RED Function Code

Datacake does provide custom Node-RED Nodes that interact with Node-RED over the MQTT API. These are however more suitable for real-time use-cases as they report live measurement value changes.

If you are however looking for a way to record multiple measurements simultaneously you should go for using the GraphQL-API instead.&#x20;

This following example shows a script that can be embedded in a Node-RED function-Node which then needs to be hooked up to an HTTP-Request-Node.&#x20;

When a message hits the function-Node, the script performs a write operation with the Datacake API and records measurement values send through the function into a given Datacake Device.

### Script

```javascript
// Set device_id to serial-number of Datacake API Device
var device_id = "be525e29-4398-4fc1-a928-dead7fdfe218";

// Set Token to your personal access token or individual token
var token = "put your token in here";

// This is the API Information for the HTTP Request Node
msg.url = "https://api.datacake.co/v1/devices/"+device_id+"/record/?batch=true"

// Create Header for Node-RED HTTP Node
msg.headers = {
    "Authorization": "Token "+token,
    "Content-Type": "application/json"
};

// Now we are going to create the Payload we forward to Datacake API
msg.payload = [
    {
        "field": "WATT",
        "value": msg.payload.watt
    },
    {
        "field": "AMPERE",
        "value": msg.payload.ampere
    },
    {
        "field": "VOLT",
        "value": msg.payload.volt
    }
];

// Return it - were all set!
return msg;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datacake.de/api/node-red-to-datacake.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
