How to create new page rule from nodejs

I am getting error in this below code
cf.pageRules.add(‘sdgshdnjh98…’, pageRuleSettings)
TypeError: Cannot read properties of undefined (reading ‘add’)

I want to create new page rul from my nodejs

const Cloudflare = require(‘cloudflare’);

const cf = new Cloudflare({
apiEmail: ‘@gmail.com’, // This is the default and can be omitted
apiKey: '
*’, // This is the default and can be omitted
});

// Define the page rule settings
const pageRuleSettings = {
targets: [
{
target: ‘url’,
constraint: {
operator: ‘matches’,
value: ‘mydomain.com’, // Enter your URL pattern here
},
},
],
actions: [
{
id: ‘forwarding_url’,
value: {
url: ‘https://www.mydomain.com’, // Enter your destination URL here
status_code: 302, // Choose 301 or 302
},
},
],
};

// Create a page rule
cf.pageRules.add(‘1ea2b017834086681c…’, pageRuleSettings)
.then(response => {
console.log(‘Page rule created successfully:’, response.result);
})
.catch(error => {
console.error(‘Error creating page rule:’, error);
});

1 Like

Hey there,

I don’t have personal experience with tools using NodeJS to configure Cloudflare. That being said, could you try creating a Redirect Rule instead of a Page Rule and see if that is able to fix the issue you are having? Page Rules are currently deprecated and will soon be no longer available.