Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

ルールセットを作成する

最終更新 Markdown で表示Agent セットアップ

指定したフェーズに、指定した種類のルールセットを作成します。フェーズのエントリポイントルールセットも作成できます。

次のいずれかの API エンドポイントを使います。

パラメーター

ルールセットを作成する POST リクエストは、リクエスト本文で次のパラメーターをサポートします。

  • name String
    • ルールセットの分かりやすい名前です。
    • 名前は変更できません。ルールセットの存続期間中は変えられません。
  • description String 任意
    • ルールセットの任意の説明です。
    • ルールセットの存続期間中に説明は変更できます。
  • kind String
    • JSON オブジェクトが表すルールセットの種類です。
    • 使用できる値:
      • custom: カスタムルールセットを作成します
      • root: アカウントレベルでフェーズの エントリポイントルールセット を作成します
      • zone: ゾーンレベルでフェーズのエントリポイントルールセットを作成します
  • phase String
    • ルールセットを作成する フェーズ の名前です。
    • その製品向けにルールセットを作成できるフェーズについては、フェーズ一覧 または各 Cloudflare 製品のドキュメントを確認してください。
  • rules Array<Rule> 任意
    • ルールセットに含める ルール の一覧です。

これらのパラメーターの詳細は JSON オブジェクト を参照してください。

例 - カスタムルールセットを作成する

次の POST リクエストは、アカウントレベルの http_request_firewall_custom フェーズに、ルールを 1 件含むカスタムルールセットを作成します。

Required API token permissions

At least one of the following token permissions is required:
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • Transform Rules Write
  • Select Configuration Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
Create an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Example custom ruleset",
		"kind": "custom",
		"description": "Example ruleset description",
		"rules": [
				{
						"action": "log",
						"expression": "cf.zone.name eq \"example.com\""
				}
		],
		"phase": "http_request_firewall_custom"
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Example custom ruleset",
		"description": "Example ruleset description",
		"kind": "custom",
		"version": "1",
		"rules": [
			{
				"id": "<RULE_ID>",
				"version": "1",
				"action": "log",
				"expression": "cf.zone.name eq \"example.com\"",
				"last_updated": "2025-03-17T15:42:37.917815Z"
			}
		],
		"last_updated": "2025-03-17T15:42:37.917815Z",
		"phase": "http_request_firewall_custom"
	},
	"success": true,
	"errors": [],
	"messages": []
}

例 - ゾーンレベルのフェーズエントリポイントルールセットを作成する

次の POST リクエストは、http_request_firewall_managed フェーズにゾーンレベルのフェーズエントリポイントルールセットを作成し、マネージドルールセットを実行するルールを 1 件含めます。

Required API token permissions

At least one of the following token permissions is required:
  • Response Compression Write
  • Config Settings Write
  • Dynamic URL Redirects Write
  • Cache Settings Write
  • Custom Errors Write
  • Origin Write
  • Managed headers Write
  • Zone Transform Rules Write
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • HTTP DDoS Managed Ruleset Write
  • Sanitize Write
  • Transform Rules Write
  • Select Configuration Write
  • Bot Management Write
  • Zone WAF Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
  • Logs Write
Create a zone rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Zone-level phase entry point",
		"kind": "zone",
		"description": "This ruleset executes a managed ruleset.",
		"rules": [
				{
						"action": "execute",
						"expression": "true",
						"action_parameters": {
								"id": "<MANAGED_RULESET_ID>"
						}
				}
		],
		"phase": "http_request_firewall_managed"
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Zone-level phase entry point",
		"description": "This ruleset executes a managed ruleset.",
		"kind": "zone",
		"version": "1",
		"rules": [
			{
				"id": "<RULE_ID>",
				"version": "1",
				"action": "execute",
				"expression": "true",
				"action_parameters": {
					"id": "<MANAGED_RULESET_ID>"
				},
				"last_updated": "2025-03-17T15:42:37.917815Z"
			}
		],
		"last_updated": "2025-03-17T15:42:37.917815Z",
		"phase": "http_request_firewall_managed"
	},
	"success": true,
	"errors": [],
	"messages": []
}

例 - アカウントレベルのフェーズエントリポイントルールセットを作成する

次の POST リクエストは、http_ratelimit フェーズ向けのアカウントレベルのフェーズエントリポイントルールセットを作成し、アカウント内のすべての Enterprise ゾーンに対してレート制限ルールセットを実行するルールを 1 件含めます。

Required API token permissions

At least one of the following token permissions is required:
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • Transform Rules Write
  • Select Configuration Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
Create an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Account-level phase entry point",
		"kind": "root",
		"description": "This ruleset executes a rate limiting ruleset.",
		"rules": [
				{
						"action": "execute",
						"expression": "(cf.zone.plan eq \"ENT\")",
						"action_parameters": {
								"id": "<RATE_LIMITING_RULESET_ID>"
						}
				}
		],
		"phase": "http_ratelimit"
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Account-level phase entry point",
		"description": "This ruleset executes a rate limiting ruleset.",
		"kind": "root",
		"version": "1",
		"rules": [
			{
				"id": "<RULE_ID>",
				"version": "1",
				"action": "execute",
				"expression": "(cf.zone.plan eq \"ENT\")",
				"action_parameters": {
					"id": "<RATE_LIMITING_RULESET_ID>"
				},
				"last_updated": "2024-09-17T15:42:37.917815Z"
			}
		],
		"last_updated": "2024-09-17T15:42:37.917815Z",
		"phase": "http_ratelimit"
	},
	"success": true,
	"errors": [],
	"messages": []
}

役に立ちましたか?