```import requests query = """ mutation { crea...
# ask-community
a
Copy code
import requests

query = """
 mutation {
  create_flow_run(input: { flow_id: "9479ea34-f558-4616-8e64-50c7a508787d" }) {
    id
  }
}
"""

url = "<https://api.prefect.io>"
response = <http://requests.post|requests.post>(
    url, json={"query": query}, headers={"authorization": "Bearer your-api-key"}
)
print(response.status_code)
print(response.text)
I am using above python code to invoke flow in prefect cloud, i have given the value of Api key and changed the flow_id as well but when i run this file i get the following error 200 {"errors":[{"path":["create_flow_run"],"message":"Unauthenticated","extensions":{"code":"UNAUTHENTICATED"}}],"data":{"create_flow_run":null}}
k
Hey @Aqib Fayyaz, looks right to me. Can you try maybe using the Prefect Client with the same API Key and doing
Client().create_flow_run()
and see if that works?
a
can you please eleborate a bit more how can i fit it in my current example?
k
It wouldn’t fit in the current example. It would just be another approach to test the API Key
Copy code
from prefect.client.client import Client

client = Client(api_key = ...)
client.create_flow_run(flow_id=...)
Docs here
👍 2
a
Great this worked
one more thing can i catch the value returned by function in flow like
Copy code
val = client.create_flow_run(flow_id=...)
k
That would return the id. There is a
get_task_run_result
task that you can use. Just call the
run
method.
upvote 1
a
client = Client(api_key = "" ) data = client.create_flow_run(flow_id="") result = get_task_run_result.run(data,"hello_world-1") print(result) Traceback (most recent call last): File "prefClient.py", line 24, in <module> result = get_task_run_result.run(data,"hello_world-1") File "/home/aqib/.local/lib/python3.8/site-packages/prefect/tasks/prefect/flow_run.py", line 213, in get_task_run_result return task_run.get_result() File "/home/aqib/.local/lib/python3.8/site-packages/prefect/backend/task_run.py", line 77, in get_result self._assert_result_type_is_okay() File "/home/aqib/.local/lib/python3.8/site-packages/prefect/backend/task_run.py", line 164, in _assert_result_type_is_okay raise ValueError( ValueError: The task result has no
location
so the result cannot be loaded. This often means that your task result has not been configured or has been configured incorrectly
a
@Aqib Fayyaz you would need to configure Results on tasks from which you want to retrieve their return values prior to using those in the
get_task_run_result
task. You can find more information incl. examples here: https://docs.prefect.io/core/concepts/results.html#results
👀 1