> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-actions-triggers-prototype.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Learn how to write an Action, which includes choosing a flow, creating an Action and configuring it, and binding it to the flow.

# Write Your First Action

This guide will walk you through how to create and deploy your first Action using the <Tooltip tip="Auth0 Dashboard: Auth0's main product to configure your services." cta="View Glossary" href="/docs/glossary?term=Auth0+Dashboard">Auth0 Dashboard</Tooltip>. When you finish, you should understand the basic functionality that you will use with all Actions and you will be familiar with the programming model. What you will learn constitutes the foundation of writing any Action, regardless of its purpose or the flow.

## The goal: notify Slack on user login

You are creating an Action that sends a message to a Slack channel when a user logs in. While this tutorial will use the Post Login trigger, the information provided here is useful for triggers of any type.

To accomplish this task, you will do the following:

* [Create an Action](#create-an-action)
* [Add a Secret](#add-a-secret)
* [Add a dependency](#add-a-dependency)
* [Save the Draft](#save-the-draft)
* [Add custom logic](#add-custom-logic)
* [Test an Action](#test-an-action)
* [Deploy an Action](#deploy-the-action)
* [Attach an Action to a Flow](#attach-the-action-to-a-flow)
* [See the results of an Action in Tenant Logs](#observe-actions-in-tenant-logs)

## Prerequisites

Because this Action will be sending messages to a Slack channel, you need to [create an Incoming Webhook for a Slack Workspace](https://api.slack.com/messaging/webhooks). Once you have a Slack Webhook URL, you may continue with this guide.

## Create an Action

To get an Action working in a specific flow, you need to create the Action and then add it to a flow.

1. Navigate to [Auth0 Dashboard > Actions > Library,](https://manage.auth0.com/#/actions/library) then select **Create Action** > **Build from scratch**.
2. Enter a **Name** and select the **Login / Post Login** trigger since you’ll be adding an Action to the Login flow, then select **Create.**

The Actions Code Editor appears:

<Frame>
  <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/I3gNYw4Uo9lArprN/docs/images/cdy7uua7fh8z/12Q8OEuDI39eWuWsrVVwN0/ff7dd385d33aee704e1cb603f43c679b/2025-02-28_13-27-14.png?fit=max&auto=format&n=I3gNYw4Uo9lArprN&q=85&s=206f10edf4d72fc3b8e702ac3a0c9568" alt="Actions Code Editor" width="902" height="507" data-path="docs/images/cdy7uua7fh8z/12Q8OEuDI39eWuWsrVVwN0/ff7dd385d33aee704e1cb603f43c679b/2025-02-28_13-27-14.png" />
</Frame>

## Create an Action from a Template

The Actions Template gallery offers a variety of starter templates on their way to creating an Action. To create an Action from a Template:

1. Navigate to [Auth0 Dashboard > Actions > Library](https://manage.auth0.com/#/actions/library), then select **Create Action.**
2. Select **Choose from template**.
3. Select the template that you would like to use for building an Action.
4. You should now see a read-only preview of the code within the template. To proceed, select **Use this template**.
5. Enter a name, and select **Create**.

## Add a Secret

Each Action can contain Secret values, which are suitable for holding sensitive information, such as <Tooltip tip="Access Token: Authorization credential, in the form of an opaque string or JWT, used to access an API." cta="View Glossary" href="/docs/glossary?term=Access+Tokens">Access Tokens</Tooltip> and API Keys. Let’s store the Slack Webhook URL as a Secret.

1. Select the Key Icon from the code editor's left sidebar
2. Select **Add Secret**.
3. Give the Secret the following name: `SLACK_WEBHOOK_URL`.
4. Paste in the Webhook URL provided by Slack and select **Create**.

You’ll see that the Secret has been added to the Action and you can use the new secret via intelligent code complete by typing `event.secrets`.

<Frame>
  <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/pJYW9vLPIqzUNcly/docs/images/cdy7uua7fh8z/4FyFtKRSIYuigxE9U8TkEJ/9598b5909694631ca673f748abf2268a/Screen_Shot_2022-02-09_at_3.24.05_PM.png?fit=max&auto=format&n=pJYW9vLPIqzUNcly&q=85&s=89701a8285191c1e7635caf7fcb2df78" alt="Actions Code Editor - Your Secret has been added in the Secrets section." width="1325" height="780" data-path="docs/images/cdy7uua7fh8z/4FyFtKRSIYuigxE9U8TkEJ/9598b5909694631ca673f748abf2268a/Screen_Shot_2022-02-09_at_3.24.05_PM.png" />
</Frame>

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  Once a Secret has been created, its value will never be revealed. Auth0 encrypts all Secrets and stores them securely.
</Callout>

## Add a dependency

You’ll be using the [`@slack/webhook` `npm` package](https://www.npmjs.com/package/@slack/webhook) to make it easy to send a message to Slack. You can use nearly any public `npm` package in an Action as long as it can be installed without relying on [native add-ons](https://www.npmjs.com/package/node-gyp).

To add the dependency:

1. Select the Dependency icon (cube icon) in the sidebar.
2. For **Name**, enter `@slack/webhook`.

   By default, your Action will use the latest version of the dependency at the time the dependency was added.
3. Select **Create**, and the Dependency should be added to the Action:

   <Frame>
     <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/nRTYX19FsgfcTLla/docs/images/cdy7uua7fh8z/Jmzvgydo204pBLikzuDJy/1d9a614b4c92f27afbbdd2b2af3b3c0b/Screen_Shot_2022-02-23_at_11.28.41_AM.png?fit=max&auto=format&n=nRTYX19FsgfcTLla&q=85&s=671b256e74b38095913bd3998cf149a9" alt="Actions Code Editor: Your dependency has been added." width="1504" height="614" data-path="docs/images/cdy7uua7fh8z/Jmzvgydo204pBLikzuDJy/1d9a614b4c92f27afbbdd2b2af3b3c0b/Screen_Shot_2022-02-23_at_11.28.41_AM.png" />
   </Frame>

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  When you save this Action, the latest version of your dependency will be resolved and replaced with a specific version number to keep future updates to the package from breaking your Action.
</Callout>

## Save the Draft

Select **Save Draft**. Your Action is saved, but it won’t be executed as part of any user flows yet. With Actions, you have the opportunity to create, edit, and test Actions before they affect any traffic in your Auth0 tenant. Once you’re happy with the behavior of the Action, you’ll deploy the Action and add it to a Flow later.

## Add custom logic

Now that the Action has been configured with a Secret and a dependency, let’s write some code!

Every Action has an event object that contains read-only, contextual information relevant to the associated trigger. If you start typing `event.` in the Editor, you should be presented with all the available properties of the event. To learn more about which information is available on each trigger, read [Explore Flows and Triggers](/docs/customize/actions/explore-triggers).

Let’s use our Action to notify Slack. Add this code to your action:

```javascript lines theme={null}
const { IncomingWebhook } = require('@slack/webhook');

exports.onExecutePostLogin = async (event, api) => {
    const url = event.secrets.SLACK_WEBHOOK_URL;
    const webhook = new IncomingWebhook(url);

    // Send the notification
    await webhook.send({ text: "Logging In..." });
};
```

Select **Save Draft** to save your Action.

## Test the Action

Before you run this Action in your tenant with real user traffic, test it to ensure it behaves as expected.

1. Select the Test (triangle) icon in the sidebar. In the Payload section, you’ll see editable JSON data relevant to the trigger that you selected when you created the Action.

   <Frame>
     <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/pJYW9vLPIqzUNcly/docs/images/cdy7uua7fh8z/4MSu8EfpMLNJpcYtrwP7Jf/66480a0434c89eadb809012bca890037/Screen_Shot_2022-02-10_at_10.01.10_AM.png?fit=max&auto=format&n=pJYW9vLPIqzUNcly&q=85&s=e8a46637a72be31597d38b7d55898740" alt="Actions Code Editor: Test Runner panel" width="378" height="644" data-path="docs/images/cdy7uua7fh8z/4MSu8EfpMLNJpcYtrwP7Jf/66480a0434c89eadb809012bca890037/Screen_Shot_2022-02-10_at_10.01.10_AM.png" />
   </Frame>
2. Select the **Run** button, and you should see a message appear in your Slack channel.

## Deploy the Action

Now that we’re satisfied that the Action is working as expected, it’s time to deploy it.

Select **Deploy**. Deploying an Action takes a snapshot of the Action at that time and records it as an **Action Version**.

Select **Version History**, and you should see the Action recorded as Version 1:

<Frame>
  <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/8yG0sorggbq9bIbi/docs/images/cdy7uua7fh8z/53sn70cXoxUHibc3Bmk7Og/193af512ac2b58f3d60ab36ca5499d90/Screen_Shot_2022-02-10_at_10.02.47_AM.png?fit=max&auto=format&n=8yG0sorggbq9bIbi&q=85&s=20670ff01d8b6d815bea5a1fa1fca545" alt="Actions: Version History" width="326" height="177" data-path="docs/images/cdy7uua7fh8z/53sn70cXoxUHibc3Bmk7Og/193af512ac2b58f3d60ab36ca5499d90/Screen_Shot_2022-02-10_at_10.02.47_AM.png" />
</Frame>

You are now free to update your Action and save it without affecting Version 1 of your Action. Auth0 will only execute the currently deployed version of an Action, so until you deploy an action again, Version 1 will be executed by Auth0.

## Attach the Action to a Flow

The final step in configuring a new Action so that it’s executed as part of your tenant’s traffic is to attach it to a Flow.

1. Navigate to [Auth0 Dashboard > Actions > Flows](https://manage.auth0.com/#/actions/flows) to see a list of the available Flows:
2. Select the [Login flow](/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger) and drag `my-slack-action` into the flow.

   <Frame>
     <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/4MaQENhfcY-1egb6/docs/images/cdy7uua7fh8z/6hU7sKWtGzSx9Z8dhsXbvp/0253fb4e9eac1133cc9626385622c54e/Login_Action_-_English.png?fit=max&auto=format&n=4MaQENhfcY-1egb6&q=85&s=a26316de4f20f9956626b717b5c7d4ad" alt="Actions Flows: Login Flow" width="842" height="836" data-path="docs/images/cdy7uua7fh8z/6hU7sKWtGzSx9Z8dhsXbvp/0253fb4e9eac1133cc9626385622c54e/Login_Action_-_English.png" />
   </Frame>
3. Select **Apply**.

The Action is now running for real user traffic within your tenant. If you’d like a more robust testing environment, consider [setting up multiple Auth0 environments](/docs/get-started/auth0-overview/create-tenants/set-up-multiple-environments).

## Observe Actions in Tenant Logs

Now that your Action is running as part of the Login flow, Auth0 captures information about each Action execution in the tenant logs. To view this, navigate to [Auth0 Dashboard > Monitoring > Logs](https://manage.auth0.com/#/logs), and select a **Successful Login** event.

You should see an **Action Details** view that contains information about any Actions that were executed as part of that flow.

<Frame>
  <img src="https://mintcdn.com/docs-dev-actions-triggers-prototype/aEQNasKiS5oso5hx/docs/images/cdy7uua7fh8z/61k82wmsXSb9M3wOExM4jH/51fae3a36a8ecddeec08bc45da468e2b/Action_logs_-_English.png?fit=max&auto=format&n=aEQNasKiS5oso5hx&q=85&s=2546f96b2204b94ef6b4d825eb5a6d84" alt="Tenant Logs: Action Details view" width="1079" height="1010" data-path="docs/images/cdy7uua7fh8z/61k82wmsXSb9M3wOExM4jH/51fae3a36a8ecddeec08bc45da468e2b/Action_logs_-_English.png" />
</Frame>

## Next steps

Now that you’ve written your first Action, check out [Explore Flows and Triggers](/docs/customize/actions/explore-triggers) to learn how to perform some common tasks with Actions.
