<@ULVA73B9P> - how can i programmatically generate...
# ask-marvin
k
@Marvin - how can i programmatically generate the url of the current flow run
m
thought for 199.1 seconds
In Prefect 3.x, the easiest way is to use the runtime context during execution:
Copy code
from prefect.runtime import flow_run

# Full UI URL for the currently-running flow run
url = flow_run.ui_url
print(url)
Notes: - This returns the correct URL for both Cloud and Server. - It returns None if called outside of an active flow run. If you need to build a URL from a flow-run ID outside of execution, you can use the URL helper after fetching the flow run model:
Copy code
import asyncio
from prefect.client.orchestration import get_client
from prefect.utilities.urls import url_for

async def flow_run_url(flow_run_id: str) -> str | None:
    async with get_client() as client:
        fr = await client.read_flow_run(flow_run_id)
    return url_for(fr)  # constructs the UI URL for that FlowRun

# Example:
# print(asyncio.run(flow_run_url("f9b2d1f8-....")))
Docs: - Runtime context - prefect.runtime API - prefect.utilities.urls.url_for If you’re trying to generate the URL from outside a running flow without fetching the model, let me know your environment (Cloud vs Server) and I can share a minimal approach for that too.