Hey lent — I'll look into this and let

Hey lent — I'll look into this and let you know what I find out.
6 Replies
Brandon
BrandonOP3mo ago
I'm unable to recreate, it's working as expected for me, if I delete a key it no longer works. What steps did you use to delete the key?
lent20254
lent202543mo ago
Hi Brandon. Thank you for your help. @Brandon On the My Profile page I clicked ... button next to it and clicked delete. On the My Profile>API Tokens page I see no tokens listed. From the dashboard going under Manage Account>AccountAPI Tokens I see no tokens listed. However I can confirm the key still works. I am not able to reproduce it either. But I am able to show I have no keys in the UI, the key still works, and I am able to prove the key still works by querying my D1 database and getting results.
Brandon
BrandonOP3mo ago
any chance you are using the global api key? could you re-roll/change it and see if you can still successfully query your db?
lent20254
lent202543mo ago
Yeah. I just rolled them and it is still working. I scoped the api token to only have D1 access and trying to do something else still fails. So that part is still being enforced. Is it helpful if I share my account ID?
Brandon
BrandonOP3mo ago
When you say it's still working do you mean you are creating a new instance and it is able to call d1 using that api key (via fetch and not the service binding). For the record step results are cached. So it might look like it's working when it's now.
lent20254
lent202543mo ago
# if requests isn’t installed, uncomment:
# !pip install requests

import requests
import json

# — your credentials —
ACCOUNT_ID = ""
DATABASE_ID = ""
API_TOKEN = ""

# — correct endpoint (singular “database”) —
endpoint = (
"https://api.cloudflare.com/client/v4"
f"/accounts/{ACCOUNT_ID}/d1/database/{DATABASE_ID}/query"
)

headers = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json",
}

# — list only user tables —
sql = """
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name NOT LIKE 'sqlite_%'
ORDER BY name;
"""

payload = {"sql": sql}

resp = requests.post(endpoint, headers=headers, json=payload)

# — print raw response for debugging —
print("HTTP status code:", resp.status_code)
print("Response body:")
print(resp.text)

# — then attempt JSON decode safely —
try:
data = resp.json()
print("\nDecoded JSON:")
print(json.dumps(data, indent=2))
except ValueError as e:
print("\nFailed to parse JSON:", e)
# stop here so you can inspect resp.text
raise

# — parse out the table names —
if data.get("success"):
# Cloudflare returns result as a list of QueryResult objects
qr = data.get("result", [])
if qr and isinstance(qr, list):
rows = qr[0].get("results", [])
if rows:
print("\nTables found:")
for row in rows:
print(" -", row.get("name"))
else:
print("\nNo user tables found in the database.")
else:
print("\nUnexpected JSON structure:", qr)
else:
print("\nAPI error details:", data.get("errors"))
# if requests isn’t installed, uncomment:
# !pip install requests

import requests
import json

# — your credentials —
ACCOUNT_ID = ""
DATABASE_ID = ""
API_TOKEN = ""

# — correct endpoint (singular “database”) —
endpoint = (
"https://api.cloudflare.com/client/v4"
f"/accounts/{ACCOUNT_ID}/d1/database/{DATABASE_ID}/query"
)

headers = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json",
}

# — list only user tables —
sql = """
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name NOT LIKE 'sqlite_%'
ORDER BY name;
"""

payload = {"sql": sql}

resp = requests.post(endpoint, headers=headers, json=payload)

# — print raw response for debugging —
print("HTTP status code:", resp.status_code)
print("Response body:")
print(resp.text)

# — then attempt JSON decode safely —
try:
data = resp.json()
print("\nDecoded JSON:")
print(json.dumps(data, indent=2))
except ValueError as e:
print("\nFailed to parse JSON:", e)
# stop here so you can inspect resp.text
raise

# — parse out the table names —
if data.get("success"):
# Cloudflare returns result as a list of QueryResult objects
qr = data.get("result", [])
if qr and isinstance(qr, list):
rows = qr[0].get("results", [])
if rows:
print("\nTables found:")
for row in rows:
print(" -", row.get("name"))
else:
print("\nNo user tables found in the database.")
else:
print("\nUnexpected JSON structure:", qr)
else:
print("\nAPI error details:", data.get("errors"))
This is how I am testing "working" if I create a new table in the database through the dashboard Ui and run this code I see the new table. Thank you again for your help! Since I cant reproduce the issue with another token I am fine just wiping every trace of this key from my project and then forgetting about it, I just wanted to understand the issue since as far as I can tell this should not be happening.

Did you find this page helpful?