# Lowcode to Kafka

{% hint style="info" %}
The availability of the functionality depends on the license and may not be available in all deployments.
{% endhint %}

## Purpose

Enables sending messages to a specified Kafka instance and a specified topic directly from requests (scripts) and BPMN processes (script tasks).

## Usage examples

* Updating data in the data warehouse
* Notifying CRM about process status changes
* Communication with external systems (e.g. the bank’s systems)

## Use in a script/script task

> Note that the Kafka instance name ("kafkaid" in the example) must be written in lowercase and without special characters.

```js
try {
    api.events.v1.publishAsync(
        "kafkaid",                  // Kafka instance name (configured by the administrator, written only in lowercase, without special characters)
        "topicName",                // topic name (configured by the administrator)    
        {                           // message content – must be valid JSON
            "id": "123",
            "status": {
                "code": "OK",
                "message": "Everything is fine!"
            },
            "list": [1, 2, 3],
            "boolean": true,
            "date": new Date()
        },
        "key",                      // message key (optional – it should be set if we need to preserve event order)
        {                           // headers (optional)
            "header1": "header1-value",
            "header2": "header2-value"
        }
    );
} catch (e) {
    // error handling
}
```

## Important information

* Sending is asynchronous – the script does not wait for confirmation that the message was delivered
* Remember to handle exceptions. The function may throw an error if there is no connection to Kafka or if the message buffer to be sent in the application is full
* Successful execution of the function confirms that the message was added to the buffer; it does not guarantee error-free sending of the message. We are unable to catch errors in the sending itself
* The message content must be valid JSON – the passed object is automatically serialized
* The key is optional, but recommended when you care about message order within a single partition
* The send timeout is configured by the administrator – it cannot be changed from the script level
* Topics are created and managed by administrators
* `kafkaId` must match the name of the Kafka instance configured in the application
