Skip to content

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

タグの管理

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

タグ操作はすべて Tagging API を使います。認証には、適切な権限を持つ アカウント API トークン またはユーザー API トークンが必要です。

リソースにタグを設定する

アカウントレベルのリソースにタグを設定するには PUT を使います。この操作は、そのリソース上の既存タグをすべて置き換えます。

curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tags" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "resource_type": "worker",
    "resource_id": "'"$RESOURCE_ID"'",
    "tags": {
      "environment": "production",
      "team": "platform",
      "cost-center": "engineering"
    }
  }'

ゾーンレベルのリソースでは、ゾーンエンドポイントを使います。

curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/tags" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "resource_type": "zone",
    "resource_id": "'"$ZONE_ID"'",
    "tags": {
      "environment": "production",
      "customer": "acme-corp"
    }
  }'

一部のリソースタイプでは追加フィールドが必要です。詳細は 対応リソースタイプ を参照してください。

リソースのタグを取得する

特定リソースのタグを取得します。

curl -X GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tags?resource_type=worker&resource_id=$RESOURCE_ID" \
  -H "Authorization: Bearer $API_TOKEN"

タグを 1 つ追加する

API は部分更新に対応していません。PUT は常にすべてのタグを置き換えます。既存タグを消さずにタグを追加するには、GET、マージ、PUT の流れを使います。

  1. 現在のタグを GET します。
curl -X GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tags?resource_type=worker&resource_id=$RESOURCE_ID" \
  -H "Authorization: Bearer $API_TOKEN"

# Response: {"result": {"tags": {"environment": "production", "team": "platform"}}}
  1. 新しいタグを、既存セットへローカルでマージします。
{
  "environment": "production",
  "team": "platform",
  "cost-center": "engineering"
}
  1. マージ済みのタグセット全体を PUT します。
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tags" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "resource_type": "worker",
    "resource_id": "'"$RESOURCE_ID"'",
    "tags": {
      "environment": "production",
      "team": "platform",
      "cost-center": "engineering"
    }
  }'

タグを 1 つ削除する

同じ GET、マージ、PUT の流れを使います。PUT の前に、削除したいタグをセットから除きます。

すべてのタグを削除する

リソースからすべてのタグを外すには、次を実行します。

curl -X DELETE "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/tags" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "resource_type": "worker",
    "resource_id": "'"$RESOURCE_ID"'"
  }'

成功時は 204 No Content が返ります。DELETE は、リソースからすべてのタグを外したいときだけ使います(廃止時など)。

役に立ちましたか?