Forum Discussion
Cannot set the data source for a Fabric Notebook using the API
- 9 months ago
The only reliable way that I've seen yet to automate this is to create a separate notebook that sets the connections for the other notebooks.
Creating a Fabric Notebook and programmatically setting its data source, particularly the lakehouse, involves some nuanced steps, especially around including the required .platform file. Based on the latest Microsoft Fabric API capabilities and community discussions, here are some insights and potential solutions:
Understanding the .platform File
The .platform file acts as a configuration file for integrating data sources like lakehouses with notebooks. When creating or updating notebooks via API, including this file enables programmatic association with a data source.
Current API Capabilities
Creating Notebooks: Using POST requests to https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/items supports uploading .ipynb files along with associated parts, but explicitly linking a data source (lakehouse) often requires this .platform file to be part of the payload.
Getting Notebook Definitions: Use GET to fetch the existing notebook's definition, which can include platform configurations.
Updating Notebook Definition: To set or change the data source, you typically need to update the notebook's definition with the .platform file, encoded in Base64, included in the parts payload.
Challenges and Common Issues
404 Error: When attempting to upload or download the .platform file, a 404 suggests the API endpoint might be incorrect or the file isn't properly referenced within the payload [user query].
Including the .platform file: Community examples, such as GitHub discussions, show that when deploying via Terraform or direct API calls, the .platform file must be Base64 encoded and properly referenced within the parts array.
Recommendations & Example Approach
Prepare the .platform file: Create your platform configuration in JSON format, then Base64 encode it.
Include in Parts Payload: When creating or updating the notebook, include the .platform file as an inline payload similar to the .ipynb part.
Use the update-notebook-definition API: After the initial creation, update the notebook with the platform configuration to link the lakehouse.
Example Code Snippet
import base64
import requests
# Sample platform configuration as JSON
platform_config = {
"lakehouse": {
"name": "MyLakehouse",
"connectionDetails": { /* connection details here */ }
}
}
# Encode to Base64
platform_payload = base64.b64encode(json.dumps(platform_config).encode('utf-8')).decode()
# Payload for updating the notebook
update_payload = {
"parts": [
{
"path": ".platform",
"payload": platform_payload,
"payloadType": "InlineBase64"
}
]
}
# API call to update the notebook definition
response = requests.post(
f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/items/{notebook_id}/UpdateDefinition",
headers=headers,
json=update_payload
)
response.raise_for_status()Final Notes
Check the exact API endpoint for updating the notebook's definition; Microsoft documentation and community sources indicate both create and update endpoints support attaching the .platform part.
Ensure the payload is properly Base64 encoded and the API headers include the necessary authorization tokens.
If the API continues to return 404, review the URL endpoints, ensure the notebook exists, and confirm correct workspace and notebook IDs.
- stuckerj9 months agoHelper I
I have yet to find a payload that will work via REST API. This is consistent with the answer on another thread:
Re: REST API vs. Notebookutils Lakehouse Update Is... - Microsoft Fabric Community
It would likely be easier to create another notebook just for the purpose of setting the data source via notebookutils, which can be done directly without jumping through the hoops of creating a mysteriously formatted .platform file.- v-dineshya9 months agoCommunity Support
Hi stuckerj ,
Please refer below link.
Solved: Update Notebook attached lakehouse using code - Microsoft Fabric Community
Lakehouse management API - Microsoft Fabric | Microsoft Learn
Solved: Rest Api to set default lakehouse of notebook - Microsoft Fabric Community
Solved: Re: Change default Lakehouse with notebookutils - Microsoft Fabric Community
Solved: How to set default Lakehouse in the notebook progr... - Microsoft Fabric Community
Solved: REST API TO CREATE NOTEBOOK - Microsoft Fabric Community
Solved: Re: Automate attachment of Lakehouse/data source i... - Microsoft Fabric Community
I hope this information helps. Please do let us know if you have any further queries.
Regards,
Dinesh
- v-dineshya9 months agoCommunity Support
Hi stuckerj ,
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. And, if you have any further query do let us know.
Regards,
Dinesh