For Workes & Pages, what is the name of the domain?
None
What is the error number?
400
What is the error message?
400 Bad Request
What is the issue or error you’re encountering
Getting error when trying to create gateway dns rule using api
What steps have you taken to resolve the issue?
Double checked my id and token if it is correct. Tried searching solution in the internet with no luck
What are the steps to reproduce the issue?
I want to create a gateway dns rule to block domains resolving to a specified country.
async function createDNSRule(accountId) {
// Cloudflare API endpoint for creating DNS rules (using account ID)
const endpoint = https://api.cloudflare.com/client/v4/accounts/${accountId}/gateway/rules
;
// Define the DNS rule (same as before)
const rule = {
“action”: “block”,
“description”: “Block DNS queries”,
“expression”: “dns.dst.geo.country in {"RU", "CN", "IR"}”,
“paused”: false,
“priority”: 1,
};
// Make the API request (using API token)
try {
const response = await fetch(endpoint, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${your_cloudflare_api_token}
, // Use API token for authorization
},
body: JSON.stringify(rule),
});
if (response.ok) {
const data = await response.json();
console.log('DNS rule created successfully:', data);
} else {
console.error('Error creating DNS rule:', response.status, response.statusText);
}
} catch (error) {
console.error(‘Error creating DNS rule:’, error);
}
}
// Replace with your actual account ID
const accountId = ‘My ID’;
// Replace with your actual API token
const your_cloudflare_api_token = ‘My Token’;
// Call the function to create the DNS rule
createDNSRule(accountId);