既存のルールセットにルールを 1 件追加します。既存のルールセットのルールをすべてリクエストに含めずに、ルールを追加するときにこのエンドポイントを使います。
次のいずれかの API エンドポイントを使います。
- Create an account ruleset rule
POST /accounts/{account_id}/rulesets/{ruleset_id}/rules - Create a zone ruleset rule
POST /zones/{zone_id}/rulesets/{ruleset_id}/rules
リクエスト本文にルール定義を含めます。
デフォルトでは、ルールはルールセット内の既存ルール一覧の末尾に追加されます。ルールの位置を指定するには、ルールセット内のルールの順序を変える の案内に従い、リクエスト本文に position オブジェクトを含めます。
このメソッドを呼び出すと、ルールセットの新しいバージョンが作成されます。
次の POST リクエストは、ゾーン $ZONE_ID のルールセット $RULESET_ID にルールを追加します。ルールセット ID は、以前 List zone rulesets 操作で取得したもので、http_request_firewall_custom フェーズのエントリポイントルールセットに対応します。
レスポンスには、ルール追加後のルールセット全体が含まれます。
Required API token permissions
At least one of the following token permissions is required:Response Compression WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score"
}'{
"result": {
"id": "<RULESET_ID>",
"name": "Zone Ruleset 1",
"description": "My phase entry point ruleset at the zone level",
"kind": "zone",
"version": "11",
"rules": [
{
"id": "<RULE_ID_1>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"last_updated": "2023-11-23T11:36:24.192361Z",
"ref": "<RULE_REF_1>",
"enabled": true
},
{
"id": "<NEW_RULE_ID>",
"version": "1",
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score",
"last_updated": "2024-06-22T12:35:58.144683Z",
"ref": "<NEW_RULE_REF>",
"enabled": true
}
],
"last_updated": "2024-06-22T12:35:58.144683Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}新しいルールのルールセット内での位置を指定するには、リクエストに次のいずれかを含む position オブジェクトを含めます。
-
"before": "<RULE_ID>"— ルールをルール<RULE_ID>の前に配置します。空のルール ID 値("")と一緒に使うと、ルールセットの先頭に配置します。 -
"after": "<RULE_ID>"— ルールをルール<RULE_ID>の後に配置します。空のルール ID 値("")と一緒に使うと、ルールセットの末尾に配置します。 -
"index": <POSITION_NUMBER>— 整数<POSITION_NUMBER>で指定した位置にルールを配置します。位置番号は1から始まります。指定位置以降の既存ルールは 1 つずつずれます(上書きはされません)。たとえばindexで位置 n にルールを置くと、既存のインデックス n、n+1、n+2 などのルールは 1 つずつずれ、新しい位置は n+1、n+2、n+3 になります。インデックスが範囲外の場合、メソッドは HTTP ステータスコード400を返します。
position オブジェクトの使用例は、ルールセット内のルールを更新する を参照してください。