Skip to content
Start here

Deploy payment ruleset

client.Monetization.Rules.Update(ctx, params) (*MonetizationRuleCollection, error)
PUT/zones/{zone_id}/monetization/rules

Replaces the zone’s Payment Required ruleset with the submitted desired state. Submit an empty rules array to clear all payment rules.

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
ParametersExpand Collapse
params RuleUpdateParams
ZoneID param.Field[string]

Path param: The unique ID of the zone.

MonetizationRulesetInput param.Field[MonetizationRulesetInput]

Body param

ReturnsExpand Collapse
type MonetizationRuleCollection struct{…}

The zone’s payment rules. Mirrors the shape of the ruleset submitted to the deploy endpoint, so the response can be read back as the desired state.

The zone’s payment rules, in the order they are evaluated. Empty when the zone has no payment rules.

One of the following:
type MonetizationRuleMonetizationRulesMonetizationRuleInputFixedPrice struct{…}

Payment rule with a fixed price set at configuration time.

ID string

The server-assigned unique ID of the payment rule. Stable across full-ruleset replacements.

maxLength32
type MonetizationRuleMonetizationRulesMonetizationRuleInputOriginControlled struct{…}

Payment rule whose price the origin server sets dynamically. The rule carries no price and the price field must be omitted — any provided value, including “0”, is rejected because it would not be enforced.

ID string

The server-assigned unique ID of the payment rule. Stable across full-ruleset replacements.

maxLength32

Deploy payment ruleset

package main

import (
  "context"
  "fmt"

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

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  monetizationRuleCollection, err := client.Monetization.Rules.Update(context.TODO(), monetization.RuleUpdateParams{
    ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"),
    MonetizationRulesetInput: monetization.MonetizationRulesetInputParam{
      Rules: cloudflare.F([]monetization.MonetizationRuleInputUnionParam{monetization.MonetizationRuleInputFixedPriceParam{
        Address: cloudflare.F("0x1234567890abcdef1234567890abcdef12345678==="),
        Expression: cloudflare.F(`(http.request.uri.path eq "/premium" and http.request.method in {"GET" "POST"})`),
        Price: cloudflare.F("250000"),
        Scheme: cloudflare.F(monetization.MonetizationRuleInputFixedPriceSchemeExact),
      }}),
    },
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", monetizationRuleCollection.Rules)
}
{
  "errors": [
    {
      "code": 0,
      "message": "message"
    }
  ],
  "messages": [
    {
      "code": 0,
      "message": "message"
    }
  ],
  "result": {
    "rules": [
      {
        "address": "0x1234567890abcdef1234567890abcdef12345678===",
        "expression": "(http.request.uri.path eq \"/premium\" and http.request.method in {\"GET\" \"POST\"})",
        "price": "250000",
        "scheme": "exact",
        "id": "023e105f4ecef8ad9ca31a8372d0c353",
        "description": "Premium API endpoint",
        "enabled": true
      }
    ]
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 0,
      "message": "message"
    }
  ],
  "messages": [
    {
      "code": 0,
      "message": "message"
    }
  ],
  "result": {
    "rules": [
      {
        "address": "0x1234567890abcdef1234567890abcdef12345678===",
        "expression": "(http.request.uri.path eq \"/premium\" and http.request.method in {\"GET\" \"POST\"})",
        "price": "250000",
        "scheme": "exact",
        "id": "023e105f4ecef8ad9ca31a8372d0c353",
        "description": "Premium API endpoint",
        "enabled": true
      }
    ]
  },
  "success": true
}