Python Microsoft Fabric API - Workspaces

list_workspaces, get_workspace, list_items and get_item, plus shortcuts and capacities.

A workspace is a folder that holds everything a team works on in Fabric, and everything in it - a lakehouse, a notebook, a pipeline, a report - is an item with an ID. These methods find workspaces and items, which is how a service gets an ID it was not given.

list_workspaces

conn.list_workspaces()

Returns every workspace the app registration is a member of.

Returns a dict with value, a list of dicts each with id, displayName, description, type and capacityId.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class ListWorkspaces(Service):

    def handle(self):

        conn = self.microsoft.fabric['My Fabric']

        response = conn.list_workspaces()

        workspaces = []
        for workspace in response['value']:
            workspace_summary = {'id': workspace['id'], 'name': workspace['displayName']}
            workspaces.append(workspace_summary)

        self.response.payload = {'workspaces': workspaces}
{"workspaces": [{"id": "11111111-2222-3333-4444-555555555555", "name": "Analytics"}]}

get_workspace

conn.get_workspace(workspace_id)

Returns one workspace.

ParameterTypeMeaning
workspace_idstrThe workspace

Returns a dict with the same fields as one entry of list_workspaces.

list_items

conn.list_items(workspace_id, item_type='')

Returns the items of a workspace, all of them or one type.

ParameterTypeMeaning
workspace_idstrThe workspace
item_typestrOne of the types below, or empty for every item

Returns a dict with value, a list of dicts each with id, displayName, description, type and workspaceId.

TypeWhat it is
LakehouseTables and files
EventstreamWhere events come in and go out
EventhouseWhere events are kept for querying
KQLDatabaseA database inside an eventhouse
NotebookA page of Spark code
DataPipelineA sequence of steps that moves and reshapes data
ReportA Power BI report
SemanticModelThe data a report reads, the item DefaultSemanticModelRefresh runs on
WarehouseA SQL warehouse
# -*- coding: utf-8 -*-

# stdlib
from http.client import NOT_FOUND

# Zato
from zato.server.service import Service

class FindLakehouse(Service):

    def handle(self):

        workspace_id = '11111111-2222-3333-4444-555555555555'
        name = 'Operations'

        conn = self.microsoft.fabric['My Fabric']

        response = conn.list_items(workspace_id, 'Lakehouse')

        # The ID of the lakehouse with the given name ..
        for item in response['value']:
            if item['displayName'] == name:
                self.response.payload = {'lakehouse_id': item['id']}
                break

        # .. or an error if there is none.
        else:
            self.response.status_code = NOT_FOUND
            self.response.payload = {'error': 'No such lakehouse'}
{"lakehouse_id": "66666666-7777-8888-9999-000000000000"}

get_item

conn.get_item(workspace_id, item_id)

Returns one item.

ParameterTypeMeaning
workspace_idstrThe workspace the item is in
item_idstrThe item

Returns a dict with the same fields as one entry of list_items.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class GetItem(Service):

    def handle(self):

        workspace_id = '11111111-2222-3333-4444-555555555555'
        item_id = '66666666-7777-8888-9999-000000000000'

        conn = self.microsoft.fabric['My Fabric']

        item = conn.get_item(workspace_id, item_id)

        self.response.payload = {'name': item['displayName'], 'type': item['type']}

Shortcuts

A shortcut makes data that lives in another workspace, or in storage outside Fabric, appear inside a lakehouse without copying it. Three methods manage them:

conn.list_shortcuts(workspace_id, item_id)
conn.create_shortcut(workspace_id, item_id, data)
conn.delete_shortcut(workspace_id, item_id, shortcut_path, shortcut_name)

list_shortcuts returns a dict with value, a list of shortcuts each with name, path and target. create_shortcut takes the shortcut as a dict in the shape the Fabric API expects and returns the created shortcut. delete_shortcut takes the folder the shortcut is in, Tables or Files, and its name.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class LinkInsurerTable(Service):

    def handle(self):

        workspace_id = '11111111-2222-3333-4444-555555555555'
        lakehouse_id = '66666666-7777-8888-9999-000000000000'

        source_workspace_id = '22222222-3333-4444-5555-666666666666'
        source_lakehouse_id = '77777777-8888-9999-0000-111111111111'

        conn = self.microsoft.fabric['My Fabric']

        # The insurers table of another workspace's lakehouse, readable here as `insurers`.
        shortcut = {
            'name': 'insurers',
            'path': 'Tables',
            'target': {
                'oneLake': {
                    'workspaceId': source_workspace_id,
                    'itemId': source_lakehouse_id,
                    'path': 'Tables/insurers',
                }
            }
        }

        conn.create_shortcut(workspace_id, lakehouse_id, shortcut)

Capacities

A capacity is the computing power a workspace runs on, which your organization pays for. conn.list_capacities() returns the ones the app registration can see, as a dict with value, a list of dicts each with id, displayName, sku, region and state. A workspace's capacityId from list_workspaces matches one of these IDs.

See also

PageWhat it covers
ConnectionWhat makes a workspace visible to the connection
JobsWhat to do with a notebook or pipeline ID once found
TablesWhat to do with a lakehouse ID once found

Learn more