Test and debug values in Kafka requests using JavaScript in Postman
You can add JavaScript code (scripts) to your Kafka requests, collections, and folders. The hooks available depend on the request’s mode:
- Producer: Before produce, which runs before the request sends its message, and After produce, which runs after the send completes.
- Consumer: Before consume, which runs before the request opens its subscription; On message, which runs once per message received from a subscribed topic (a bounded consume that receives 12 messages runs this hook 12 times); and After consume, which runs once the bounded consume completes (its stop condition is reached).
In addition to reading and writing variables (pm.environment, pm.variables) and general JavaScript, Kafka scripts can read and edit the request with pm.request, read each consumed message with pm.message, and read the produce or consume outcome with pm.response.
pm.request: read and edit the request before it runs
pm.request is available in Before produce and Before consume, and reflects the fields you can edit before the request runs.
In Before produce, you can read or set:
url- The broker address the message sends to.topic- The topic the message sends to.key- The message key.value- The message value (the payload).headers- The message headers.partition- The partition to send to. Leave unset and the broker chooses.timestamp- The message timestamp.acknowledgements- The broker acknowledgement level to wait for before the produce completes.compression- The compression codec applied to the message.
In Before consume, you can read or set:
url- The broker address to subscribe through.topics- The topic or topics to subscribe to.startPosition- Where in the topic the consume starts reading from, for example the beginning of the topic or a specific offset.stopAfter- The bounded consume’s stop condition (record count, duration, or idle timeout).
For example, to consume from the beginning of a topic regardless of what’s configured on the request:
key and value are decoded for you: a plain object if the message is JSON, or a Uint8Array if it’s raw bytes. Assign a string, object, or Uint8Array back and the runtime re-encodes it before sending.
To skip the current produce or consume entirely, without opening a connection, call pm.execution.skipRequest() from Before produce or Before consume.
pm.message: read the current message in On message
pm.message is available in On message, and holds the single record that triggered that run of the hook:
topic- The topic the record was received from.partition- The partition the record was received from.offset- The record’s offset within its partition.timestamp- The time the record was produced.key- The record’s key.value- The record’s value (the payload).headers- The record’s headers.
If a message is a tombstone, or otherwise has no key, pm.message.key reads as undefined.
pm.message here is Kafka’s own shape. If you’re looking at the general Postman Sandbox reference, it describes a simpler data/timestamp form used by other streaming protocols. For Kafka, use the fields listed on this page.
pm.response: read the outcome after the request completes
pm.response is available in After produce and After consume, and holds the result of the request:
In After produce:
- On success, the broker’s acknowledgement:
topic,partition,offset,timestamp. - On failure,
errorinstead, with the reason the broker rejected the message.
In After consume:
messages- An array of every record received during the bounded consume. Each entry has the same shape aspm.message, described earlier.reason- Why the consume stopped (for example, that it reached its configured record count, duration, or idle timeout).
As with pm.message, Kafka’s pm.response fields are specific to Kafka and differ from the generic shape on the Postman Sandbox reference page.
You can use scripts to debug your requests by logging to Postman Console, or dynamically read and update variables. This is what makes cross-protocol variable passing possible: an HTTP step earlier in a run can set a variable with pm.environment.set(...), and a Kafka Before consume or On message script can read it with pm.environment.get(...).
Bounded consume runs to its configured stop condition
A bounded consume always runs until its stopAfter condition (Stop After, on the request’s Consumer fields) is met. There is no way to end a consume from a script, and this isn’t planned as a near-term addition: it depends on a deeper change to the scripting sandbox that’s still in progress. If you want a consume to stop sooner, configure a tighter stopAfter bound (fewer records, a shorter duration, or a shorter idle timeout) on the request itself, rather than relying on script logic.
To skip a consumer request entirely, without opening a connection, call pm.execution.skipRequest() from the Before consume script.
The Postman Sandbox is available to you when you write your Kafka scripts. You can import packages with scripts and tests from your team’s Postman Package Library, and import packages from external package registries, such as npm and JSR.
To learn more about Postman test scripts, see Write scripts to test API response data in Postman.
Cross-protocol variable passing
Because a Kafka request can read and write the same variable scopes as an HTTP request, you can chain protocols in a single collection run:
- An HTTP request sets a variable (
pm.environment.set("orderId", pm.response.json().orderId)) in its own script. - A later Kafka consumer request’s Before consume or On message script can read that variable back with
pm.environment.get("orderId"), for example to log it to the Console. - A Kafka producer request further down the run can reuse the same variable in its message value or key using
{{orderId}}.
Assert on message content
Because pm.message exposes each record’s key, value, and headers directly, you can write assertions against a consumed message’s own content in On message, without passing it through a variable first:
You can write the same kind of assertion against the full set of received messages in After consume using pm.response.messages.