cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Create a DataBricks Notebook using API

pranav2
New Contributor II
import requests
import json

# Databricks workspace API URL

# Databricks API token (generate one from your Databricks account)
databricks_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx....."
path = "/Users/pranav.kakade@skylinelabs.us/SAMPLE_ANALYTICS_PIPELINE"
# Notebook content

content = {
  "cells": [
    {
      "cell_type": "code",
      "metadata": {},
      "source":
        "print('Hello, Databricks!')",
      "execution_count": None,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}

data = {
    "content":content,
    "overwrite":"true",
    "path":"/Users/pranav.kakade@skylinelabs.us/SAMPLE_ANALYTICS_PIPELINE/OVERWRITE",
    "format":"JUPYTER"
}

notebook_content = {  
  "content": {
    "cells": [
      {
        "cell_type": "code",
        "source": [
          "# This is a Databricks notebook created using the REST API",
          "print('Hello, Databricks!')"
        ],
       
        "execution_state": "running",
        "outputs": []
      }
     
    ],
    "metadata": {
      "celltoolbar": "Default"
    },
   
  },
  "format":"JUPYTER",
  "path":"/Users/pranav.kakade@skylinelabs.us/SAMPLE_ANALYTICS_PIPELINE",
  "overwrite": "true",
  "language": "PYTHON"
}

notebook = {
  "path": "/Users/pranav.kakade@skylinelabs.us/SAMPLE_ANALYTICS_PIPELINE/OVERWRITE",
  "format": "JUPYTER",
  "language": "PYTHON",
  "content": "string",
  "overwrite": True
}

notebook_content_json = json.dumps(notebook_content)

# Include the notebook path in the payload

# Headers for the Databricks API request
headers = {
    "Authorization": f"Bearer {databricks_token}",
    "Content-Type": "application/json"
}

# Create the notebook
response = requests.post(
    databricks_url,
    {
        "data":notebook,
        "headers":headers
    }
)

# Check if the request was successful
if response.status_code == 200:
    print("Notebook created successfully.")
else:
    print("Failed to create notebook:", response.status_code, response.text)

This code giving below error

Failed to create notebook: 401 <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 401 Unauthorized</title> </head> <body><h2>HTTP ERROR 401</h2> <p>Problem accessing /api/2.0/workspace/import. Reason: <pre> Unauthorized</pre></p> </body> </html>

Have checked the credentials but still not working



4 REPLIES 4

Kaniz_Fatma
Community Manager
Community Manager

Hi @pranav2The error message Failed to create notebook: 401 Unauthorized Indicates that the server is not recognizing your credentials. This could be due to various reasons, including an incorrect or expired token or a lack of necessary permissions. Given the information provided, there are a few possible issues and solutions.

1. **Incorrect token**: Ensure your ticket is correct and has not expired. Tokens can be generated from your Databricks account under "User Settings"> "Access Tokens".

2. **Lack of necessary permissions**: If you are a non-admin user and want to log in as an admin user, you must provide the X-Databricks-Azure-Workspace-Resource-Id header and the’ Authorization’: ’Bearer' header. You must be in a Contributor or Owner role on the workspace resource in Azure.

3. **Incorrect URL**: Make sure the URL you use for the Databricks workspace API is correct. The URL in your code seems to be a placeholder and not a valid Databricks workspace URL.

Replace https://blair.biz/appindex.html with the correct Databricks workspace URL.

pranav2
New Contributor II

Everything is correct as I had mentioned. It is still not working.

pranav2
New Contributor II

Can you please provide me with code on how to create a notebook in databricks using api?

 

Hi @pranav2To create a Databricks Notebook using API, you would use the Workspace API's create endpoint. 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group