Victor
03/04/2025, 1:22 PMMarvin
03/04/2025, 1:23 PMserve
function from Prefect's top-level API. This feature allows you to run a flow as a service, accepting HTTP requests with parameters to trigger the flow. Here’s a brief overview of how you might achieve this:
1. Define Your Flow: Ensure your flow is defined with parameters. Prefect supports both positional and keyword arguments for flow parameters.
2. Use Prefect's serve
Feature: Prefect provides a serve
function that can be used to expose a flow as a REST API. This function listens for HTTP requests and executes the flow with the provided parameters.
3. Handle Parameters: When you trigger the flow via the REST API, you can pass parameters in the request. Prefect will automatically handle the type conversion of these inputs.
For more detailed instructions and examples, you can refer to the Prefect documentation on writing flows.
If you're using Prefect 3.x, here’s a quick way to get started with the serve
function:
python
from prefect import flow, serve
@flow
def my_flow(param1, param2):
# Your flow logic here
pass
serve(my_flow)
This code snippet will expose my_flow
as a REST API, allowing you to trigger it with parameters via HTTP requests.
For more details on the serve
function, you can explore the Prefect API documentation or the source code here.Victor
03/04/2025, 1:25 PMMarvin
03/04/2025, 1:26 PMprefect server start
command. More details are available in the self-hosting guide.
2. Define Your Flow: Create your flow in Python, ensuring it accepts parameters as needed.
3. Create a YAML Deployment: Define your deployment in a prefect.yaml
file. This file allows you to configure how and where your flow runs, including infrastructure settings.
4. Deploy the Flow: Use the prefect deploy
command with your YAML file to deploy the flow. This will set up the flow on your Prefect server, making it available for execution via the API.
Here's a basic example of what your prefect.yaml
might look like:
yaml
version: 1
flows:
- name: my-flow
parameters:
param1: default_value1
param2: default_value2
deployments:
- name: my-deployment
flow: my-flow
schedule:
cron: "* * * * *" # Example cron schedule
infrastructure:
type: process
5. Trigger Flow via API: Once deployed, you can trigger your flow using the Prefect API. You can specify parameters in the API request to customize each flow run.
For more details on deployments and YAML configurations, visit the Prefect deployment documentation.
By following these steps, you can effectively expose your flow as a REST API while using a self-hosted Prefect setup and YAML for deployment.