Databricks API Wrapper

1.1.8 · active · verified Thu Apr 16

The `databricksapi` library provides a Python wrapper for the Databricks REST API, simplifying interactions with Databricks workspaces, clusters, jobs, and more. It leverages the `requests` module for HTTP communication. Currently at version 1.1.8, its release cadence is moderate, with updates typically addressing new Databricks API features or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the DatabricksAPI client using environment variables for host and token, then demonstrates fetching a list of clusters. Ensure `DATABRICKS_HOST` includes the `https://` prefix and `DATABRICKS_TOKEN` has sufficient permissions (e.g., 'clusters/list') to perform the requested API calls.

import os
from databricks_api import DatabricksAPI

databricks_host = os.environ.get('DATABRICKS_HOST', 'https://your-databricks-host.cloud.databricks.com') # e.g., 'https://dbc-xxxx.cloud.databricks.com'
databricks_token = os.environ.get('DATABRICKS_TOKEN', 'dapiXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

if not databricks_host or 'your-databricks-host' in databricks_host:
    print("Error: Please set DATABRICKS_HOST environment variable or replace placeholder in code.")
elif not databricks_token or 'dapi' not in databricks_token:
    print("Error: Please set DATABRICKS_TOKEN environment variable or replace placeholder in code.")
else:
    try:
        # Initialize the Databricks API client
        databricks = DatabricksAPI(host=databricks_host, token=databricks_token)
        
        # Example: List active clusters
        # Requires 'clusters/list' permission on the token
        clusters_response = databricks.clusters.list_all_clusters()
        clusters = clusters_response.get('clusters', [])
        print(f"Found {len(clusters)} clusters.")
        if clusters:
            print(f"First cluster name: {clusters[0]['cluster_name']}")
            
        # Example: List jobs (uncomment to run, requires 'jobs/list' permission)
        # jobs_response = databricks.jobs.list_jobs()
        # jobs = jobs_response.get('jobs', [])
        # print(f"Found {len(jobs)} jobs.")

    except Exception as e:
        print(f"An error occurred: {e}")
        if hasattr(e, 'response') and hasattr(e.response, 'json'):
            print(f"API Error Details: {e.response.json()}")

view raw JSON →