Skip to content
Start here

Create an IP Access rule

client.Firewall.AccessRules.New(ctx, params) (*AccessRuleNewResponse, error)
POST/{accounts_or_zones}/{account_or_zone_id}/firewall/access_rules/rules

Creates a new IP Access rule for an account or zone. The rule will apply to all zones in the account or zone.

Note: To create an IP Access rule that applies to a single zone, refer to the IP Access rules for a zone endpoints.

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)
Account Firewall Access Rules Write
ParametersExpand Collapse
params AccessRuleNewParams
Configuration param.Field[AccessRuleNewParamsConfiguration]

Body param: The rule configuration.

type AccessRuleIPConfiguration struct{…}
Target AccessRuleIPConfigurationTargetoptional

The configuration target. You must set the target to ip when specifying an IP address in the rule.

Value stringoptional

The IP address to match. This address will be compared to the IP address of incoming requests.

type IPV6Configuration struct{…}
Target IPV6ConfigurationTargetoptional

The configuration target. You must set the target to ip6 when specifying an IPv6 address in the rule.

Value stringoptional

The IPv6 address to match.

type AccessRuleCIDRConfiguration struct{…}
Target AccessRuleCIDRConfigurationTargetoptional

The configuration target. You must set the target to ip_range when specifying an IP address range in the rule.

Value stringoptional

The IP address range to match. You can only use prefix lengths /16 and /24 for IPv4 ranges, and prefix lengths /32, /48, and /64 for IPv6 ranges.

type ASNConfiguration struct{…}
Target ASNConfigurationTargetoptional

The configuration target. You must set the target to asn when specifying an Autonomous System Number (ASN) in the rule.

Value stringoptional

The AS number to match.

type CountryConfiguration struct{…}
Target CountryConfigurationTargetoptional

The configuration target. You must set the target to country when specifying a country code in the rule.

Value stringoptional

The two-letter ISO-3166-1 alpha-2 code to match. For more information, refer to IP Access rules: Parameters.

Mode param.Field[AccessRuleNewParamsMode]

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

const AccessRuleNewParamsModeBlock AccessRuleNewParamsMode = "block"
const AccessRuleNewParamsModeChallenge AccessRuleNewParamsMode = "challenge"
const AccessRuleNewParamsModeWhitelist AccessRuleNewParamsMode = "whitelist"
const AccessRuleNewParamsModeJSChallenge AccessRuleNewParamsMode = "js_challenge"
const AccessRuleNewParamsModeManagedChallenge AccessRuleNewParamsMode = "managed_challenge"
AccountID param.Field[string]optional

Path param: The Account ID to use for this endpoint. Mutually exclusive with the Zone ID.

ZoneID param.Field[string]optional

Path param: The Zone ID to use for this endpoint. Mutually exclusive with the Account ID.

Notes param.Field[string]optional

Body param: An informative summary of the rule, typically used as a reminder or explanation.

ReturnsExpand Collapse
type AccessRuleNewResponse struct{…}
ID string

The unique identifier of the IP Access rule.

maxLength32
AllowedModes []AccessRuleNewResponseAllowedMode

The available actions that a rule can apply to a matched request.

One of the following:
const AccessRuleNewResponseAllowedModeBlock AccessRuleNewResponseAllowedMode = "block"
const AccessRuleNewResponseAllowedModeChallenge AccessRuleNewResponseAllowedMode = "challenge"
const AccessRuleNewResponseAllowedModeWhitelist AccessRuleNewResponseAllowedMode = "whitelist"
const AccessRuleNewResponseAllowedModeJSChallenge AccessRuleNewResponseAllowedMode = "js_challenge"
const AccessRuleNewResponseAllowedModeManagedChallenge AccessRuleNewResponseAllowedMode = "managed_challenge"
Configuration AccessRuleNewResponseConfiguration

The rule configuration.

One of the following:
type AccessRuleIPConfiguration struct{…}
Target AccessRuleIPConfigurationTargetoptional

The configuration target. You must set the target to ip when specifying an IP address in the rule.

Value stringoptional

The IP address to match. This address will be compared to the IP address of incoming requests.

type IPV6Configuration struct{…}
Target IPV6ConfigurationTargetoptional

The configuration target. You must set the target to ip6 when specifying an IPv6 address in the rule.

Value stringoptional

The IPv6 address to match.

type AccessRuleCIDRConfiguration struct{…}
Target AccessRuleCIDRConfigurationTargetoptional

The configuration target. You must set the target to ip_range when specifying an IP address range in the rule.

Value stringoptional

The IP address range to match. You can only use prefix lengths /16 and /24 for IPv4 ranges, and prefix lengths /32, /48, and /64 for IPv6 ranges.

type ASNConfiguration struct{…}
Target ASNConfigurationTargetoptional

The configuration target. You must set the target to asn when specifying an Autonomous System Number (ASN) in the rule.

Value stringoptional

The AS number to match.

type CountryConfiguration struct{…}
Target CountryConfigurationTargetoptional

The configuration target. You must set the target to country when specifying a country code in the rule.

Value stringoptional

The two-letter ISO-3166-1 alpha-2 code to match. For more information, refer to IP Access rules: Parameters.

Mode AccessRuleNewResponseMode

The action to apply to a matched request.

One of the following:
const AccessRuleNewResponseModeBlock AccessRuleNewResponseMode = "block"
const AccessRuleNewResponseModeChallenge AccessRuleNewResponseMode = "challenge"
const AccessRuleNewResponseModeWhitelist AccessRuleNewResponseMode = "whitelist"
const AccessRuleNewResponseModeJSChallenge AccessRuleNewResponseMode = "js_challenge"
const AccessRuleNewResponseModeManagedChallenge AccessRuleNewResponseMode = "managed_challenge"
CreatedOn Timeoptional

The timestamp of when the rule was created.

formatdate-time
ModifiedOn Timeoptional

The timestamp of when the rule was last modified.

formatdate-time
Notes stringoptional

An informative summary of the rule, typically used as a reminder or explanation.

Scope AccessRuleNewResponseScopeoptional

All zones owned by the user will have the rule applied.

ID stringoptional

Defines an identifier.

maxLength32
Email stringoptional

The contact email address of the user.

maxLength90
Type AccessRuleNewResponseScopeTypeoptional

Defines the scope of the rule.

One of the following:
const AccessRuleNewResponseScopeTypeUser AccessRuleNewResponseScopeType = "user"
const AccessRuleNewResponseScopeTypeOrganization AccessRuleNewResponseScopeType = "organization"

Create an IP Access 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"),
  )
  accessRule, err := client.Firewall.AccessRules.New(context.TODO(), firewall.AccessRuleNewParams{
    Configuration: cloudflare.F[firewall.AccessRuleNewParamsConfigurationUnion](firewall.AccessRuleIPConfigurationParam{

    }),
    Mode: cloudflare.F(firewall.AccessRuleNewParamsModeChallenge),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", accessRule.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": "92f17202ed8bd63d69a66b86a49a8f6b",
    "allowed_modes": [
      "whitelist",
      "block",
      "challenge",
      "js_challenge",
      "managed_challenge"
    ],
    "configuration": {
      "target": "ip",
      "value": "198.51.100.4"
    },
    "mode": "challenge",
    "created_on": "2014-01-01T05:20:00.12345Z",
    "modified_on": "2014-01-01T05:20:00.12345Z",
    "notes": "This rule is enabled because of an event that occurred on date X.",
    "scope": {
      "id": "023e105f4ecef8ad9ca31a8372d0c353",
      "email": "user@example.com",
      "type": "user"
    }
  },
  "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": "92f17202ed8bd63d69a66b86a49a8f6b",
    "allowed_modes": [
      "whitelist",
      "block",
      "challenge",
      "js_challenge",
      "managed_challenge"
    ],
    "configuration": {
      "target": "ip",
      "value": "198.51.100.4"
    },
    "mode": "challenge",
    "created_on": "2014-01-01T05:20:00.12345Z",
    "modified_on": "2014-01-01T05:20:00.12345Z",
    "notes": "This rule is enabled because of an event that occurred on date X.",
    "scope": {
      "id": "023e105f4ecef8ad9ca31a8372d0c353",
      "email": "user@example.com",
      "type": "user"
    }
  },
  "success": true
}