Worker API python example give error

import os
import requests

Correctly set this as your Cloudflare account ID.


Assuming you’ve set an environment variable named CLOUDFLARE_AUTH_TOKEN with your actual token.


Validate that the AUTH_TOKEN was retrieved correctly.

if not AUTH_TOKEN:
print(“Authorization token not found. Ensure the CLOUDFLARE_AUTH_TOKEN environment variable is set.”)
exit(1) # Exit the script if the token is not set.

prompt = “Tell me all about PEP-8”

response =
headers={“Authorization”: f"Bearer {AUTH_TOKEN}"},
“messages”: [
{“role”: “system”, “content”: “You are a friendly assistant”},
{“role”: “user”, “content”: prompt}

It’s good practice to check the response status code before attempting to parse the JSON.

if response.status_code != 200:
print(f"Request failed with status code {response.status_code}: {response.text}")
result = response.json()
# Now, let’s check if the keys exist and the ‘response’ is not empty.
if result.get(‘result’) and ‘response’ in result[‘result’]:
print(“Failed to get a valid response”)

it give back error - >
Request failed with status code 400: {“success”:false,“errors”:[{“code”:10000,“message”:“Authentication error”}]}

Process finished with exit code 0

what is it mean what is the issue?

Hi @shai5

The error code 400 with the message “Authentication error” indicates that there is a problem with the authentication token you are using. Here are the steps to troubleshoot this issue:

  1. Double-check that your CLOUDFLARE_AUTH_TOKEN is set correctly in your environment variables.
  2. Ensure that the token has the necessary permissions to access the Cloudflare Worker API.
  3. Verify that the ACCOUNT_ID is correct and corresponds to the account associated with the auth token.
  4. Make sure that the token has not expired or been revoked.

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