--- title: KafkaProducerAPI - Scoped description: The KafkaProducerAPI provides a method to produce messages from server-side scripts to an Apache Kafka topic.Instantiates a KafkaProducerAPI.Sends a message to a Kafka topic, optionally running a script include if an asynchronous send fails. locale: en-US canonical_url: https://www.servicenow.com/docs/r/australia/api-reference/server-api-reference/KafkaProducerAPIScoped.html release: australia product: Server API Reference classification: server-api-reference topic_type: concept last_updated: "2026-09-15" reading_time_minutes: 3 breadcrumb: [Server API reference, API reference, API implementation and reference] --- # KafkaProducerAPI- Scoped 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://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/integrate-applications/manage-topic-alias.md) 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://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/integrate-applications/stream-connect-apache-kafka.md). **Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md) ## KafkaProducerAPI - KafkaProducerAPI\(\) Instantiates a KafkaProducerAPI. |Name|Type|Description| |----|----|-----------| |None| || Creates an instance of KafkaProducerAPI. ``` var kafkaProducerAPI = new sn_ih_kafka.KafkaProducerAPI(); ``` ## KafkaProducerAPI - send\(Object params\) 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.
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
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. 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\]
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. 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
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); ```