Microsoft Fabric REST API in Fabric Notebook using Service Principal

Managing Microsoft Fabric tasks from Fabric Notebook using Python is super easy. You can achieve this through Microsoft Fabric REST API and Python requests library. In this easy-to-follow tutorial, we’ll show you how to call the Fabric REST API to list all the Notebooks in your workspace using service principal. Before starting, make sure that Azure App Registration is correctly setup. Incase you are unfamiliar with this or encounter issues, check out our tutorial on Creating App Registration for Microsoft Fabric.

Let’s dive into the tutorial on using Fabric REST APIs in Fabric Notebooks.

1. Create a Fabric Notebook and import requests library.

import requests

2. Before making a call to Fabric REST API, we need to get access token. Replace Tenant ID, Client ID and Client Secret with the values we get from Azure App Registration.

url = “https://login.microsoftonline.com/{Tenant ID}/oauth2/v2.0/token”
headers = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
data = “””grant_type=client_credentials
&client_id={Client ID}
&client_secret={Client Secret}
&scope=https%3A%2F%2Fapi.fabric.microsoft.com%2F.default”””
response = requests.post(url=url, data=data, headers=headers)
access_token = response.json()[“access_token”]

3. Now call the REST API using with access token specified in the header as follow.

url2 = “https://api.fabric.microsoft.com/v1/workspaces/{Workspace ID}/notebooks”
headers2 = {‘Content-Type’: ‘application/json’, ‘Authorization’: f’Bearer {access_token}’}
response2 = requests.get(url=url2, headers=headers2)
pipeline_details = response2.json()[“value”]

4. We can save the results in dataframe and display as follow.

df = spark.read.json(spark.sparkContext.parallelize(pipeline_details))
display(df)

Microsoft Fabric Notebook
Scroll to Top