Skip to content
Start here

Create a content security rule

client.PageShield.Policies.New(ctx, params) (*PolicyNewResponse, error)
POST/zones/{zone_id}/page_shield/policies

Creates a rule that applies a client-side security action when its filter expression matches.

Security

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)
Page ShieldDomain Page ShieldZone Settings Write
ParametersExpand Collapse
params PolicyNewParams
ZoneID param.Field[string]

Path param: Identifier

maxLength32
Action param.Field[PolicyNewParamsAction]

Body param: The action to take if the expression matches

const PolicyNewParamsActionAllow PolicyNewParamsAction = "allow"
const PolicyNewParamsActionLog PolicyNewParamsAction = "log"
const PolicyNewParamsActionAddReportingDirectives PolicyNewParamsAction = "add_reporting_directives"
Description param.Field[string]

Body param: A description for the policy

Enabled param.Field[bool]

Body param: Whether the policy is enabled

Expression param.Field[string]

Body param: The expression which must match for the policy to be applied, using the Cloudflare Firewall rule expression syntax

Value param.Field[string]

Body param: The policy which will be applied

ReturnsExpand Collapse
type PolicyNewResponse struct{…}
ID string

Identifier

maxLength32
Action PolicyNewResponseAction

The action to take if the expression matches

One of the following:
const PolicyNewResponseActionAllow PolicyNewResponseAction = "allow"
const PolicyNewResponseActionLog PolicyNewResponseAction = "log"
const PolicyNewResponseActionAddReportingDirectives PolicyNewResponseAction = "add_reporting_directives"
Description string

A description for the policy

Enabled bool

Whether the policy is enabled

Expression string

The expression which must match for the policy to be applied, using the Cloudflare Firewall rule expression syntax

Value string

The policy which will be applied

Create a content security rule

package main

import (
  "context"
  "fmt"

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

func main() {
  client := cloudflare.NewClient(
    option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
    option.WithAPIEmail("user@example.com"),
  )
  policy, err := client.PageShield.Policies.New(context.TODO(), page_shield.PolicyNewParams{
    ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Action: cloudflare.F(page_shield.PolicyNewParamsActionAllow),
    Description: cloudflare.F("Checkout page CSP policy"),
    Enabled: cloudflare.F(true),
    Expression: cloudflare.F(`ends_with(http.request.uri.path, "/checkout")`),
    Value: cloudflare.F("script-src 'none';"),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", policy.ID)
}
{
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353",
    "action": "allow",
    "description": "Checkout page CSP policy",
    "enabled": true,
    "expression": "ends_with(http.request.uri.path, \"/checkout\")",
    "value": "script-src 'none';"
  },
  "success": true,
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ]
}
Returns Examples
{
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353",
    "action": "allow",
    "description": "Checkout page CSP policy",
    "enabled": true,
    "expression": "ends_with(http.request.uri.path, \"/checkout\")",
    "value": "script-src 'none';"
  },
  "success": true,
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ]
}