This document provides a comprehensive, step-by-step guide to getting started with the Fabric API, including detailed instructions and best practices for successful implementation. This guide will focus on migrating Power BI workspaces to Fabric Capacity using the Fabric API in Python.
Ensure you have the following prerequisites before proceeding:
-
Python Packages:
- Install the required Python packages:
pip install msal pip install azure-identity azure-keyvault-secrets
- Install the required Python packages:
-
Azure Credentials:
- Set the following environment variables with your Azure credentials:
AZURE_CLIENT_IDAZURE_TENANT_IDAZURE_CLIENT_SECRET
- Set the following environment variables with your Azure credentials:
-
Azure Key Vault:
- Your Azure Key Vault should store the client secret needed for authentication.
- Set the Key Vault name and secret name in the notebook.
-
API Permissions:
- Ensure the app registration has the necessary permissions to interact with the Fabric API. Follow these steps:
- Go to the Azure portal.
- Navigate to "Azure Active Directory" > "App registrations".
- Select your app registration (e.g.,
Fabric POC App). - Go to "API permissions" > "Add a permission".
- Select "Microsoft Graph" and add the following Delegated permissions:
User.Read
- Select "APIs my organization uses" > "Power BI Service" and add the following Delegated permissions:
Capacity.ReadWrite.AllWorkspace.ReadWrite.All
- Grant admin consent for the permissions.
Click here to learn more about how to create an App Registration
- Ensure the app registration has the necessary permissions to interact with the Fabric API. Follow these steps:
-
Workspace Access:
- Assign the app registration to the Fabric workspace to have access to the artifacts within the workspace:
- Navigate to the workspace.
- Click on
Manage access. - Add the app registration (e.g.,
Fabric POC App) and assign the appropriate role (Admin, Contributor, or Member required).
- Assign the app registration to the Fabric workspace to have access to the artifacts within the workspace:
Set the environment variables for AZURE_CLIENT_ID, AZURE_TENANT_ID, and AZURE_CLIENT_SECRET.
import os
os.environ['AZURE_CLIENT_ID'] = '<CLIENT ID>'
os.environ['AZURE_TENANT_ID'] = '<TENANT ID>'
os.environ['AZURE_CLIENT_SECRET'] = '<CLIENT SECRET>'Fetch the client secret from Azure Key Vault using the azure-identity and azure-keyvault-secrets packages.
from azure.identity import EnvironmentCredential
from azure.keyvault.secrets import SecretClient
key_vault_name = "<KEY_VAULT_NAME>"
secret_name = "<KEY_VAULT_SECRET>"
vault_url = f"https://{key_vault_name}.vault.azure.net"
credential = EnvironmentCredential()
secret_client = SecretClient(vault_url=vault_url, credential=credential)
retrieved_secret = secret_client.get_secret(secret_name)
client_secret = retrieved_secret.value
Use the msal package to acquire an access token for the Microsoft Fabric API.
from msal import ConfidentialClientApplication
tenant_id = os.getenv('AZURE_TENANT_ID')
client_id = os.getenv('AZURE_CLIENT_ID')
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ["https://api.fabric.microsoft.com/.default"]
app = ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
result = app.acquire_token_for_client(scopes)
if 'access_token' in result:
access_token = result['access_token']
else:
raise Exception("Token acquisition failed:", result.get("error"), result.get("error_description"))
You can interact with various endpoints provided by the Fabric API. Below are examples of common operations:
import requests
headers = {'Authorization': 'Bearer ' + access_token}
capacities_url = "https://api.fabric.microsoft.com/v1/capacities"
capacities_response = requests.get(capacities_url, headers=headers)
if capacities_response.status_code == 200:
capacities = capacities_response.json()["value"]
for capacity in capacities:
print(f"ID: {capacity['id']}, Name: {capacity['displayName']}")
else:
print("Failed to retrieve capacities:", capacities_response.status_code, capacities_response.text)workspaces_url = "https://api.fabric.microsoft.com/v1/workspaces"
workspaces_response = requests.get(workspaces_url, headers=headers)
if workspaces_response.status_code == 200:
workspaces = workspaces_response.json()["value"]
for workspace in workspaces:
print(f"ID: {workspace['id']}, Name: {workspace['displayName']}")
else:
print("Failed to retrieve workspaces:", workspaces_response.status_code, workspaces_response.text)
workspace_id = "<WORKSPACE_ID>"
capacity_id = "<CAPACITY_ID>"
assign_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/assignToCapacity"
data = {"capacityId": capacity_id}
assign_response = requests.post(assign_url, headers=headers, json=data)
if assign_response.status_code in [200, 202]:
print(f"Workspace {workspace_id} assigned successfully to capacity {capacity_id}.")
else:
print(f"Failed to assign workspace {workspace_id}: {assign_response.status_code} {assign_response.text}")
Implement robust error handling to manage API errors gracefully.
def handle_response(response):
if response.status_code == 200:
return response.json()
else:
print("Error:", response.status_code, response.text)
return None
By following this guide, you can start using the Fabric API to automate and integrate Microsoft Fabric with your existing systems. Refer to the official API documentation for detailed information on all available endpoints and their usage.
For further assistance, consult the Microsoft Fabric documentation and community forums.