Skip to content

Kafka Connector Example

Given below is a sample scenario that demonstrates how to send messages to a Kafka broker via Kafka topics. The publishMessages operation allows you to publish messages to the Kafka brokers via Kafka topics.


You can try out this example using both 3.0.0 and 3.1.0 versions of the Kafka connector.

What you'll build

Given below is a sample API that illustrates how you can connect to a Kakfa broker with the init operation and then use the publishMessages operation to publish messages via the topic. It exposes Kakfa functionalities as a RESTful service. Users can invoke the API using HTTP/HTTPs with the required information.

API has the /publishMessages context. It publishes messages via the topic to the Kafka server.

The following diagram illustrates all the required functionality of the Kafka service that you are going to build.


If you do not want to configure this yourself, you can simply get the project and run it.

Set up Kafka

Before you begin, set up Kafka by following the instructions in Setting up Kafka.

Configure the connector in WSO2 Integration Studio

Follow these steps to set up the Integration Project and the Connector Exporter Project.


  1. Right-click the created Integration Project and select New -> Rest API to create the REST API.

  2. Specify the API name as KafkaTransport and API context as /publishMessages. You can go to the source view of the XML configuration file of the API and copy the following configuration (source view).

    <?xml version="1.0" encoding="UTF-8"?>
    <api context="/publishMessages" name="KafkaTransport" xmlns="">
        <resource methods="POST">
    Now we can export the imported connector and the API into a single CAR application. The CAR application needs to be deployed during server runtime.


Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP


Follow these steps to deploy the exported CApp in the Enterprise Integrator Runtime.



Create a topic:

Let’s create a topic named “test” with a single partition and only one replica. Navigate to the and run the following command:

bin/ --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test     


If your Kafka version is older than 2.2, then the --bootstrap-server option will not be supported. Use the --zookeeper options as shown below.

bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

Sample Request:

Send a message to the Kafka broker using a CURL command or sample client.

curl -X POST -d '{"name":"sample"}' "http://localhost:8290/publishMessages" -H "Content-Type:application/json" -v

Expected Response:

Navigate to the and run the following command to verify the messages:

bin/ --bootstrap-server localhost:9092 --topic test --from-beginning

See the following message content:


This demonstrates how the Kafka connector publishes messages to the Kafka brokers.

What's next