次のセクションは、よく使う API 呼び出しのリクエスト例です。利用できる API エンドポイントの一覧は Endpoints を参照してください。
この例は、アカウント内のすべての Programmable Flow Protection プログラムを取得します。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/programs" \
--header "Authorization: Bearer <API_TOKEN>"{
"result": [
{
"id": "<PROGRAM_ID>",
"name": "rate-limiter",
"status": "success",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
}
],
"success": true,
"errors": [],
"messages": []
}この例は、C で書いた新しい eBPF プログラムをアップロードします。プログラムのソースコードは、Content-Type: text/plain でリクエストボディとして送ります。
人が読めるプログラム名を指定するには、任意の X-Program-Name ヘッダーを含めます。省略すると、API はプログラム名として UUID を生成します。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/programs" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: text/plain" \
--header "X-Program-Name: my-rate-limiter" \
--data-binary "@/path/to/program.c"{
"result": {
"id": "<PROGRAM_ID>",
"name": "my-rate-limiter",
"status": "success",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
},
"success": true,
"errors": [],
"messages": []
}プログラムのコンパイルまたは検証に失敗すると、API は詳細なエラーメッセージを返します。
{
"result": null,
"success": false,
"errors": [
{
"code": 1001,
"message": "Program verification failed: invalid memory access at line 42"
}
],
"messages": []
}この例は、既存のプログラムを新しいソースコードで更新します。1 つ以上のルールで使用中のプログラムでも更新できます。新しいプログラムのコンパイルまたは検証に失敗すると、更新は失敗し、既存のプログラムが引き続き有効です。
curl --request PATCH \
"https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/programs/{program_id}" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: text/plain" \
--data-binary "@/path/to/updated-program.c"{
"result": {
"id": "<PROGRAM_ID>",
"name": "program",
"status": "success",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
},
"success": true,
"errors": [],
"messages": []
}この例はプログラムを削除します。有効なルールから参照されているプログラムは削除できません。
curl --request DELETE \
"https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/programs/{program_id}" \
--header "Authorization: Bearer <API_TOKEN>"{
"result": null,
"success": true,
"errors": [],
"messages": []
}この例は、アカウント内のすべての Programmable Flow Protection ルールを取得します。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/rules" \
--header "Authorization: Bearer <API_TOKEN>"{
"result": [
{
"id": "<RULE_ID>",
"program_id": "<PROGRAM_ID>",
"scope": "global",
"name": "global",
"mode": "enabled",
"expression": "",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
}
],
"success": true,
"errors": [],
"messages": []
}この例は、グローバルスコープで、monitoring モードの Programmable Flow Protection ルールを作成します。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/rules" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"program_id": "<PROGRAM_ID>",
"scope": "global",
"name": "global",
"mode": "monitoring"
}'{
"result": {
"id": "<RULE_ID>",
"program_id": "<PROGRAM_ID>",
"scope": "global",
"name": "global",
"mode": "monitoring",
"expression": "",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
},
"success": true,
"errors": [],
"messages": []
}JSON ボディのフィールドの詳細は JSON objects を参照してください。
この例は、西ヨーロッパリージョンにスコープし、式フィルター付きのルールを作成します。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/rules" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"program_id": "<PROGRAM_ID>",
"scope": "region",
"name": "WEUR",
"mode": "enabled",
"expression": "ip.dst in { 192.0.2.0/24 }"
}'{
"result": {
"id": "<RULE_ID>",
"program_id": "<PROGRAM_ID>",
"scope": "region",
"name": "WEUR",
"mode": "enabled",
"expression": "ip.dst in { 192.0.2.0/24 }",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
},
"success": true,
"errors": [],
"messages": []
}JSON ボディのフィールドの詳細は JSON objects を参照してください。
この例は既存のルールを更新します。mode、scope、expression は更新できますが、program は変更できません。program を変えるには、ルールを削除して新しく作成します。
curl --request PATCH \
"https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/rules/{rule_id}" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"mode": "enabled"
}'{
"result": {
"id": "<RULE_ID>",
"program_id": "<PROGRAM_ID>",
"scope": "global",
"name": "global",
"mode": "enabled",
"expression": "",
"created_on": "<TIMESTAMP>",
"modified_on": "<TIMESTAMP>"
},
"success": true,
"errors": [],
"messages": []
}JSON ボディのフィールドの詳細は JSON objects を参照してください。
この例は既存のルールを削除します。
curl --request DELETE \
"https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/rules/{rule_id}" \
--header "Authorization: Bearer <API_TOKEN>"{
"result": null,
"success": true,
"errors": [],
"messages": []
}この例は、デバッグのためにプログラムを PCAP ファイルに対して実行します。API は、各パケットのプログラム判定を注釈した PCAP ファイルを返します。
リクエストボディには、バイナリ形式の PCAP ファイルを含める必要があります。API は入力 PCAP に基づいて IP ヘッダーのオフセットを自動検出します。自動検出を上書きするには、任意の ip_offset クエリパラメーターで、各パケット内の IP ヘッダーのオフセットバイト数を指定します(例: Ethernet フレームでは 14)。
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/magic/programmable_flow_protection/configs/programs/{program_id}/pcap" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/vnd.tcpdump.pcap" \
--data-binary "@/path/to/input.pcap" \
--output output.pcap出力 PCAP ファイルには入力ファイルと同じパケットが含まれますが、各パケットに注釈が付きます。 Packet Comment 注釈には、次が含まれることがあります。
- プログラムの戻り値:
CF_EBPF_PASSまたはCF_EBPF_DROP Ignored: 受信パケットが UDP でない場合Analytics tag: このパケットにプログラムが設定した、カスタムの network analytics タグ(ある場合)Challenge packet: プログラムからクライアントへ送出したチャレンジパケット(ある場合)