Passer au contenu principal
POST
https://{tenantDomain}/api/v2
/
rules
Go
package example

import (
    context "context"

    management "github.com/auth0/go-auth0/management/management"
    client "github.com/auth0/go-auth0/management/management/client"
    option "github.com/auth0/go-auth0/management/management/option"
)

func do() {
    client := client.NewClient(
        option.WithToken(
            "<token>",
        ),
    )
    request := &management.CreateRuleRequestContent{
        Name: "name",
        Script: "script",
    }
    client.Rules.Create(
        context.TODO(),
        request,
    )
}
import { ManagementClient } from "auth0";

async function main() {
const client = new ManagementClient({
token: "<token>",
});
await client.rules.create({
name: "name",
script: "script",
});
}
main();
import { ManagementClient } from "auth0";

async function main() {
const client = new ManagementClient({
token: "<token>",
});
await client.rules.create({
name: "name",
script: "script",
});
}
main();
curl --request POST \
--url https://{tenantDomain}/api/v2/rules \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "my-rule",
"script": "function (user, context, callback) {\n callback(null, user, context);\n}",
"order": 2,
"enabled": true
}
'
import requests

url = "https://{tenantDomain}/api/v2/rules"

payload = {
"name": "my-rule",
"script": "function (user, context, callback) {
callback(null, user, context);
}",
"order": 2,
"enabled": True
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://{tenantDomain}/api/v2/rules",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'my-rule',
'script' => 'function (user, context, callback) {
callback(null, user, context);
}',
'order' => 2,
'enabled' => true
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
HttpResponse<String> response = Unirest.post("https://{tenantDomain}/api/v2/rules")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"my-rule\",\n \"script\": \"function (user, context, callback) {\\n callback(null, user, context);\\n}\",\n \"order\": 2,\n \"enabled\": true\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://{tenantDomain}/api/v2/rules")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"my-rule\",\n \"script\": \"function (user, context, callback) {\\n callback(null, user, context);\\n}\",\n \"order\": 2,\n \"enabled\": true\n}"

response = http.request(request)
puts response.read_body
{
  "name": "rule_1",
  "id": "con_0000000000000001",
  "enabled": true,
  "script": "function (user, context, callback) {\n  callback(null, user, context);\n}",
  "order": 1,
  "stage": "login_success"
}

Autorisations

Authorization
string
header
requis

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Corps

name
string
défaut:my-rule
requis

Name of this rule.

Pattern: ^[a-zA-Z0-9]([ \-a-zA-Z0-9]*[a-zA-Z0-9])?$
script
string
défaut:function (user, context, callback) { callback(null, user, context); }
requis

Code to be executed when this rule runs.

Minimum string length: 1
order
number
défaut:2

Order that this rule should execute in relative to other rules. Lower-valued rules execute first.

Plage requise: x >= 0
enabled
boolean
défaut:true

Whether the rule is enabled (true), or disabled (false).

Réponse

Rule successfully created.

name
string
défaut:rule_1

Name of this rule.

id
string
défaut:con_0000000000000001

ID of this rule.

enabled
boolean
défaut:true

Whether the rule is enabled (true), or disabled (false).

script
string
défaut:function (user, context, callback) { callback(null, user, context); }

Code to be executed when this rule runs.

order
number
défaut:1

Order that this rule should execute in relative to other rules. Lower-valued rules execute first.

stage
string
défaut:login_success

Execution stage of this rule. Can be login_success, login_failure, or pre_authorize.