> For the complete documentation index, see [llms.txt](https://docs.datacake.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datacake.de/guides/python.md).

# Python

How to connect a Python Script to Datacake

## HTTP Request

### Example Script

```python
import requests

# The Webhook URL for your API Product on Datacake
url = "https://api.datacake.co/integrations/api/85dd4a6f-f587-4e5d-a522-c871caaba059/"

# Serial for device identification on Datacake
serial = "pythondevice001"

# Some random sensor data (replace with real data)
temperature = 16.04
humidity = 53.23
pressure = 1015
light = 734
power_status = True
lat = 51.4356
lon = 6.953

# Create a dictionary containing payload for webhook call
payload = {
    "s": serial,
    "t": temperature,
    "h": humidity,
    "p": pressure,
    "l": light,
    "ps": power_status,
    "loc": "(" + str(lat) + "," + str(lon) + ")"
}

# Do a post request on the webhook including payload data
r = requests.post(url, json=payload)
```

### Decoder on Datacake

Below you will find the corresponding HTTP Payload decoder for the above Python Script that perform as HTTP Post Request.

```javascript
function Decoder(request) {

    // Parse JSON string in request body to real json
    var payload = JSON.parse(request.body);

    // Extract serial number in payload for device routing
    var serial = payload.s;
    
    // Return an array of dictionaries to Datacake
    // Each dictionary contains device serial, database identifier and value
    return [
        {
            device: serial, // Serial number of device
            field: "TEMPERATURE", // Identifier of database field
            value: payload.t // Actual data value for field
        },
        {
            device: serial,
            field: "HUMIDITY",
            value: payload.h
        },
        {
            device: serial,
            field: "PRESSURE",
            value: payload.p
        },
        {
            device: serial,
            field: "LIGHT",
            value: payload.l
        },
        {
            device: serial,
            field: "POWER_STATUS",
            value: payload.ps
        },
        {
            device: serial,
            field: "LOCATION",
            value: payload.loc
        },
    ];
}
```

### Routing via Serial Number

#### Python

```python
# Serial for device identification on Datacake
serial = "pythondevice001"
```

#### Datacake Decoder

```javascript
// Extract serial number in payload for device routing
var serial = payload.s; // "pythondevice001"

// Return an array of dictionaries to Datacake
// Each dictionary contains device serial, database identifier and value
return [
    {
        device: serial, // Serial number of device "pythondevice001"
        field: ...
        value: ...
    },
    ...
]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.datacake.de/guides/python.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
