> This page is for Applications Protection.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.humansecurity.com/llms.txt.

# Create custom rule

POST https://console.humansecurity.com/api/v2/botDefender/customRules
Content-Type: application/json

Configure a custom allow or deny list rule.


Reference: https://docs.humansecurity.com/applications/reference/create-custom-rule

## Authentication

- `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer <token>`, where token is your auth token.

## Servers

- `https://console.humansecurity.com/api` (Version 1 of the API, default)
- `https://console.humansecurity.com/api` (Version 2 of the API)

## Request

### Body (application/json)

This endpoint expects a customRule.

- `name` (string, required)
- `conditions` (CustomRuleConditions, required) — A nested object with **exactly** two levels.
- `actions` (list of enum, required)
  - Allowed values: `allow`, `blockWithChallenge`, `hardBlock`
- `status` (enum, required)
  - Allowed values: `active`, `paused`
- `ruleId` (string, optional)
- `priority` (integer, optional, default: the value corresponding to the lowest priority rule.) — API returns rules between 0 (the highest priority) and the number of rules minus 1 (the lowest priority). This corresponds to the console's rules between 1 (the highest priority) and the number of rules. For example, if there are 10 rules, then in the API, the 0th rule is the highest priority while the 9th rule is the lowest priority. This 0th rule corresponds to the 1st rule in the console. Based on this example, the default value would be 9.
- `description` (string, optional)

## Response

### 200

Success

- `result` (boolean, optional)
- `message` (string, optional)
- `content` (customRule, optional)

## Errors

### 400 Bad Request Error

The request was invalid or did not have the proper authentication.

- `any`

## Types

### CustomRuleConditions

A nested object with **exactly** two levels.

- `operator` (enum, optional)
  - Allowed values: `$or`, `$and`
- `children` (list of CustomRuleConditionsChildrenItems, optional)

### customRule

- `name` (string, required)
- `conditions` (CustomRuleConditions, required) — A nested object with **exactly** two levels.
- `actions` (list of enum, required)
  - Allowed values: `allow`, `blockWithChallenge`, `hardBlock`
- `status` (enum, required)
  - Allowed values: `active`, `paused`
- `ruleId` (string, optional)
- `priority` (integer, optional, default: the value corresponding to the lowest priority rule.) — API returns rules between 0 (the highest priority) and the number of rules minus 1 (the lowest priority). This corresponds to the console's rules between 1 (the highest priority) and the number of rules. For example, if there are 10 rules, then in the API, the 0th rule is the highest priority while the 9th rule is the lowest priority. This 0th rule corresponds to the 1st rule in the console. Based on this example, the default value would be 9.
- `description` (string, optional)

### CustomRuleConditionsChildrenItems

- `operator` (enum, optional)
  - Allowed values: `$or`, `$and`
- `children` (list of CustomRuleConditionsChildrenItemsChildrenItems, optional)

### CustomRuleConditionsChildrenItemsChildrenItems

- `operator` (enum, optional) — The operator to filter with. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.
  - Allowed values: `$eq`, `$contains`, `$startsWith`, `$endsWith`, `$re`, `$notEndsWith`, `$ne`, `$in`, `$notIn`, `$exists`, `$notExists`
- `conditionType` (enum, optional) — The condition to filter by. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.
  - Allowed values: `userAgent`, `domain`, `socketIps`, `socketIpASN`, `path`, `queryParam`, `header`, `httpMethod`, `deviceType`, `country`, `customParam`
- `value` (CustomRuleConditionsChildrenItemsChildrenItemsValue, optional) — The value(s) to match based on the operator. Not required with exists or notExists, but required otherwise. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.
- `fieldOption` (CustomRuleConditionsChildrenItemsChildrenItemsFieldOption, optional) — The name of the field. Only required when using a `queryParam`, `header`, or `customParam` condition. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.

### CustomRuleConditionsChildrenItemsChildrenItemsValue

The value(s) to match based on the operator. Not required with exists or notExists, but required otherwise. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.

- string
- list of string

### CustomRuleConditionsChildrenItemsChildrenItemsFieldOption

The name of the field. Only required when using a `queryParam`, `header`, or `customParam` condition. See [this article](/applications/reference/custom-rule-operators-and-conditions) for details on how to implement.

- string
- list of string

## Examples

**Request**

```json
{
  "name": "string",
  "actions": [
    "allow"
  ],
  "status": "active"
}
```

**Response**

```json
{
  "result": true,
  "message": "success",
  "content": {
    "name": "string",
    "conditions": {
      "operator": "$or",
      "children": [
        {
          "operator": "$or",
          "children": [
            {
              "operator": "$eq",
              "conditionType": "userAgent",
              "value": "string",
              "fieldOption": "string"
            }
          ]
        }
      ]
    },
    "actions": [
      "allow"
    ],
    "status": "active",
    "ruleId": "a747610a-e1d6-4e50-bc60-04c8787ac7b8",
    "priority": 1,
    "description": "string"
  }
}
```

**SDK Code**

```python
import requests

url = "https://console.humansecurity.com/api/v2/botDefender/customRules"

payload = {
    "name": "string",
    "actions": ["allow"],
    "status": "active"
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

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

print(response.json())
```

```javascript
const url = 'https://console.humansecurity.com/api/v2/botDefender/customRules';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"name":"string","actions":["allow"],"status":"active"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://console.humansecurity.com/api/v2/botDefender/customRules"

	payload := strings.NewReader("{\n  \"name\": \"string\",\n  \"actions\": [\n    \"allow\"\n  ],\n  \"status\": \"active\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://console.humansecurity.com/api/v2/botDefender/customRules")

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\": \"string\",\n  \"actions\": [\n    \"allow\"\n  ],\n  \"status\": \"active\"\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://console.humansecurity.com/api/v2/botDefender/customRules")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"name\": \"string\",\n  \"actions\": [\n    \"allow\"\n  ],\n  \"status\": \"active\"\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://console.humansecurity.com/api/v2/botDefender/customRules', [
  'body' => '{
  "name": "string",
  "actions": [
    "allow"
  ],
  "status": "active"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://console.humansecurity.com/api/v2/botDefender/customRules");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"string\",\n  \"actions\": [\n    \"allow\"\n  ],\n  \"status\": \"active\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "name": "string",
  "actions": ["allow"],
  "status": "active"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://console.humansecurity.com/api/v2/botDefender/customRules")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```