# CoAP

If a device communicates over **CoAP** with akenza, a CoAP device connector has to be set up and assigned to a **Data Flow**.

After creation, a secret is generated which has to be provided in the request. Both the secret and the device ID are set as query params in the CoAP request.

{% hint style="info" %}
NOTE: The akenza CoAP connector currently only supports JSON content format. Make sure to provide the appropriate header for it.
{% endhint %}

## Send a CoAP Uplink

<mark style="color:green;">`POST`</mark> `coap://coap.akenza.io/v3/capture?secret={uplinkSecret}&deviceId={deviceId}`

The body can be any **JSON** object.

#### Query Parameters

| Name         | Type   | Description                                                                                     |
| ------------ | ------ | ----------------------------------------------------------------------------------------------- |
| timestamp    | string | The timestamp of the event (ISO-8601 formatted - the current time will be used if not provided) |
| topic        | string | The data topic ("default" will be used if not provided)                                         |
| uplinkSecret | string | The uplink secret used to authenticate the request                                              |
| deviceId     | string | The device ID                                                                                   |

#### Headers

| Name            | Type   | Description      |
| --------------- | ------ | ---------------- |
| content\_format | number | application/json |

{% tabs %}
{% tab title="201 Note that the actual response code is 2.01 for CoAP" %}

```
{
    "id": "UUID",
    "timestamp": "ISO-8601 date string",
    "message": "uplink received"
}
```

{% endtab %}
{% endtabs %}

### Sample nodeJS Script

The below sample nodeJS script allows sending a CoAP uplink. It requires the node module [coap](https://www.npmjs.com/package/coap) to be installed.

```typescript
const coap = require('coap');

// define connection options
const options = {
    hostname: "coap.akenza.io",
    port: 5683,
    method: "POST",
    pathname: "/v3/capture",
    query: "secret={secret}&deviceId={deviceId}",
    options: {
        "Content-Format": "application/json",
    },
};
// create the request object
const req = coap.request(options);
// set the payload
const payload = {
    temperature: Math.random() * 100,
};
req.write(JSON.stringify(payload));
// handle success response
req.on('response', function (res) {
    res.pipe(process.stdout);
});
// handle error response
req.on('error', function (err) {
    console.log("error while sending coap request", err);
});
// send the request
req.end();
```


---

# 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.akenza.io/akenza.io/get-started/your-data-flow/device-connector/coap.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.
