cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
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



2 REPLIES 2

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?

 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now