Dear Prefect Community, What is the API payload I ...
# ask-community
x
Dear Prefect Community, What is the API payload I have to use to register a particular FLOW or TASK via APIs? Do I need to provide the entire Python code or the path of the .py file? I don't want to use CLI or Prefect UI to do the same. If you can provide an example with details, that would be great.
a
x
Thanks Anna.
That means, we can't register it by calling an API and passing the code in the payload?
I don't see any example in this page using API other than GraphQL.
a
This is all you need to register via API call - GraphQL is the API and the payload is your serialized flow.
x
Got it. Do we have any example for the GraphQL JSON content i.e., the serialized flow JSON?
The part what I am not clear is, if I write a python (with @TASK and @FLOW, etc.) code in a local path and store it, only using API, how can I register that FLOW/TASK by passing the .py file?
k
You can really just use the CLI. Otherwise, you can use
flow.serialize()
and that’s the payload you need to include
upvote 1
But then you need some sort of utility to extract the flow from a file, and at that point, it’s a lot of reinventing of the utilities Prefect already has
x
I would like to use flow.serialize() and I am trying to find an example for flow.serialize()
k
Sorry I’m a bit confused. You can just call it after you define your Flow and print the output and it should show the JSON
x
How the registered flow knows the python function what it suppose to execute from a specific Python file? Where do we specify the .py file path?
May be I misunderstood the logic. Sorry.
k
Your JSON representation includes Storage and RunConfig. Storage would point to the location that contains the file path so if you do:
Copy code
with Flow(..) as flow:
    ...

flow.storage = ...
flow.serialize()
The storage information is contained in the serialized Flow. To point it to a path, you want to attach
Local
storage. That’s what you mean right?
x
I have flow1.py where I have task1, flow1 and subflow1. I would like to use REST APIs (Prefect 2.0) and register TASK1, FLOW1 and SUBFLOW1.
So that I can see them in the UI. Then using REST APIs, I can run FLOW1.
These are my use cases.
k
I think with your use case, just use
flow.register()
to register, and then you can use the REST API to run
x
flow.register() within the Python Code? or any REST API which would do flow.register()?
I think you meant, write a python code with flow.register() and run it in CLI. Later, use that FLOW_NAME in the REST API, is that correct?
I would like to avoid the CLI totally.
k
Yes inside the Flow like this . You don’t need the CLI. Just run this Python script and it will register the Flow with Prefect Cloud
And then once that is registered in Cloud, you can just invoke the Flow run using a REST API like this or you can use the Prefect Client and use
client.create_flow_run()
x
That is exactly my Q is, I need to run at least once the python code (with flow.register()) in the command line for registration. There is no REST API to pass the Python code and the API would run the code internally and do the registration.
k
Ohh my bad I just noticed. We’re talking about Orion right?
x
yes
k
Have you seen the documentation on Deployments ? This is the equivalent of registering
x
yes. But I don't see any REST API way of doing the same e.g. prefect deployment create ./my_flow_deployment.py
Request body of Create Deployment REST API is
Copy code
{
  "name": "string",
  "flow_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "flow_data": {
    "encoding": "string",
    "blob": "string"
  },
  "schedule": {
    "interval": 0,
    "timezone": "America/New_York",
    "anchor_date": "2022-02-15T18:48:17.789Z"
  },
  "is_schedule_active": true,
  "parameters": {},
  "tags": [
    "tag-1",
    "tag-2"
  ],
  "flow_runner": {
    "type": "string",
    "config": {}
  }
}
But it uses an existing flow_id or the flow id is a GUID.
k
Will ask one of the engineers how to do this
x
Thanks, Kevin.
k
It’s not recommended to encode your own flow data because there is a lot of logic to create a deployment from a Flow
It’s really recommended to either use the CLI or Python because that is the intended API
x
Thanks Kevin