Is it possible to start a run just using curl?
# ask-community
a
Is it possible to start a run just using curl?
Given I know the project, the flow name and I have the cloud token
k
I believe it is, but I don’t know the exact format. This is the Python request version which should get you started:
Copy code
query = """query {flow{ name, id}}"""


url = '<https://api.prefect.io>'
r = <http://requests.post|requests.post>(url, json={'query': query}, headers={"authorization": "Bearer MY_TOKEN_HERE"})
print(r.status_code)
print(r.text)
use the
create_flow_run
mutation instead of course
a
@Alvaro Durán Tovar given that you asked about curl, it seems like you want to trigger a flow run using the CLI. What would be even easier than curl would be to leverage the Prefect CLI. If your flow is already registered, you could use:
Copy code
prefect run --name your-unique-flow-name --watch
The extra --watch flag allows you to see the flow run logs directly in the terminal. If your flow is not registered, you can accomplish the same providing a path:
Copy code
prefect run -p /path/to/flow.py
a
I know, I'm just trying to find a way to setup a script with minimal requirements to trigger runs
To trigger metaflow runs actually
a
Can you share more about your use case? How would you like this script to be triggered - via CLI command, API call? Where would it run? Is it something you would trigger based on some event, or should it run on schedule?
a
Sure So I want to improve our UX when using metaflow, with kubernetes not with batch. Metaflow doesn't have a detached mode which means you must keep your computer open otherwise the master process dies. I want to have a script to run metaflow flows from git repos, given the correct config trigger the runs on kubernetes. This is already running nicely, giving a repo URL, commit and params starts a pod on k8s running metaflow on the cloud instead of from the developer computer.
Now I want to call that prefect flow (everything is orchestrated by prefect) from a script as easy as possible. I can use prefect cli, but if I want to avoid it (for example users might have different conda envs, with without prefect, sometimes might not be available)
Therefore if I can make a curl request everything should run quite nicely
No based on event, no based on schedule Will run on demand let's say I just want to off load running metaflow to the cloud
a
@Alvaro Durán Tovar this should work with curl - provide your flow ID and API key:
Copy code
curl '<https://api.prefect.io>' \
  -X POST \
  -H 'content-type: application/json' \
	-H 'authorization: Bearer XXX' \
  --data '{
    "query":"mutation {create_flow_run(input: { flow_id: \"05f1872f-44b3-443a-bbd4-0a1198351cad\" }) {id}}"
  }'
a
Nice thanks
Is the flow id needed? I guess I can find a way to fetch it first with another query
k
Yeah you need it because that mutation specifically takes a flow_id. Seems like you’d need to query using the
flow
route
a
In that case, you’ll likely won’t be using curl for this right? Probably a Python script with requests would be easier:
Copy code
import requests

query = """
 mutation {
  create_flow_run(input: { flow_id: "e9827ce5-f686-4183-b3e9-c629d63d7510" }) {
    id
  }
}
"""

url = "<https://api.prefect.io>"
response = <http://requests.post|requests.post>(
    url, json={"query": query}, headers={"authorization": "Bearer XXX"}
)
print(response.status_code)
print(response.text)
You can use the same requests syntax to query for flow ID.
a
👌
c
I think you may want to use the version_group_id instead? if a new flow version is updated, the flow_id seems to change. and I tried with flow_group_id and that didn't seem to work, but it seems to run fine ( pick up the most recent version ) using the version_group_id. Ideally create_flow_run could take project and flow name....
k
The
create_flow_run
task takes in project and flow name if you can install Prefect to trigger the new Flow run. This setup above is if you can’t install Prefect on the machine
So you can do
create_flow_run.run(flow_name="name", project_name="project")
to run the Python underneath
Or the CLI with
prefect run --project some_name --name flow_name
c
right, but not by using the https graphql api. 🙁