Connect to your cluster using the Kafka Go client


This QuickStart assumes that you have created a Pulsar cluster with the Kafka protocol enabled, created a service account, and granted the service account produce and consume permissions to a namespace for the target topic.

This document describes how to connect to your Pulsar cluster using the Kafka Go client through Token authentication.

Before you begin


  • Before getting the token of a service account, verify that the service account is authorized as a superuser or an admin of the tenants and namespaces.
  • A token has a system-defined Time-To-Live (TTL) of 7 days. Before a token expires, ensure that you generate a new token for your service account.
  • Get the JWT token.

    1. On the left navigation pane, click Service Accounts.

    2. In the row of the service account you want to use, in the Token column, click Generate new token, then click the Copy icon to copy the token to your clipboard.

  • Get the service URL of your Pulsar cluster.

    1. On the left navigation pane, in the Admin area, click Pulsar Clusters.
    2. Select the Details tab, and in the Access Points area, click Copy at the end of the row of the Kafka Service URL (TCP).


  1. Install the Kafka Go client.

    go get -u
  2. Build a Go application to produce and consume messages.

    package main
    import (
    func main() {
      // Step 1: replace with your configurations
      serverUrl := "SERVER-URL"
      jwtToken := "YOUR-TOKEN"
      topicName := "persistent://public/default/test-go-topic"
      namespace := "public/default"
      password := "token:" + jwtToken
      // Step 2: create a producer to send messages
      producer, err := kafka.NewProducer(&kafka.ConfigMap{
        "bootstrap.servers": serverUrl,
        "security.protocol": "SASL_SSL",
        "sasl.mechanism":    "PLAIN",
        "sasl.username":     namespace,
        "sasl.password":     password,
      if err != nil {
      defer producer.Close()
      err = producer.Produce(&kafka.Message{
        TopicPartition: kafka.TopicPartition{Topic: &topicName, Partition: kafka.PartitionAny},
        Value:          []byte("hello world"),
      }, nil)
      if err != nil {
      // wait for delivery report
      e := <-producer.Events()
      message := e.(*kafka.Message)
      if message.TopicPartition.Error != nil {
        fmt.Printf("failed to deliver message: %v\n",
      } else {
        fmt.Printf("delivered to topic %s [%d] at offset %v\n",
      // Step 3: create a consumer to read messages
      consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
        "bootstrap.servers":  serverUrl,
        "security.protocol":  "SASL_SSL",
        "sasl.mechanisms":    "PLAIN",
        "sasl.username":      namespace,
        "sasl.password":      password,
        "": 6000,
        "":           "my-group",
        "auto.offset.reset":  "earliest",
      if err != nil {
        panic(fmt.Sprintf("Failed to create consumer: %s", err))
      defer consumer.Close()
      topics := []string{topicName}
      err = consumer.SubscribeTopics(topics, nil)
      if err != nil {
        panic(fmt.Sprintf("Failed to subscribe topics: %s", err))
      // read one message then exit
      for {
        message, err = consumer.ReadMessage(1 * time.Second)
        if err == nil {
          fmt.Printf("consumed from topic %s [%d] at offset %v: %+v",
            message.TopicPartition.Partition, message.TopicPartition.Offset, string(message.Value))
    • serverUrl: the Kafka service URL of your Pulsar cluster.
    • jwtToken: the token of your service account.
  3. Run the Go application and you should see the following output:

    delivered to topic persistent://public/default/test-go-topic [0] at offset 29
    consumed from topic persistent://public/default/test-go-topic [0] at offset 15: hello world
Kafka Python