Python SDK PUT Ruleset

Hi Cloudflare community,

I am trying to update the expression of a WAF custom rule but I am having some difficulties.
I first tried to update the rule using the zone firewall rules PUT method but I received this error message.

CloudFlareAPIError(99998, 'invalid action provided: creating or modifying rules with the skip action is only available via Rulesets API')

Consequently, I tried to do the same thing with the rulesets API but I am confused how to get one single rule and update it.
Using the Python SDK,

    cf_rules = cf.zones.rulesets(
      cf_zone_id,
      cf_rulesets_id,
  )

Returns one rulesets containing many rules.
I would like to update only 1 rule expression from that ruleset.

Thank you in advance.

Hello there,

To update a single rule expression in a ruleset using Cloudflare’s Python SDK, please try following these steps below:

  • Retrieve the ruleset using the cf.zones.rulesets.get method.
  • Iterate through the rules in the ruleset to find the rule you want to update.
  • Modify the ‘expression’ field of the rule.
  • Update the ruleset with the modified rule using the cf.zones.rulesets.update method.

Here is a code snippet to help you:

# Retrieve the ruleset
ruleset = cf.zones.rulesets.get(cf_zone_id, cf_rulesets_id)

# Find the rule and update the expression
for rule in ruleset['result']['rules']:
    if rule['id'] == "your_rule_id":
        rule['expression'] = "your_updated_expression"
        break

# Update the ruleset with the modified rule
cf.zones.rulesets.update(cf_zone_id, cf_rulesets_id, data=ruleset['result'])

Replace your_rule_id and your_updated_expression with the appropriate values.

1 Like

Thank you very much Luis, we could get this working with your answer.

Best regards,

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.