Skip to main content

KafkaJS client API Documentation

Package: @alt-stack/kafka-client-kafkajs

This is the generated-schema producer binding for KafkaJS. It validates outgoing data against a runtime Topics map; it does not use a KafkaRouter and cannot consume records.

createKafkaClient

async function createKafkaClient<T extends TopicsMap>(
options: KafkaJSClientOptions<T>,
): Promise<KafkaClient<T, Producer>>

The function obtains a KafkaJS producer, applies producerConfig, connects immediately, and returns a typed client. It accepts either an existing KafkaJS Kafka instance or a KafkaConfig object.

import { createKafkaClient } from "@alt-stack/kafka-client-kafkajs";
import { Topics } from "./generated-events.js";

const client = await createKafkaClient({
kafka: {
clientId: "billing-publisher",
brokers: ["localhost:9092"],
},
topics: Topics,
});

await client.send("invoice-created", { invoiceId: "inv_123" });
await client.disconnect();

KafkaJSClientOptions<T>

PropertyTypeRequiredBehavior
kafkaKafka | KafkaConfigyesexisting KafkaJS instance or constructor config
topicsT extends TopicsMapyesexact topic schemas used for types and runtime safeParse
producerConfigProducerConfignoforwarded to kafka.producer(...)
onError(error: Error) => voidnocalled before a connection, validation, or send error is thrown

Send behavior

send and sendBatch JSON-stringify payloads and forward SendOptions to KafkaJS records. Validation failures throw ValidationError; producer failures throw SendError. A failed initial connection throws ConnectionError. producer exposes the underlying KafkaJS Producer, and disconnect() delegates to it.

Re-exports

The package re-exports the KafkaClient, TopicsMap, and SendOptions types plus KafkaClientError, ValidationError, SendError, and ConnectionError. Their exact shapes are in Kafka client core.