タグ操作はすべて 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"API は部分更新に対応していません。PUT は常にすべてのタグを置き換えます。既存タグを消さずにタグを追加するには、GET、マージ、PUT の流れを使います。
- 現在のタグを
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"}}}- 新しいタグを、既存セットへローカルでマージします。
{
"environment": "production",
"team": "platform",
"cost-center": "engineering"
}- マージ済みのタグセット全体を
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"
}
}'同じ 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 は、リソースからすべてのタグを外したいときだけ使います(廃止時など)。