所有标签操作均使用 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"
}
}'对于 zone 级资源,使用 zone 端点:
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"
# 响应:{"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。