# Record Measurements via API

This is a quick tutorial that will show you how you can use the Datacake GraphQL-API to record measurement values into your devices.

There is a large overview of Endpoints and how they function including some examples on an external developer overview.

## Record Multiple Values at once

### Payload Structure

Make sure that you structure the payload like in the following snippet.

```javascript
[
    {"field": "WATT", "value": msg.payload.watt },
    {"field": "AMPERE", "value": msg.payload.ampere},
    {"field": "VOLT", "value": msg.payload.volt}
];
```

So the general structure is:

```javascript
[{"field":"field1", "value":123.00}, {"field":"field2", "value":"a string"}]
```

### URL

The URL you should use for recording multiple measurements at once is:

`https://api.datacake.co/v1/devices/22h7c45f-2917-358-9db1-d8a544ab78ed/record/?batch=true`

It is structured as follow:

`https://api.datacake.co/v1/devices/<<device-id>>/record/?batch=true`

#### The Device-Id (<\<device-id>>)

This is the ID of your Device. You find this in the URL of the Datacake Portal and on the Device-View.

![Device-ID](/files/-M5vlmHbDosWkr7uP5Zf)

### Rate Limiting

Per default the Datacake GraphQL-API has an internal rate limiting of 1 write per second and per field. If you want to record historical data this might be an issue.

## Examples

Here you find some Examples that show you how to record multiple measurements.

### Curl

```bash
curl https://api.datacake.co/v1/devices/22h7c45f-2917-358-9db1-d8a544ab78ed/record/?batch=true
  -X POST
  -H "Authorization: Token 923847692384769283469823467"
  -H "Content-Type: application/json"
  --data '[\
    {\
        "field": "TEMPERATURE",\
        "value": 23.5,\
        "timestamp": "1555570137"\
    }\,
    {\
        "field": "HUMIDITY",\
        "value": 42,\
        "timestamp": "1555570137"\
    }\
  ]'
```

### Javascript

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

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

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

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

// Now we are going to create the Payload we forward to Datacake API
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;
```

### Python

```python
import requests
​
DTCK_KEY = 'yourapikey'
DTCK_DEVICE_ID = 'yourdeviceid'

if __name__ == "__main__":

    power_dc1 = 234
    power_dc2 = 345

    requests.post(f"https://api.datacake.co/v1/devices/{DTCK_DEVICE_ID}/record/?batch=true", headers={"Authorization": f"Token {DTCK_KEY}"}, json=[
        {
            "field": "POWER_DC1",
            "value": power_dc1,
            "timestamp": "1555570137" # a timestamp is optional
        },
        {
            "field": "POWER_DC2",
            "value": power_dc2
        }
    ])
```


---

# 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/record-measurements-via-api.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.
