---
sourceDocument: Brazil API Reference
sourceDocumentLink: https://www.servicenow.com/docs/r/api-reference

 Release :

    - brazil

ft:locale :

    - en-US

ft:publication_title :

    - Brazil API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# KafkaProducerAPI - Scoped

# KafkaProducerAPI - Scoped {#ariaid-title1}

Release version: Brazil  
Updated September 15, 2026  
![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read  
The KafkaProducerAPI provides a method to produce messages from server-side scripts to an Apache Kafka topic.

To access this API, the ServiceNow IntegrationHub Action Step - Kafka Producer plugin (`com.glide.hub.action_step.kafka`) must be activated. This API runs in the `sn_ih_kafka` namespace.

Use the send() method to deliver a message to a Kafka topic. Before calling send(), a [topic alias](https://www.servicenow.com/docs/access?context=manage-topic-alias&version=brazil&pubname=brazil-integrate-applications&ft:locale=en-US) record for the target Kafka topic must exist in the Topic Alias \[sys_sc_topic_alias\] table.

Permission for sending messages to a given topic is enforced by access control on the topic alias record.

For more information about working with Kafka topics, see [Using Stream Connect for Apache Kafka](https://www.servicenow.com/docs/access?context=stream-connect-apache-kafka&version=brazil&pubname=brazil-integrate-applications&ft:locale=en-US).

## KafkaProducerAPI - KafkaProducerAPI() {#ariaid-title2}

Instantiates a KafkaProducerAPI.
{#KafkaProducerAPI-KafkaProducerAPI__table_mnd_k31_pkc__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#KafkaProducerAPI-KafkaProducerAPI__table_mnd_k31_pkc}  
Creates an instance of KafkaProducerAPI.

    var kafkaProducerAPI = new sn_ih_kafka.KafkaProducerAPI();

## KafkaProducerAPI - send(Object params) {#ariaid-title3}

Sends a message to a Kafka topic, optionally running a script include if an asynchronous send fails.
Before calling this method for asynchronous message delivery, create a script include to handle delivery failures. When the script include executes, an array of error objects (callbackErrors) is passed to
the callback function. Each error object represents one message delivery failure.  
{#KafkaProducerAPI-send_O__table_send_errorobj__entry__3}

| Name | Type | Description |
|-|-|-|
| context | String | The value passed as params.context in the original send() call for the failed message. Data type: String |
| errorMessage | String | Error message describing the reason for failure. Data type: String |
| errorType | String | The fully qualified class name for the exception. Data type: String |
| producerId | String | Sys_id of the producer event associated with the failed send. Table: Kafka Producers \[sys_kafka_producer\] Data type: String |
[Table 2. Error object properties]

{#KafkaProducerAPI-send_O__table_send_errorobj}
{#KafkaProducerAPI-send_O__table_send_params__entry__3}

| Name | Type | Description |
|-|-|-|
| params | Object | Object containing parameters to use for sending the message. { context: "String", headers: {Object}, isSync: Boolean, key: "String", message: "String", onErrorScriptIncludeId: "String", topicSysId: "String" } |
| params.context | String | Optional. Caller-supplied context string to pass to the script include if an asynchronous send fails. Used to identify the message that triggered the failure. |
| params.headers | Object | Optional. Key-value pairs for message headers. Any headers supported by Apache Kafka are valid. For example: { "source": "order_update_br", "correlation_id": "8a1c3f2b1b3a10" } |
| params.isSync | Boolean | Optional. Flag that indicates whether to send the message synchronously. Possible values: * true: Send synchronously and block for the result. * false: Send asynchronously. An error callback can be specified in onErrorScriptIncludeId to execute when an asynchronous send fails. {#KafkaProducerAPI-send_O__ul_rcs_mjp_pkc} Default: false |
| params.key | String | Optional. Message key for partitioning in Apache Kafka. Messages with the same key are sent to the same partition, ensuring ordering. Default: Empty string |
| params.message | String | Content of the message to send to the topic. |
| params.onErrorScriptIncludeId | String | Optional. Sys_id of the script include to run if an asynchronous send fails. Default: None Table: Script Includes \[sys_script_include\] |
| params.schemaSysId | String | Optional. Sys_id of a registered schema record to use for encoding the message. Default: The message is sent as plain text. Table: Stream Connect Schemas \[stream_connect_schema\] |
| params.topicSysId | String | Sys_id of the target Kafka topic alias record. Table: Topic Alias \[sys_sc_topic_alias\] |
[Table 3. Parameters]

{#KafkaProducerAPI-send_O__table_send_params} {#KafkaProducerAPI-send_O__table_send_returns__entry__2}

| Type | Description |
|-|-|
| Object | Map containing delivery result information. For synchronous delivery, the method returns after the message is delivered. For asynchronous delivery, the method returns immediately. The delivery may later succeed or fail. Asynchronous delivery failure can be handled using the provided onErrorScriptIncludeId. { delivery: "String", offset: Number, partition: Number } |
| \<Object\>.delivery | Delivery type. Possible values: * `async`: The message will be sent asynchronously. * `sync`: The message was sent synchronously. {#KafkaProducerAPI-send_O__ul_pzt_xjp_pkc} Data type: String |
| \<Object\>.offset | Unique ID assigned to the message within the topic partition. Only returned for `sync` delivery. Data type: Number |
| \<Object\>.partition | Partition that the message was delivered to. Only returned for `sync` delivery. Data type: Number |
[Table 4. Returns]

{#KafkaProducerAPI-send_O__table_send_returns}  
This business rule sends an order-updated event asynchronously, routing any delivery failure to the example `KafkaSendFailureHandler` script include. Note that `KafkaSendFailureHandler` is shown
only as an example; you must create your own script include to handle delivery failures.

    var kafkaProducerAPI = new sn_ih_kafka.KafkaProducerAPI();

    var response = kafkaProducerAPI.send({
        topicSysId: '8a1c3f2b1b3a1010e7cfc51b1ce0bc42', // sys_id for 'orders.updated'
        key: current.getUniqueValue(),
        message: JSON.stringify({
            order_id: current.getUniqueValue(),
            status: current.getValue('status')
        }),
        isSync: false,
        headers: { source: 'order_update_br' },
        onErrorScriptIncludeId: 'b2e4a1c3f2b1a1010e7cfc51b1ce0c99', // sys_id of KafkaSendFailureHandler
        context: current.getUniqueValue()
    });

    gs.info('Kafka send submitted, delivery mode: ' + response.delivery);
    // Output:
    // Kafka send submitted, delivery mode: async

This script include reads callbackErrors and logs each delivery failure.

    // Script include: KafkaSendFailureHandler
    (function callback(callbackErrors) {

        gs.info("callback size: ", callbackErrors.length);

        for (var i = 0; i < callbackErrors.length; i++) {
            gs.info("callback " + callbackErrors[i].errorMessage + " " +
                callbackErrors[i].errorType + " " +
                JSON.stringify(callbackErrors[i].context) + " " +
                callbackErrors[i].producerId);
        }

    })(callbackErrors);


