Skip to content

Instantly share code, notes, and snippets.

@Suzhou65
Last active September 5, 2024 10:03
Show Gist options
  • Save Suzhou65/cf63b430bfc44c03a3b1fbe2af10d6a9 to your computer and use it in GitHub Desktop.
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 auth_key.

import json
import requests

cloudflare_verify = "https://api.cloudflare.com/client/v4/user/tokens/verify"
auth_key = "API Token"
headers = {'Authorization': auth_key, '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}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment