How to create Transform Rules throught API

Here is the way to do it by using Cloudflare API from the Cloudflare Developer Docs:

1 Like

Thanks :slight_smile:

1 Like

@fritex I mistakenly tried to create a rule three times (due to incorrect parameter passing). And now I have such a picture: The rules were not created, but it is displayed as if they are Скриншот 25-11-2021 16:04:30.jpg. What does it mean?

And now i got error response:

exceeded maximum number of zone rulesets for phase ‘http_request_transform’

From the screenshot above, this are Page Rules, which are a bit different than Transform Rules.

In case you want to add/edit Page Rules using Cloudflare API, here are the docs:

Kindly, re-check and navigate to Rules → select Transform Rules - if you created them, you should see them here:

For Free plan, currently there is a limit of:

  • 3 Page Rules
  • 2 Transform Rules

@fritex hat does at mean? I cant see my 3 page rules…

@fritex By the way, in docs i cant find how to enable Argo Tiered Cache Скриншот 25-11-2021 17:54:17.jpg :frowning:
Can you help me plz? :slight_smile:

I remember there was a question about it recently how to enable it via the Cloudflare API.

May I recommend looking into the below post from @michael for a solution :wink:

Yes, good, thx :slight_smile:

1 Like

@fritex still actual… How to create Transform Rules throught API - #7 by user11534

May I ask what was the response code/result after creating Page Rules by using Cloudflare API?

Nevertheless, you might created Transform Rules rather than Page Rules via the API.

Did you checked if you were doing this for the required zone/domain, or maybe made them for some other and not for the one which you wanted, if so?

One time response error was:

json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded

Because my PHP-file encoding was windows-1251 and description of rule contains Cyrillic characters.

Next two times responses returns error Скриншот 25-11-2021 19:21:48.jpg (Because expression contains single quotes)

The third time (when my request was correct) i got error:

exceeded maximum number of zone rulesets for phase 'http_request_transform'

My correct code is:

public function addRewriteRuleForRobots(string $zoneId)
{
    return $this->adapter->post("zones/$zoneId/rulesets", [
        'kind' => 'zone',
        'name' => 'Замена robots.txt',
        'phase' => 'http_request_transform',
        'rules' => [
            [
                'action' => 'rewrite',
                'expression' => '(http.request.uri.path eq "/robots.txt")',
                'description' => 'Открываем сайт для индексации тем, кто купил Бесплатный Сайт От Гдебар.',
                'action_parameters' => [
                    'uri' => [
                        'path' => [
                            'value' => '/robots-free-site.txt'
                        ]
                    ]
                ]
            ]
        ]
    ]);
}

Important! I tried create Transform Rewrite Rule https://skr.sh/sBCg84CMlJM, but reduced limit of Page Rules :slight_smile: Скриншот 25-11-2021 19:21:50.jpg

After all manimulations, i tried to create Transform Rewrite Rule for another zone (domain) and all is ok, rule was created.

That is bug or feature? :smiley:

@fritex By the way, limit of Transform Rules actual for all my domains? Or 2 rules per domain?

Currently, 2 Transform Rules per domain (and 3 Page Rules per domain) - at least for a Free plan.

I really hope they will lift this limit up a bit, soon :slight_smile:

Great :slight_smile: But after this strange situation, now in all my domains i see this =\ https://skr.sh/sBCkbyGb1vo

@fritex dude?)

From the screenshot you provided, I do not understand, what is the issue?
What steps have you made?
What was the wanted result?

Free Plan - 2 Transform Rules, 3 Page Rules, where Transform Rule are not the same as Page Rules

I am sorry, but I might be missing something what is your point here.

Or, rather you created some rules via API and they are not applied to the specific domain (zone)?

You asked me

I totally answered in this post, what i done and what happened How to create Transform Rules throught API - #13 by osipovmn

I am sorry, but I am not sure if that’s a bug and am not able to provide the answer to it, as far as I haven’t used Cloudflare API to create/manage Transform and/or Page Rules so far.

Kindly and patiently wait for an reply from someone else regarding this.