API and Zone ID

For Workes & Pages, what is the name of the domain?

What is the error number?

400 Client Error

What is the error message?

bad requests for url

What is the issue or error you’re encountering

Im using python API calls to get WAF events.

What steps have you taken to resolve the issue?

I’ve used different tests of python code to get the WAF events, and then to list all Zones, but it states No WAF events found, and No zones found. The Zone ID is present and website is active, however the Zone ID is wrong and the API is Account level. Using the curl command to test the API when generated the token works, but Zone ID is the main issue. I’ve also tried using ‘Cloudflare’ module, but ‘Cloudflare’ module was not found, on the other hand ‘cloudflare’ was and when using cloudflare.cloudflare() it only raised more issues so I opted out for just the request module.

What are the steps to reproduce the issue?

import requests

def list_all_zones(api_token):
# Cloudflare API URL to list zones
url = “https://api.cloudflare.com/client/v4/zones

# Set up the headers, including the Authorization token
headers = {
    "Authorization": f"Bearer {api_token}",
    "Content-Type": "application/json"
}

# Make the GET request to Cloudflare API
response = requests.get(url, headers=headers)

# Check if the response status code is OK (200)
if response.status_code == 200:
    # Parse and return the JSON data
    zones = response.json().get("result", [])
    if zones:
        for zone in zones:
            print(f"Domain: {zone['name']}, Zone ID: {zone['id']}")
    else:
        print("No zones found.")
else:
    # Print the error if something goes wrong
    print(f"Error {response.status_code}: {response.text}")

Example usage

api_token = “your-api-token”
list_all_zones(api_token)

This is the code used, but still no zones found. Only one domain, no subdomains, is active and logging events daily, but unable to make API calls.

Does use command like curl produce the same result on the same API endpoint?