how to call fabric rest apis from python notebooks.
introduction.
Now that Microsoft pushed Fabric REST APIs to public preview, I thought I’d provide a simple Python script on how to call them. It really is no rocket science, while it’s worth noticing that some of the API calls do not (yet?) work with Service Principals. In fact, the Fabric API groups Core and Admin are the ones that seem to currently work when authenticating with Service Principals. My guess is that in the future, Microsoft will add the Service Principals capability for the other Fabric API group Lakehouse as well. For the authentication part, especially on how to get the access token from Entra ID (former Azure Active Directory) for a Service Principal, I highly recommend checking out the series of articles by datalineo. Lastly, as already mentioned the API is still in public preview, at the time of writing this article.
prerequisites.
1. A Fabric capacity and workspace
2. A JupyterLab Notebook (of course, you can also use Fabric Notebooks)
3. Optional: A Service Principal that has appropriate access rights. You need this one if you do not want to use a user’s credentials when fetching a token (check out the very end of the Showtime part, if you are interested in a solution without a Service Principal). If you wanna use a Service Principal, take a look at the appendix on how to set this up.
plan of action.
1. What’s the goal?
The goal is to call Fabric REST APIs from a Python notebook. As seen in the example below, we called the Get Workspace API returning Fabric workspaces with the help of a Service Principal. Note, if you fancy a solution without a Service Principal, we do have an approach for that in this article, too.
2. The script
Below, the script with the Service Principal authentication. Note, the tenant_id, client_id and client_secret you can retrieve from the Service Principal page in Azure. Check out the appendix on where exactly you can find them. Also, in the script below, we call the specific Get Workspace API at the very bottom of the code snippet (line 13). If you’d like to call another API, you need adapt that line.
If you are using a Fabric Notebook you can choose to fetch a token without a Service Principal as well. You can do this by utilising the mssparkutils:
Another way is to use the FabricRestClient() via semantic-link as described by Sandeep.
3. Showtime
Here, we executed the Python script with the help of a Service Principal, normalized the json response and printed the dataframe:
Here another one, where we call the Get Items API. This call lists all the different items that are present in a certain workspace, like lakehouses, semantic models, pipelines, data flows, etc.
If you enable the Allow service principals to use read-only admin APIs in the Admin settings, you can also run APIs from the Fabric API Group Admin.