Skip to content

Instantly share code, notes, and snippets.

@Suzhou65
Last active September 21, 2025 16:45
Show Gist options
  • Select an option

  • Save Suzhou65/cf63b430bfc44c03a3b1fbe2af10d6a9 to your computer and use it in GitHub Desktop.

Select an option

Save Suzhou65/cf63b430bfc44c03a3b1fbe2af10d6a9 to your computer and use it in GitHub Desktop.
Verify API Token for CloudFlare API

First, logged in the Cloudflare Dashboard, and go to User Profile, choice API Tokens, generated API Token.

Then, once successfully generated, the token secret is only shown once. Make sure to copy the secret to a secure place. Replace the API Token at BearerAuth.

import json
import requests

cloudflare_verify = "https://api.cloudflare.com/client/v4/user/tokens/verify"
BearerAuth = "API Token"
AuthMail = "Auth Mail"

headers = {"Authorization":BearerAuth, "X-Auth-Email":AuthMail, "Content-Type":"application/json"}
cloudflare_respon = requests.get(cloudflare_verify, headers=headers)

if cloudflare_respon.status_code == 200:
    print("Ok")
else:
    print(cloudflare_respon.status_code)
    
json_data = json.loads(cloudflare_respon.text)

If the API Token activated successfully, the result will print.

Ok

Then, the JSON data(json_data) it respon will be like this:

{'result': {'id': 'c995e0fb6f249ac0e9a7ed90fc678e4b', 'status': 'active'},
 'success': True,
 'errors': [],
 'messages': [{'code': 10000,
   'message': 'This API Token is valid and active',
   'type': None}]}
@Hunk13
Copy link

Hunk13 commented Aug 7, 2025

Should use

auth_key = "Bearer API Token"

instead

auth_key = "API Token"

link

@Suzhou65
Copy link
Author

Thanks for reminding, This Gist has been updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment