Sub topic 9
Calling any endpoint with databricks api
This chapter assumes you have authenticated the CLI with a profile, know the REST basics from the previous chapters (host, path, bearer auth), and can use jq.
What the command does
Not every REST endpoint has a dedicated CLI command, and new endpoints often appear in the API first. The databricks api command fills that gap. It sends an authenticated HTTP request to any path on your workspace and prints the JSON response.
The command reuses the authentication you already set up. It reads the host and credentials from your profile or environment variables, and it adds the Authorization header for you. You write only the HTTP method, the path, and an optional body.
Syntax
The general form is databricks api METHOD PATH [--json BODY]. The methods are get, post, put, patch, and delete. The path starts with a slash and includes the API version, exactly as it appears in the REST documentation.
# GET with a query string. Quote the path so the shell does not expand "?".
databricks api get "/api/2.1/jobs/get?job_id=123" --profile dev
# POST with an inline JSON body
databricks api post /api/2.1/jobs/run-now --json '{"job_id": 123}' --profile dev
# POST with the body read from a file
databricks api post /api/2.1/jobs/run-now --json @run-request.json --profile devThe --profile flag selects a profile from ~/.databrickscfg. Without it, the CLI uses the default profile or the DATABRICKS_HOST and DATABRICKS_TOKEN variables.
Combining it with jq
The output is JSON, so it pipes into jq like any other CLI output. This script starts a run and then reads its state.
run_id=$(databricks api post /api/2.1/jobs/run-now \
--json '{"job_id": 123}' | jq -r '.run_id')
databricks api get "/api/2.1/jobs/runs/get?run_id=$run_id" \
| jq -r '.state.life_cycle_state'This is the same flow as the curl and Python examples, with less code. You do not manage tokens or the host URL yourself.
Common errors
| Symptom | Likely cause |
|---|---|
| 404 Not Found | The path is wrong, or the API version is missing or outdated. Compare it with the API reference. |
| 401 or 403 | The profile's token has expired, or the identity lacks permission on the resource. Run `databricks current-user me` to check which identity the CLI is using. |
| 400 Bad Request | The JSON body is malformed or a field name is wrong. The error message usually names the field. |
| Query parameters ignored | The shell split or expanded the `?` or `&`. Wrap the path in double quotes. |
| Call goes to the wrong workspace | The wrong profile was used. Pass `--profile` explicitly in scripts. |
When to choose it
Pick databricks api for shell scripts that need one or two raw calls and already depend on the CLI. Pick curl when the CLI is not installed. Pick the SDK when you need retries, pagination, or typed results in Python.
Exercise
A script runs databricks api get /api/2.1/jobs/runs/get?run_id=$id and sometimes returns the wrong run or an error, depending on the shell. What is the likely problem, and how do you fix it?
Show answer
The unquoted ? can be treated as a glob character by the shell, and an unquoted & would also end the command. Wrap the whole path in double quotes, as in "/api/2.1/jobs/runs/get?run_id=$id". Double quotes still let the shell expand $id.