Hi, what’s the cleanest way to get run history? I ...
# ask-community
j
Hi, what’s the cleanest way to get run history? I am unable to figure out what credentials to pass to the REST API and also can’t access it with the SDK using task_run_history. The error is:
Copy code
AttributeError: 'Depends' object has no attribute 'session_context'
1
s
Hi @Jonah, to get run history via REST API:
Copy code
import requests

PREFECT_API_URL='<https://api.prefect.cloud/api/accounts/abc/workspaces/xyz>'
PREFECT_API_KEY="pnu_defghi"

headers = {"Authorization": f"Bearer {PREFECT_API_KEY}"}
endpoint = f"{PREFECT_API_URL}/ui/flow_runs/history"


response = <http://requests.post|requests.post>(url=endpoint, headers=headers)
print(response.status_code)
json = response.json()
print(json)
For `task_run_history`:
Copy code
import requests

PREFECT_API_URL='<https://api.prefect.cloud/api/accounts/abc/workspaces/xyz>'
PREFECT_API_KEY="pnu_defghi"

headers = {"Authorization": f"Bearer {PREFECT_API_KEY}"}
endpoint = f"{PREFECT_API_URL}/task_runs/history"

payload = {
    "history_start": "2021-04-20T00:00:00+00:00",
    "history_end": "2021-04-25T00:00:00+00:00",
    "history_interval_seconds": 86400,
}

response = <http://requests.post|requests.post>(endpoint, headers=headers, json=payload)
print(response.status_code)
json = response.json()
print(json)
Running the code variant you sent me, I ran it successfully. What is the response.json()?
j
404 {“detail”:“Not Found”}
s
Can you try to run the flow run history endpoint (code above) to see if this leads to the same error?
j
Still 404 error. The URL is
<https://api.prefect.cloud/api/accounts/270a9520-6cbd-4661-bab3-69a820b40820/workspace/f5171a9d-3e51-47b9-821b-7664eb56631f/ui/flow_runs/history>
s
Can you do a healthcheck? Please be sure to use this code below, rather than editing your existing code:
Copy code
import requests

PREFECT_API_URL='<https://api.prefect.cloud/api/accounts/abc/workspaces/xyz>'
PREFECT_API_KEY="pnu_ghijk"

headers = {"Authorization": f"Bearer {PREFECT_API_KEY}"}
endpoint = f"{PREFECT_API_URL}/health"

response = requests.get(url=endpoint, headers=headers)
print(response.status_code)
json = response.json()
print(json)
j
I got both to work. I had a few copy-paste-update-variables issues. Thank you so much for your help.
🚀 1
s
So glad you got things working!