<@ULVA73B9P> Can you provide a complete example using the Prefect client SDK to start a flow run on ...
c
@Marvin Can you provide a complete example using the Prefect client SDK to start a flow run on a self-hosted instance of Prefect? Ideally the example would also show to provide authorization credentials and the server URL.
m
thought for 301.2 seconds
Absolutely — here are complete, working examples for Prefect 3.x that show how to start a flow run on a self-hosted Prefect Server, including how to provide the server URL and authorization. Assumptions: - You’re using a self-hosted Prefect Server (not Prefect Cloud) - Your API is reachable at something like http://127.0.0.1:4200/api - You’ve enabled API auth on your server and have an auth string (username:password) Key settings for self-hosted: - PREFECT_API_URL — your server’s API endpoint (must include /api) - PREFECT_API_AUTH_STRING — the auth string (e.g., admin:password) - Do not set PREFECT_API_KEY for self-hosted; that’s for Prefect Cloud and will take precedence if present. Useful docs: - Prefect Server (self-hosted) - Security and auth settings Option A: Configure via environment variables (simplest) Set environment variables, then use the high-level helper to trigger a deployment by name: macOS/Linux:
Copy code
export PREFECT_API_URL="<http://127.0.0.1:4200/api>"
export PREFECT_API_AUTH_STRING="admin:password"
Windows (PowerShell):
Copy code
$env:PREFECT_API_URL="<http://127.0.0.1:4200/api>"
$env:PREFECT_API_AUTH_STRING="admin:password"
Python (simple trigger using helper):
Copy code
from prefect.deployments.flow_runs import run_deployment

# "flow-name/deployment-name" must match your deployment exactly
flow_run = run_deployment(
    name="my-flow/my-deployment",
    parameters={"x": 1, "y": "hello"},
    flow_run_name="manual-trigger-{{timestamp}}",
    tags=["manual", "api-trigger"],
)

print("Created flow run:", flow_run.id)
Option B: Pass URL and credentials directly to the client (explicit) Sync example:
Copy code
from prefect.client.orchestration import SyncPrefectClient

with SyncPrefectClient(
    api="<http://127.0.0.1:4200/api>",
    auth_string="admin:password",
) as client:
    # Look up the deployment by "flow/deployment" name
    deployment = client.read_deployment_by_name("my-flow/my-deployment")

    # Create a flow run from that deployment
    flow_run = client.create_flow_run_from_deployment(
        deployment_id=deployment.id,
        parameters={"x": 1, "y": "hello"},
        flow_run_name="manual-trigger-{{timestamp}}",
        tags=["manual", "api-trigger"],
    )

    print("Created flow run:", flow_run.id)
Async example:
Copy code
import asyncio
from prefect.client.orchestration import PrefectClient

async def main():
    async with PrefectClient(
        api="<http://127.0.0.1:4200/api>",
        auth_string="admin:password",
    ) as client:
        deployment = await client.read_deployment_by_name("my-flow/my-deployment")
        flow_run = await client.create_flow_run_from_deployment(
            deployment_id=deployment.id,
            parameters={"x": 1, "y": "hello"},
            flow_run_name="manual-trigger-{{timestamp}}",
            tags=["manual", "api-trigger"],
        )
        print("Created flow run:", flow_run.id)

asyncio.run(main())
Optional: Configure settings in code (no env vars)
Copy code
from prefect.settings import temporary_settings, PREFECT_API_URL, PREFECT_API_AUTH_STRING
from prefect.deployments.flow_runs import run_deployment

with temporary_settings({
    PREFECT_API_URL: "<http://127.0.0.1:4200/api>",
    PREFECT_API_AUTH_STRING: "admin:password",
}):
    flow_run = run_deployment(
        name="my-flow/my-deployment",
        parameters={"x": 1},
        flow_run_name="manual-trigger-{{timestamp}}",
    )
    print("Created flow run:", flow_run.id)
Notes: - Ensure you have a running worker for the work pool your deployment uses; otherwise, the run will be scheduled but never picked up. - For self-hosted Server auth, use PREFECT_API_AUTH_STRING or the client’s auth_string parameter. Avoid PREFECT_API_KEY unless you’re targeting Prefect Cloud. If you share your deployment name and how your server auth is configured, I can tailor the snippet exactly to your setup.
c
Thank you. Once a flow run has successfully been start, can the script exit or does it have to remain running?
n
hi @Chris Killpack! right now you have to tag marvin to send it the slack event by default,
run_deployment
will block until the invoked flow run reaches a terminal state, but you can use
timeout=0
to fire-and-forget