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. In Auth0, the authentication transaction flow works as follows when you use rules:
- An app initiates an authentication request to Auth0.
- Auth0 routes the request to an identity provider through a configured connection.
- The user authenticates successfully.
- 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.Use the Dashboard
-
Go to Dashboard > Auth Pipeline > Rules and click Create.

-
Select a rule template.

-
Name the rule, modify the script to suit your needs, and click Save changes.

Use the Management API
Make aPOST call to the Create Rule endpoint. Be sure to replace MGMT_API_ACCESS_TOKEN, RULE_NAME, RULE_SCRIPT, RULE_ORDER, and RULE_ENABLED placeholder values with your , rule name, rule script, rule order number, and rule enabled value, respectively.
| 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). |
Manage rate limits
For rules that call Auth0 APIs, you should always handle rate limiting by checking theX-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.