How can I list all blocks across my workspaces?

Here’s an example of how you would list your blocks across all your workspaces using the Prefect API Python client. There’s two interesting bits to note:

  1. The API client is async, so you need some async flavor sprinkled in.
  2. The API client generally assumes that you’re working within a specific workspace, so we have to do a bit of extra work to make the list blocks call for each workspace.
import anyio

from prefect.client import OrionClient
from import get_cloud_client
from prefect.settings import PREFECT_API_KEY

async def list_blocks(api_url):
    async with OrionClient(
    ) as client:
        blocks = await client.read_block_documents()
    return blocks

async def list_workspaces():
    async with get_cloud_client() as client:
        workspaces = await client.read_workspaces()
    return workspaces

async def list_all_blocks():
    workspaces = await list_workspaces()
    for workspace in workspaces:

        blocks = await list_blocks(workspace.api_url())

        print(f"\nBlocks in workspace: '{workspace.workspace_name}':")
        for block in blocks:

if __name__ == "__main__":
1 Like