Hi all, i wanna create a api, that use to call to ...
# ask-community
a
Hi all, i wanna create a api, that use to call to execute a task in prefect. How to do that? thanks all
a
@Anh Nguyen do you want to call an external API from a Prefect task, or do you want to kick off a Prefect flow from some API?
a
yes! that's right. thanks you. Could you tell me to do that ?
a
1. do you want to call an external API from a Prefect task? 2. do you want to kick off a Prefect flow from some API? which one is your use case, @Anh Nguyen?
a
yes i want to kick off a Prefect flow from some API
a
you can use the following GraphQL mutation:
Copy code
import requests

query = """
 mutation {
  create_flow_run(input: { flow_id: "your-flow-id" }) {
    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 need to set an API key instead of XXX and your flow ID.
a
yes! In the request, can i pass parameter to flow and using the parameter in processing flow ?
a
Perhaps this
create_flow_run
task is what you’re looking for? Can you describe your use case?
Copy code
from prefect.tasks.prefect import create_flow_run

with Flow("FLOW_NAME",) as flow:
    extract_load_id = create_flow_run(
        flow_name="your-flow-name",
        project_name="PROJECT_NAME",
        parameters={"key": "value"},
    )
@Anh Nguyen if you would want to use requests for that:
Copy code
import requests

create_mutation = """
mutation($input: createFlowRunInput!){
    createFlowRun(input: $input){
        flow_run{
            id
        }
    }
}
"""

inputs = dict(
    versionGroupId="339c86be-5c1c-48f0-b8d3-fe57654afe22", parameters=dict(x=6)
)
response = <http://requests.post|requests.post>(
    url="<https://api.prefect.io>",
    json=dict(query=create_mutation, variables=dict(input=inputs)),
    headers=dict(authorization=f"Bearer {API_KEY}"),
)
print(response.status_code)
print(response.text)
a
i want to use the value is got from request that will be insert to db
Copy code
from prefect.tasks.prefect import create_flow_run

with Flow("FLOW_NAME",) as flow:
    extract_load_id = create_flow_run(
        flow_name="your-flow-name",
        project_name="PROJECT_NAME",
        parameters={"key": "value"},
    )
a
I think you can start by following this getting started tutorial to get familiar with Prefect in general: https://docs.prefect.io/core/getting_started/quick-start.html Then, you can build your logic to insert data into a database e.g. via a custom function and decorate it with @task
a
yes, but i cant get value of parameters! how to get value of parameters in flow ?
a
@Anh Nguyen Like so:
Copy code
from prefect import task, Flow, Parameter


@task(log_stdout=True)
def hello_world(name):
    print(f"hello {name}")


with Flow("mini.example") as flow:
    name = Parameter("name", default="Anh")
    hw = hello_world(name)

if __name__ == "__main__":
    flow.run()
a
sorry if my question is not clear. Step 1: Kill off the prefect flow from api and passing [key:value] Step 2: In the prefect flow i want to get value in [key:value]. This value is use to insert to db.