Skip to content
Start here

Create a User Agent Blocking rule

client.Firewall.UARules.New(ctx, params) (*UARuleNewResponse, error)
POST/zones/{zone_id}/firewall/ua_rules

Creates a new User Agent Blocking rule in a zone.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Firewall Services Write
ParametersExpand Collapse
params UARuleNewParams
ZoneID param.Field[string]

Path param: Defines an identifier.

maxLength32
Configuration param.Field[UARuleNewParamsConfiguration]

Body param

Target UARuleNewParamsConfigurationTargetoptional

The configuration target. You must set the target to ua when specifying a user agent in the rule.

Value stringoptional

the user agent to exactly match

Mode param.Field[UARuleNewParamsMode]

Body param: The action to apply to a matched request.

const UARuleNewParamsModeBlock UARuleNewParamsMode = "block"
const UARuleNewParamsModeChallenge UARuleNewParamsMode = "challenge"
const UARuleNewParamsModeWhitelist UARuleNewParamsMode = "whitelist"
const UARuleNewParamsModeJSChallenge UARuleNewParamsMode = "js_challenge"
const UARuleNewParamsModeManagedChallenge UARuleNewParamsMode = "managed_challenge"
Description param.Field[string]optional

Body param: An informative summary of the rule. This value is sanitized and any tags will be removed.

maxLength1024
Paused param.Field[bool]optional

Body param: When true, indicates that the rule is currently paused.

ReturnsExpand Collapse
type UARuleNewResponse struct{…}
ID stringoptional

The unique identifier of the User Agent Blocking rule.

maxLength32
Configuration UARuleNewResponseConfigurationoptional

The configuration object for the current rule.

Target stringoptional

The configuration target for this rule. You must set the target to ua for User Agent Blocking rules.

Value stringoptional

The exact user agent string to match. This value will be compared to the received User-Agent HTTP header value.

Description stringoptional

An informative summary of the rule.

maxLength1024
Mode UARuleNewResponseModeoptional

The action to apply to a matched request.

maxLength12
One of the following:
const UARuleNewResponseModeBlock UARuleNewResponseMode = "block"
const UARuleNewResponseModeChallenge UARuleNewResponseMode = "challenge"
const UARuleNewResponseModeJSChallenge UARuleNewResponseMode = "js_challenge"
const UARuleNewResponseModeManagedChallenge UARuleNewResponseMode = "managed_challenge"
Paused booloptional

When true, indicates that the rule is currently paused.

Create a User Agent Blocking rule

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/firewall"
  "github.com/cloudflare/cloudflare-go/option"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  uaRule, err := client.Firewall.UARules.New(context.TODO(), firewall.UARuleNewParams{
    ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Configuration: cloudflare.F(firewall.UARuleNewParamsConfiguration{

    }),
    Mode: cloudflare.F(firewall.UARuleNewParamsModeChallenge),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", uaRule.ID)
}
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "id": "372e67954025e0ba6aaa6d586b9e0b59",
    "configuration": {
      "target": "ua",
      "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4"
    },
    "description": "Prevent access from abusive clients identified by this User Agent to mitigate a DDoS attack",
    "mode": "js_challenge",
    "paused": false
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "id": "372e67954025e0ba6aaa6d586b9e0b59",
    "configuration": {
      "target": "ua",
      "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4"
    },
    "description": "Prevent access from abusive clients identified by this User Agent to mitigate a DDoS attack",
    "mode": "js_challenge",
    "paused": false
  },
  "success": true
}