> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Rules

> Learn how to create rules using the Auth Dashboard or the Management API.

> [!WARNING]
>
> **Rules will reach end of life (EOL) on January 31, 2028.**
>
> * As of October 16, 2023, Rules are no longer available in newly created tenants.
> * As of November 18, 2024, Rules are read-only. Active Rules still run, and you can enable or disable them and change their configuration values and secrets. You can't edit their code in the Dashboard, the Management API, or tools that use it, such as the Auth0 CLI, Deploy CLI, Terraform, and Agent Skills.
> * **After January 31, 2028, Rules stop running and are removed.**

**Use Actions to extend Auth0:**

* To learn more about Actions, read [Understand How Auth0 Actions Work](/docs/docs/customize/actions/actions-overview).
* To migrate, read [Migrate from Rules to Actions](/docs/docs/customize/actions/migrate/migrate-from-rules-to-actions).

You can build your own rule(s) to support your specific functionality requirements. You can modify a pre-existing rule template or choose to start from scratch using one of our samples. Auth0 provides a number of pre-existing rules and rule templates to help you achieve your goal(s). To see a list, visit our [rules repository on GitHub](https://github.com/auth0/rules).

## How rules work

Rules are JavaScript functions that execute when a user authenticates to your application. They run once the authentication process is complete, and you can use them to customize and extend Auth0's capabilities. For security reasons, your rules code executes isolated from the code of other Auth0 tenants in a sandbox. Rules also run during the token refresh flow. To learn more, read [Refresh Tokens](/docs/docs/secure/tokens/refresh-tokens).

In Auth0, the authentication transaction flow works as follows when you use rules:

![Rules in the Authentication Flow diagram](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/2gtBtkPChIyguA24x6enx2/ffbb8e21e86920ef9914f6fc126dc1df/flow.png)

1. An app initiates an authentication request to Auth0.
2. Auth0 routes the request to an identity provider through a configured connection.
3. The user authenticates successfully.
4. The ID token and/or access token is passed through the rules pipeline, then sent to the application.

## Prerequisite

If you plan to use global variables in your rule, be sure to configure your rules variables first. To learn more, read [Configure Global Variables for Rules](/docs/docs/customize/rules/configure-global-variables-for-rules).

## Use the Dashboard

1. Go to [Dashboard > Auth Pipeline > Rules](https://manage.auth0.com/#/rules) and click **Create**.

   ![Dashboard - Auth Pipeline - Rules ](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/4OiSXzc5fYgPagHdOGbfvj/a589bdf811df66658fe21c509aed610c/Dashboard_-_Auth_Pipeline_-_Rules.png)
2. Select a rule template.

   ![Dashboard - Auth Pipeline - Rules - Template](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/6IydSSjg2oQrdSzErcTYtN/d17348a98c597a74b2989c298764b9e1/dashboard-rules-create_choose-template.png)
3. Name the rule, modify the script to suit your needs, and click **Save changes**.

   ![Dashboard - Auth Pipeline - Rules - Edit Rule](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/5CoC6cnazv2uT1iSq6OGsm/6cb30d9479971be771313da80acc4802/Dashboard_-_Auth_Pipeline_-_Rules_-_Edit_Rule.png)

## Use the Management API

Make a `POST` call to the [Create Rule endpoint](/docs/docs/api/management/v2/rules/post-rules). Be sure to replace `MGMT_API_ACCESS_TOKEN`, `RULE_NAME`, `RULE_SCRIPT`, `RULE_ORDER`, and `RULE_ENABLED` placeholder values with your Management API (Management API: A product to allow customers to perform administrative tasks.) Access Token (Management API: A product to allow customers to perform administrative tasks.), rule name, rule script, rule order number, and rule enabled value, respectively.

Using the Auth0 CLI? If you haven't already, [set up and authenticate your CLI session](/docs/docs/deploy-monitor/auth0-cli) before running this command.

| Value | Description |
| - | - |
| `MGMT_API_ACCESS_TOKEN` | Access Token for the Management API with the scope `create:rules`. |
| `RULE_NAME` | Name of the rule you would like to create. The rule name can only contain alphanumeric characters, spaces, and hyphens; it may not start or end with spaces or hyphens. |
| `RULE_SCRIPT` | Script that contains the code for the rule. Should match what you would enter if you were creating a new rule using the Dashboard. |
| `RULE_ORDER` (optional) | Integer that represents the order in which the rule should be executed in relation to other rules. Rules with lower numbers are executed before rules with higher numbers. If no order number is provided, the rule will execute last. |
| `RULE_ENABLED` (optional) | Boolean that represents whether the rules is enabled (`true`) or disabled (`false`). |

> [!WARNING]
>
> We expose IPv6 addresses in our public endpoints (e.g., `travel0.us.auth0.com`). If a request arrives from a machine that supports IPv6, then the `context.request.ip` property will contain an IPv6 address. If you perform manual IP address manipulation, we suggest you use the [ipaddr.js@1.9.0 library](https://www.npmjs.com/package/ipaddr.js/v/1.9.0).

## Manage rate limits

For rules that call Auth0 APIs, you should always handle rate limiting by checking the `X-RateLimit-Remaining` header and acting appropriately when the number returned nears 0. You should also add logic to handle cases in which you exceed the provided rate limits and receive the HTTP Status Code 429 (Too Many Requests); in this case, if a retry is needed, it is best to allow for a back-off to avoid going into an infinite retry loop. To learn more about rate limits, read [Rate Limit Policy For Auth0 APIs](/docs/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy).

## Available modules

Rules run in a JavaScript sandbox configured for a specific Node.js version.

The sandbox supports all versions of the JavaScript language (and associated syntax) for the configured Node.js version, and a large number of Node.js modules. For a list of supported sandbox modules, check out [Can I require: Auth0 Extensibility](https://auth0-extensions.github.io/canirequire/).

## Learn more

* [Configure Global Variables for Rules](/docs/docs/customize/rules/configure-global-variables-for-rules)
