APIを介してルールを作成する
Rulesets APIを使用して、APIを介して設定ルールを作成します。
APIを介して設定ルールを作成する際は、以下の点に注意してください:
- ルールアクションを
set_configに設定します。 - 一致するリクエストのために上書きしたい設定に従って、
action_parametersフィールドにパラメータを定義します。 - ルールをゾーンレベルの
http_config_settingsフェーズにデプロイします。
Follow this workflow to create 設定ルール for a given zone via API:
-
Use the List zone rulesets operation to check if there is already a ruleset for the
http_config_settingsphase at the zone level. -
If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:
- kind:
zone - phase:
http_config_settings
- kind:
-
Use the Update a zone ruleset operation to add 設定ルール to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.
APIトークンがAPI操作を実行するために必要な権限を持っていることを確認してください。
例: Email ObfuscationとBrowser Integrity Checkを有効にするルールを追加
以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一の設定ルールに設定します — 連絡先ページのEmail ObfuscationとBrowser Integrity Checkを有効にする — ゾーンルールセットの更新操作を使用しています:
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "starts_with(http.request.uri.path, \"/contact-us/\")", "description": "連絡先ページでメールアドレスを難読化し、BICを有効にします", "action": "set_config", "action_parameters": { "email_obfuscation": true, "bic": true } } ]}'例: 管理エリアのためにI'm Under Attackモードをオンにするルールを追加
以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一の設定ルールに設定します — 管理エリアのためにI’m Under Attackモードをオンにする — ゾーンルールセットの更新操作を使用しています:
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "http.host eq \"admin.example.com\"", "description": "管理エリアのためにI'm Under Attackモードをオンにします", "action": "set_config", "action_parameters": { "security_level": "under_attack" } } ]}'設定ルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも以下の権限を持っている必要があります:
- Zone > Config Rules > Edit