Links

Stripe to Kafka

Send messages from Stripe to Kafka via the Buildable Node SDK

Overview

In this example, we’re going to use the Buildable SDK to send data from Stripe to Kafka in 2 easy steps:
  1. 1.
    Create a Stripe connection in Buildable
  2. 2.
    Send events to a Kafka server
For this example, you'll need a few things:
  • A Buildable account and Buildable Secret Key
  • A Stripe account and Secret Key
  • A Kafka server

Step 1: Create a Stripe Connection

To grab events from Stripe, simply create a Stripe connection in Buildable:
  1. 1.
    Click on the Connections tab
  2. 2.
    Click + New
  3. 3.
    Select the Stripe option
  4. 4.
    Securely add your Stripe credentials
  5. 5.
    Click Connect to establish the connection
  6. 6.
    Select the customer.created, customer.deleted and customer.updated events
  7. 7.
    Click Subscribe
O

Step 2: Send events to Kafka

Connect to Kafka

To connect to your Kafka server, take a look at the Official Getting Started Documentation.
const { Kafka } = require("kafkajs");
// Connect to Kafka
const kafka = new Kafka({
clientId: process.env.KAFKA_CLIENT_ID,
brokers: [process.env.KAFKA_BROKER],
ssl: true,
sasl: {
mechanism: "plain",
username: process.env.KAFKA_USERNAME,
password: process.env.KAFKA_PASSWORD,
},
});
// Create the producer
const producer = kafka.producer();
// Connect to the producer
await producer.connect();

Listen to Messages from Stripe

Install the Node SDK
> npm install @buildable/messages
Create an Environment Variable
Add your Buildable Secret Key values in a .env file
Create your Listener
Grab the customer.created, customer.deleted and customer.updated message from Buildable
// Select events to listen on
const events = [
"customer.created",
"customer.deleted",
"customer.updated"
];
const listenerConfig = {
platform: "stripe",
label: "my-stripe",
txKey: "kafka.message.sent",
};
async function main() {
// Listen to all events
events.forEach((eventName) =>
client.on(eventName, async ({ payload, event }) => {
// Create Kafka message...
}, listenerConfig)
);
}
main().catch(console.error);

Send data to Kafka

To create a Kafka message, we will use the producer.send method:
await producer.send({
topic: "integrations",
messages: [
{
key: "unique-key",
value: JSON.stringify({
event: "customer.created",
payload: {},
}),
},
],
})

You're done! Here's the final Code

// Init the SDK
const { Kafka } = require("kafkajs");
const { createClient } = require("@buildable/messages");
const client = createClient(process.env.BUILDABLE_SECRET_KEY);
// Create Kafka Client
const kafka = new Kafka({
clientId: process.env.KAFKA_CLIENT_ID,
brokers: [process.env.KAFKA_BROKER],
ssl: true,
sasl: {
mechanism: "plain",
username: process.env.KAFKA_USERNAME,
password: process.env.KAFKA_PASSWORD,
},
});
// Select the Kafka topic
const topic = "integrations";
// Select the events to listen on
const events = [
"customer.created",
"customer.deleted",
"customer.updated"
];
const listenerConfig = {
platform: "stripe",
label: "my-stripe-connection",
txKey: "kafka.message.sent",
};
async function main() {
// Create and connect to Kafka producer
const producer = kafka.producer();
await producer.connect();
// Listen to all events
events.forEach((eventName) =>
client.on(eventName, async ({ payload, event }) => {
// Create Kafka message
return await producer.send({
topic,
messages: [
{
key: event.key,
value: JSON.stringify({
event: `${listenerConfig.platform}.${listenerConfig.label}.${eventName}`,
payload,
}),
},
],
})
}, listenerConfig)
);
}
main().catch(console.error);