Forum Discussion

stuckerj's avatar
stuckerj
Helper I
10 months ago
Solved

Cannot set the data source for a Fabric Notebook using the API

I'm trying to automate the upload of Fabric Notebooks. I cannot establish a lakehouse data source for a notebook programmatically. When using the API, it seems I need to include a .platform file, but all attempts at uploading or even downloading it fail.

 

This code fails with a 404 error, even when confirming the workspace_id and notebook_id are correct.

 

# === GET NOTEBOOK DEFINITION ===
get_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/items/{notebook_id}/GetDefinition?format=ipynb"
resp = requests.get(get_url, headers=headers)
resp.raise_for_status()
print("Raw API response:")
print(resp.text)

 

When I create a Notebook using the code at Manage and execute Fabric notebooks with public APIs - Microsoft Fabric | Microsoft Learn, the Notebook is created successfully, but the data source is not created, so when the notebook is run, it fails to process data correctly.

 

To fix this problem, some sources say that a .platform file is required, and I cannot seem to figure out how to include that with the Notebook creation.

 

Has anyone been able to both create a Notebook and set its data source programmatically?  I'd love to see an example of working code.

  • 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.

6 Replies

  • 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

    1. Prepare the .platform file: Create your platform configuration in JSON format, then Base64 encode it.

    2. Include in Parts Payload: When creating or updating the notebook, include the .platform file as an inline payload similar to the .ipynb part.

    3. 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.

  • v-prasare's avatar
    v-prasare
    Community Support

    Hi stuckerj,

    We would like to confirm if our community members answer resolves your query or if you need further help. If you still have any questions or need more support, please feel free to let us know. We are happy to help you.


    MJParikh ,Thanks for your prompt response

     

     

    Thank you for your patience and look forward to hearing from you.
    Best Regards,
    Prashanth Are
    MS Fabric community support

  • 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.