# Apache Kafka Solver API

![](https://437457296-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MN4F4-qybg8XDATvios%2Fuploads%2FZGWyq5JgsMCWgTes98UP%2Fapache_kafka-ar21.svg?alt=media\&token=41dd66cb-0701-48a0-89d7-3a13963c2171)

DecisionRules.io supports asynchronous calls to the Rule Solver. This option should be used if your infrastructure is based on asynchronous message processing.

## Setting up Apache Kafka Solver API

{% hint style="success" %}
Kafka Solver API is now available both in the cloud and on premise.
{% endhint %}

### Before you start using the Kafka Solver API

{% hint style="info" %}
In the cloud, it is necessary to enable Kafka Solver for a specific customer manually. If you are interested, please contact us at <sales@decisionrules.io>
{% endhint %}

1. Request access rights for your client application to the DecisionRules Kafka Cluster at <sales@decisionrules.io>
2. Configure the connection of your client application to the Kafka cluster.
3. Register on the assigned Kafka topic, where you will receive the results.
4. Sending input data to the input Kafka Topic, from which DecisionRules takes it.

### Information for connecting to Kafka Cluster

| Key                | Value                                           |
| ------------------ | ----------------------------------------------- |
| **broker**         | pkc-lq8v7.eu-central-1.aws.confluent.cloud:9092 |
| **ssl**            | true                                            |
| **sasl.mechanism** | plain                                           |
| **sasl.username**  | contact us on <sales@decisionrules.io>          |
| **sasl.password**  | contact us on <sales@decisionrules.io>          |

### Example Connection Config

Java Configuration

{% code title="config.properties" %}

```properties
# Required connection configs for Kafka producer, consumer, and admin
bootstrap.servers=pkc-lq8v7.eu-central-1.aws.confluent.cloud:9092
security.protocol=SASL_SSL
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule   required username='{{ CLUSTER_API_KEY }}'   password='{{ CLUSTER_API_SECRET }}';
sasl.mechanism=PLAIN
# Required for correctness in Apache Kafka clients prior to 2.6
client.dns.lookup=use_all_dns_ips

# Best practice for Kafka producer to prevent data loss
acks=all
```

{% endcode %}

#### Python Configuration

{% code title="config.py" %}

```python
# Kafka
bootstrap.servers=pkc-lq8v7.eu-central-1.aws.confluent.cloud:9092
security.protocol=SASL_SSL
sasl.mechanisms=PLAIN
sasl.username={{ CLUSTER_API_KEY }}
sasl.password={{ CLUSTER_API_SECRET }}
```

{% endcode %}

### How to communicate from a client application using Kafka Messsages

DecisionRules.io always uses two Kafka topics to communicate with the client application.

* **Response Topic (read-only)**
  * Topic on which your client application listens
  * You will receive the evaluated data in this topic
* **Request Topic (write)**
  * Kafka Topic into which you write data for evaluating DecisionRules
  * DecisionRules is listening on this topic

| Name           | Example Topic Name                  | Permissions |
| -------------- | ----------------------------------- | ----------- |
| Request Topic  | {company}-{random\_number}-request  | WRITE       |
| Response Topic | {company}-{random\_number}-response | READ        |

### Producing data to Request Topic

#### Message Headers

| Header                 | Description                                                                                                                         |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| api-key                | <p>(mandatory)</p><p>Solver API Key. Generate your api key in the <a href="https://app.decisionrules.io/api-keys">dashboard</a></p> |
| rule-id OR ruleflow-id | <p>(mandatory)</p><p>Rule or RuleFlow ID</p>                                                                                        |
| x-correlation-id       | <p>(optional)</p><p>User-generated ID to help you correlate input and output data.</p>                                              |
| version                | <p>(optional) </p><p>Version of the rule you want to solve.</p>                                                                     |

#### Message Data

| Message Attribute | Description                                                               |
| ----------------- | ------------------------------------------------------------------------- |
| Key               | \<empty>                                                                  |
| Value             | JSON object filled according to the data model called by Rule or RuleFlow |

#### Example Request Data

{% code title="Message Value Example (JSON Object)" %}

```json
{
  "client": {
    "age": 12
  }
}
```

{% endcode %}

#### Message Data Schema

{% code title="Request Schema" %}

```json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {},
  "required": []
}
```

{% endcode %}

### Consuming Data from Response Topic

| Attribute     | Type   | Description                                                                                                                                                                                                                                                                  |
| ------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| data          | Array  | Field filled with data according to the rule evaluation. One element in the array means one result. For example, if multiple rows are matched when evaluating a decision table, they are returned as array items                                                             |
| errors        | Array  | <p>If an error occurs in the evaluation for any reason, each error is added to the error field.</p><p>If there are no errors in the evaluation, the "errors" attribute is not returned.</p><p>The contents of the "data" field will be empty when any error is returned.</p> |
| correlationId | String | <p>(optional) If the "x-correlation-id" header was filled in the input message, this value is available in the output message.</p><p>If "x-correlation-id" is not filled in the input, the attribute is not present in the output.</p>                                       |

#### Example Response Data

{% code title="Single data" %}

```json
{
  "data": [
    {
      "totalProducts": 2,
      "amountPerProduct": 500,
      "segment": "mass-market",
      "profitability": 1.2
    }
  ],
  "correlationId": "ef429e33-f2c4-41ad-b9f3-cad12ea3b95e"
}
```

{% endcode %}

{% code title="Multiple data" %}

```json
{
  "data": [
    {
      "totalProducts": 2,
      "amountPerProduct": 500,
      "segment": "mass-market",
      "profitability": 1.2
    },
    {
      "totalProducts": 1,
      "amountPerProduct": 300,
      "segment": "mass-market",
      "profitability": 1.53
    }
  ],
  "correlationId": "12429e33-f2c4-41ad-b9f3-cad12ea3b95e"
}
```

{% endcode %}

{% code title="Error response" %}

```json
{
  "errors": [
    {
      "code": 500,
      "message": "This Rule Flow belongs to another user OR Rule Flow not found"
    }
  ],
  "data": [],
  "correlationId": "ef429e33-f2c4-41ad-b9f3-cad12ea3b95e"
}
```

{% endcode %}

#### Message Data Schema

```json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": [
        {
          "type": "object"
        }
      ]
    },
    "correlationId": {
      "type": "string"
    },
    "errors": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "code": {
              "type": "integer"
            },
            "message": {
              "type": "string"
            }
          },
          "required": [
            "code",
            "message"
          ]
        }
      ]
    }
  },
  "required": []
}
```


---

# 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.decisionrules.io/doc/api/apache-kafka-solver-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.
