---
title: "Use Auth0 Actions to Send User Info to Apache Kafka When They Register"
description: "Use Auth0 Actions to send authentication events to Apache Kafka."
authors:
  - name: "Enes Akar"
    url: "https://auth0.com/blog/authors/enes-akar/"
date: "Feb 3, 2022"
category: "Developers,Tutorial,Actions"
tags: ["actions", "kafka", "restapi"]
url: "https://auth0.com/blog/send-auth0-events-to-kafka-for-realtime-processing/"
---

# Use Auth0 Actions to Send User Info to Apache Kafka When They Register

In this post, you’ll learn how to integrate Auth0 with [Apache Kafka](https://kafka.apache.org/) using Actions. You’ll do this by writing an Action that pushes information from new user registrations to a Kafka cluster. By doing this, you open a number of analytics and user experience possibilities, including:

*   Processing sign-up events (with [Apache Flink](https://flink.apache.org/) or [Spark](https://spark.apache.org/)) to notify your business and sales team when a new user signs up for an account.
*   Trigger processes such as sending a welcome email or Slack message in response to a user signing up.
*   Using a [Kafka connector](https://docs.snowflake.com/en/user-guide/kafka-connector-overview.html) to move the user data gathered from the sign-up process to a database or data warehouse to feed reporting and analytics applications.
*   Feeding new user data to your CRM application.

![Kafka](https://images.ctfassets.net/23aumh6u8s0i/47eyPrCR712YD4KWSRvG80/8150b144cf5cbb5747096d3f3af8bea4/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

## Why Apache Kafka?

Apache Kafka is a distributed messaging platform that receives data as events in real-time from sources such as sensors, mobile devices, databases, applications, and services (including Auth0) and makes that data available to target systems. It supports both publish/subscribe and distributed queue scenarios. Kafka gives you the flexibility to perform both stream processing and batch processing with your data. Kafka’s replication feature and durability allow you to use it as a persistent hub for your data.

## Auth0 Actions

Auth0 Actions are serverless Node.js functions that are triggered by certain events that take place when signing up for an Auth0 user account or authenticating with Auth0. Using Actions, you can customize and extend Auth0’s capabilities with your custom logic that you can add to events such as login, registration, changing passwords, and more. Auth0 Actions are stateless, so you’ll need an external store if you use them to collect data. Check [this article](https://auth0.com/docs/customize/actions/actions-overview) to learn more about Actions.

In this post, we will write an Auth0 Action which will be triggered by the Post User Registration flow, which happens after a user has completed signing up for an account.

## Create a Kafka Cluster

Because Auth0 Actions are stateless, we need a Kafka service that does not require stateful connections. We will use [Upstash’s Kafka service](https://upstash.com/kafka), which has a REST API and is designed for serverless environments and a free plan suitable for this project.

If you don’t already have an Upstash account, [sign up for one](https://console.upstash.com/login). The process is very quick, especially if you sign up using an Amazon, GitHub, or Google account. You’ll be taken to the [Upstash console](https://console.upstash.com/) immediately afterward:

![Cluster](https://images.ctfassets.net/23aumh6u8s0i/7i8OxJQwxFJZYOAKZGvS1f/d89275afb1698cc4f7c498eb100fb12d/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Select **Kafka** in the menu bar at the top of the page. You’ll be taken here:

![Cluster](https://images.ctfassets.net/23aumh6u8s0i/4shEb8jySPGei6xBcZ0DPK/6e056b1ae7fa8e0a9a60f7f67d047506/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

In order to use Kafka, you need a cluster, which is a set of one or more servers running Apache Kafka. Click the **Create Cluster** button. This form will appear:

![Cluster](https://images.ctfassets.net/23aumh6u8s0i/6LZJRtkgqpHLUPxpdoesuj/08d7a77dc2da2ef54e57f3dea316eaf7/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Provide the following information for each of its fields:

*   **Name:** Pick a name that will make it easy to recognize, such as “Auth0 Actions Test.”
*   **Region:** Select the region closest to you.
*   **Type:** Select **Single Zone**

Click the **Create** button to continue. You’ll see the form’s second page:

![Create](https://images.ctfassets.net/23aumh6u8s0i/130Z4nzMY3elKtmElpZz4o/d25551d11ae2615697e6b724199cac47/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

You need to provide a name for a _topic,_ which is how Kafka organizes events. If you think of events as being like files, topics are like directories. Enter the topic name **user-events-topic** into the **Name** field and click the **Send Request** button.

Upstash will create the cluster and immediately take you to its **Details** page:

![Details](https://images.ctfassets.net/23aumh6u8s0i/1iU7Q3GmamqgUbJQGVOOeF/fbf5724310a40936c0937a0e3d4baeca/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Scroll down to the **REST API** section, where you’ll see controls to copy the values for `UPSTASH_KAFKA_REST_URL`, `UPSTASH_KAFKA_REST_USERNAME`, and `UPSTASH_KAFKA_REST_PASSWORD`, which you will use in the next step.

![REST API](https://images.ctfassets.net/23aumh6u8s0i/3FEKvlN77iwUh0V8RBQ6aC/265f34f90d4d38ad09f64b5662a7dc79/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Keep this page open in a browser tab or window; you’ll need it shortly.

## Implement the Action

You will need an Auth0 account for this step. If you don’t already have an account, [you can sign up for one for free.](https://auth0.com/signup)

### Create a new action

Log into the Auth0 dashboard. On the left menu, select **Actions** → **Library**, then click the **Build Custom** button. This will open the **Create Action** pop-up:

![reate Action](https://images.ctfassets.net/23aumh6u8s0i/541DakfFmVUUMnBG9vHwr9/f60d4fe3cd82639f39f835a32b9c4a51/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Provide a name for the Action (I used **sendKafka**).  

Since the goal is to send a message to Kafka after a user has signed up for an account, the action should be triggered by the Post User Registration event. Select **Post User Registration** from the **Trigger** menu, then click the **Create** button.

You should now see the code editor:

![reate Action](https://images.ctfassets.net/23aumh6u8s0i/4QTUZab91hhnUek6nbydtm/789ab00bcbde339441a01d02ec0018c5/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

### Define the Action’s secrets

To send messages to the Kafka cluster, the Action needs the `UPSTASH_KAFKA_REST_URL`, `UPSTASH_KAFKA_REST_USERNAME` and `UPSTASH_KAFKA_REST_PASSWORD` values for your Kafka cluster, which you can copy from the Upstash console. You could simply use these values to define constants, but it’s more secure to keep them hidden. You can do this with Actions’ **_Secrets_** feature, which lets you securely store sensitive values and access them as properties of the `event.secrets` object.

Let’s start with the first value, `UPSTASH_KAFKA_REST_USERNAME`. Switch to the browser tab or window with the Upstash console and click on the icon beside `UPSTASH_KAFKA_REST_USERNAME` to copy its value. Then switch back to the Action code editor and click the “Secrets” icon:

![Secrets](https://images.ctfassets.net/23aumh6u8s0i/25t3LnePJf5WZEZlXWUNWk/d8eb298b643cc246f4edd5e9f16e8dc3/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

A new column with the title **Secrets** will appear in the code editor:

![Secrets](https://images.ctfassets.net/23aumh6u8s0i/5Mpielp1PpUBKdggFN95Ez/ff08915ea004c8a7eb894aa92bc1faec/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Click the **Add Secret** button. A pop-up will appear, and you will be able to define your first secret value:

![Add Secret](https://images.ctfassets.net/23aumh6u8s0i/6dxqg239qUPjaKFocZeGcT/c0acea0519808c411846200a23fee411/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Enter `UPSTASH_KAFKA_REST_URL` into the **Key** field, paste the value you copied from the Upstash console into the **Value** field, and click the **Create** button. This will create a property named `event.secrets.UPSTASH_KAFKA_REST_URL`, which you will be able to use in your Action.

Click the **Add Secret** button again. This time, define the `UPSTASH_KAFKA_REST_USERNAME` secret by copying its value from Upstash and filling out the **Define Secret** form as shown below:

![Add Secret](https://images.ctfassets.net/23aumh6u8s0i/g1QnSJwEekRL6u2lbcWoa/12a993bf7846febff07fbe3a4962f3e4/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Click the **Create** button, then click the **Add Secret** button one more time. Define the `UPSTASH_KAFKA_REST_PASSWORD` secret by copying its value from Upstash and filling out the **Define Secret** form as shown below…

![Define Secret](https://images.ctfassets.net/23aumh6u8s0i/35dYyYGnD9CrFlUNPhigqB/afd68d74bc539e86f607af23a956dab9/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

…then click the **Create** button. The **Secrets** column should now contain a list of three secrets:

![Create](https://images.ctfassets.net/23aumh6u8s0i/1wkGPTskvMIU8BHQUXdlTn/6090399c09c0cecebf448949444b5fa7/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

### Add the node-fetch module to the Action

You can import any npm module into an Action, which makes them very powerful. This action uses the [node-fetch](https://www.npmjs.com/package/node-fetch) module to send events to the Kafka cluster, so you’ll need to add it as a dependency. You can do this with the **_Modules_** feature.

Click the “Modules” icon, which is just below the “Secrets” icon:

![Modules](https://images.ctfassets.net/23aumh6u8s0i/1FIkQiyIwiEnMuLceyRqWm/229bfc23e1b8b9fb8c1f804fbfa672a4/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

A new column with the title **Modules** will appear in the code editor:

![Modules](https://images.ctfassets.net/23aumh6u8s0i/6D6vSKdIZwkhIk26zhpMfv/f6c18c676a69f6c519ba9b30166034e9/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Click the **Add Module** button. A pop-up will appear:

![Add Module](https://images.ctfassets.net/23aumh6u8s0i/7KbTmaZmZNNUDiMnqgvPF9/c1fa1efdc6285f5f51405f8a924df68c/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Enter **node-fetch** into the **Name** field and **2** into the **Version** field. Do _not_ select the latest version (3 at the time of writing), as it does not allow `require()` to import it.

Click the **Create** button. The **Modules** column should now list the _node-fetch_ module:

![Create](https://images.ctfassets.net/23aumh6u8s0i/3Uvbaeb6xNhGQWcE2T8Ru4/2dd6b53db2bf15ab2f23e020416e366c/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

### Write the Action

Now that you have provided the prerequisites, it’s time to write the Action!

Paste the code below into the editor:

```
const fetch = require('node-fetch');
 
exports.onExecutePostUserRegistration = async (event) => {
  const address = event.secrets.UPSTASH_KAFKA_REST_URL
  const user = event.secrets.UPSTASH_KAFKA_REST_USERNAME
  const pass = event.secrets.UPSTASH_KAFKA_REST_PASSWORD
  const auth = Buffer.from(`${user}:${pass}`).toString('base64')
  const topic = 'user-events-topic'

  const userData = JSON.stringify(event.user);
  const x = await fetch(`${address}/produce/${topic}`, {
    method: 'POST',
    headers: {'Authorization': `Basic ${auth}`},
    body: JSON.stringify({"value" : userData})
  })
  const response = await x.json();
  console.log(response)
};
```

This Action uses the `onExecutePostUserRegistration()` function provided by Auth0. The function is called when the user has finished signing up for an account. It takes a single parameter, `event`, whose `user` property is an object containing information about the newly-registered user, such as their email address and user ID.

This code retrieves the secret values stored in the Action’s Secrets and uses them to connect to the Kafka cluster. It takes the `user` object, converts it into a raw JSON string, and sends it to the topic named `user-events-topic` in the Kafka cluster. The user data to the Kafka topic using the Upstash REST API.

### Test the Action

Now, we can run the function by clicking on the **Test** icon on the editor’s sidebar:

![Test](https://images.ctfassets.net/23aumh6u8s0i/6UPSQybrGeZRu0kj6qzpsQ/13cf0a64078bd41880c5352ef585dc04/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

A new column with the title **Test** will appear in the code editor:

![Test](https://images.ctfassets.net/23aumh6u8s0i/6iPKb04fleyOW1kndgekn5/8a43590a9482e388757f5cecf628b227/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Click the **Run** button to run the test. A **Test Results** pane will appear in the lower section of the code editor:

![Run](https://images.ctfassets.net/23aumh6u8s0i/3NNpjQElJ4qrQQAGPSZ5mj/750f79ace5041fb987f839115067ea2b/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

You should see something similar to the output below in the **Logs** section:

```
Logs:
[
  {
    topic: 'user-events-topic',
    partition: 0,
    offset: 317,
    timestamp: 1641950606804
  }
]
```

You now have a working Action. It’s time to deploy it!

## Deploy the Action

Click the **Deploy** button located near the upper right corner of the page. This saves your Action and makes it the current version in your library,

Now that your Action is in the Library, you need to add it to the Post User Registration flow. Navigate to the flow by selecting **Actions** from the left side menu, then **Flows** →**Post User Registration**. You’ll see this:

![Deploy](https://images.ctfassets.net/23aumh6u8s0i/MTvdp0U3YWpwQ3zP0PNvm/cd660cc3c68f8c1b3f85738f4b764915/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

In the **Add Action** section on the right side of the page, select the **Custom** tab to see the Actions you have created:

![Deploy](https://images.ctfassets.net/23aumh6u8s0i/2ws2KlG9D05TFenL1KkHz5/3665c18f8042ba8f30ae2772c48fc206/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

You should see the **sendKafka** action. Drag and drop it to the area between the **Start** and **Complete** nodes in the flow as shown below:

![sendKafka](https://images.ctfassets.net/23aumh6u8s0i/6xO4IuP3sJXiURYYFYQDqt/4ba5fb18546c69890af0c71cefe55f27/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Now that you have added your Action to the flow apply the change by clicking the **Apply** button near the upper right corner of the page.

## Test the Action

Now that the Action is deployed, it’s time to test it!

### Create a new user

The Action you just created runs whenever a new user signs up, so one way to test it is to create a new user using the Auth0 dashboard.

In the Auth0 dashboard’s left menu, select **User Management** → **Users** → **Create User** and create a new user:

![User Management](https://images.ctfassets.net/23aumh6u8s0i/4oVUjX54aRwTPCOu4bBqXu/6e6bd39df85e29061f918af92681067f/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

After creating the user, switch to the Upstash browser tab or window, select the **Topics** tab, and then select the **user-events-topic** topic:

![Topics](https://images.ctfassets.net/23aumh6u8s0i/5KBLE6L4v4AVlNb5RsQPKX/208f1e1ba0c3820de192cf4d928c99d7/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

This will display the statistics for your Kafka topic:

![Topics](https://images.ctfassets.net/23aumh6u8s0i/6bYnLjjiKI1uQ8hTl7hQnQ/3c318e37ea74536beaa3923054b2c1f8/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Each time you create a new user, a new message is pushed to the Kafka topic, increasing its count —which appears under **Messages** —by one.

You can check the Kafka topic by using the **Consumer** curl script in the **REST API** section under the **Details** tab:

![Details](https://images.ctfassets.net/23aumh6u8s0i/54opQIKp8N757l82HmPrle/199a88de3d5caad79126da557fd76d26/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

![Details](https://images.ctfassets.net/23aumh6u8s0i/7HtICJcX8nk92VgX7O244c/053a270c39a0fb52ea759e85b476812b/Uploaded_from_Send_Auth0_Events_to_Kafka_for_Realtime_Processing_)

Run the curl script in your command line console. The result should look something like this:

```
[ {
  "topic" : "user-events-topic",
  "partition" : 0,
  "offset" : 316,
  "timestamp" : 1641948554609,
  "key" : "",
  "value" : "{\"email\":\"exampleuser@example.com\",\"tenant\":\"test-upstash\",\"user_id\":\"auth0|61de2511fd334200aq221d\",\"app_metadata\":{},\"user_metadata\":{},\"email_verified\":false,\"phone_verified\":false}",
  "headers" : [ ]
} ]%
```
## Conclusion

Auth0 Actions enable developers to inject custom logic into the user lifecycle, adding additional behavior and features to the login process. In this article, we used Auth0 Actions to produce events for Kafka triggered by new user registrations. Once stored in Kafka, there are endless possibilities for how you can leverage the data.