Sub topic 9
Calling any endpoint with databricks api
This chapter assumes you have a working CLI with a saved profile (see the chapters on authentication and profiles). It also assumes you can read a REST API reference page and follow the curl example from the REST API basics chapter. You don't need new concepts. databricks api is the curl call from earlier, with the CLI handling authentication.
What the command does
The typed command groups, such as databricks jobs and databricks clusters, cover most of the API. The CLI is released on its own schedule, so some endpoints have no typed command yet. This is common for preview features and recently added endpoints. databricks api fills that gap. It sends an HTTP request to any path on your workspace host and prints the response body.
The command does four things for you:
- It resolves credentials the same way every other CLI command does: the
--profileflag, then environment variables, then the default profile. - It refreshes OAuth tokens when they expire, so a long-running script doesn't break.
- It adds the
Authorizationheader and builds the full URL from the host and the path you give it. - It exits with a non-zero status when the API returns an error, so
set -eandifstatements work.
It does not page through results, retry, or validate your request against a schema. You are sending raw HTTP, with less to type.
Syntax
The first argument is the HTTP method, written as a subcommand. The second is the path, which starts with / and includes the API version.
databricks api get /api/2.0/preview/scim/v2/MeThe available methods are get, post, put, patch, delete, and head. Run databricks api --help to confirm what your installed version supports.
Useful flags:
| Flag | Purpose |
|---|---|
| `--json '{...}'` | Request body as an inline JSON string. |
| `--json @file.json` | Request body read from a file. |
| `-p`, `--profile` | Use a named profile from `~/.databrickscfg`. |
| `--debug` | Print request details to help diagnose failures. |
| `-o`, `--output` | Output format. Responses are JSON either way, so you rarely need this. |
Do not paste a real token into --debug output that you share. The debug log can include request headers. Check it before posting it in a ticket.
GET requests and query parameters
Put query parameters in the path. Quote the whole path, because ? and & have meaning to the shell.
databricks api get "/api/2.1/jobs/runs/get?run_id=456"
databricks api get "/api/2.1/jobs/list?limit=5&expand_tasks=false" -p stagingRecent CLI versions also accept --json on a get and convert the body to query parameters. This is handy for long parameter lists, but behavior has varied between versions. Query strings in the path work everywhere, so use them in scripts that run on different machines.
POST requests with a body
Send the body with --json. This is the same call as the curl example in the REST API chapter:
databricks api post /api/2.1/jobs/run-now --json '{"job_id": 123}'For larger bodies, keep the JSON in a file. The file can be reviewed and committed, and it avoids shell quoting problems.
cat > run_request.json <<'EOF'
{
"job_id": 123,
"job_parameters": { "run_date": "2026-12-04" }
}
EOF
databricks api post /api/2.1/jobs/run-now --json @run_request.jsonCheck the endpoint's reference page for which fields it accepts. A field the endpoint doesn't recognize is usually rejected with a 400 error, but some endpoints ignore unknown fields. A typo in an optional field can therefore silently change nothing.
Processing responses with jq
Output goes to stdout as JSON, so jq works as it does with typed commands. Capture the run ID, then poll the run:
#!/usr/bin/env bash
set -euo pipefail
job_id="${1:?usage: run_job.sh JOB_ID}"
run_id=$(databricks api post /api/2.1/jobs/run-now \
--json "{\"job_id\": ${job_id}}" | jq -r '.run_id')
echo "Started run ${run_id}"
while :; do
resp=$(databricks api get "/api/2.1/jobs/runs/get?run_id=${run_id}")
life_cycle=$(jq -r '.state.life_cycle_state' <<<"$resp")
case "$life_cycle" in
TERMINATED|SKIPPED|INTERNAL_ERROR) break ;;
esac
sleep 15
done
result=$(jq -r '.state.result_state // "NONE"' <<<"$resp")
echo "Finished: ${result}"
[ "$result" = "SUCCESS" ]The last line makes the script's exit code reflect the job outcome, so a CI step fails when the job fails. A run that ends in INTERNAL_ERROR or SKIPPED has no result_state, which is why the script falls back to NONE. That also produces a non-zero exit.
Pagination is manual
Typed commands like databricks jobs list fetch every page for you. databricks api returns exactly one response. If the endpoint pages its results, you loop yourself. The Jobs 2.1 list endpoint returns a next_page_token when more results exist, and you pass it back as page_token.
#!/usr/bin/env bash
set -euo pipefail
token=""
while :; do
path="/api/2.1/jobs/list?limit=25"
if [ -n "$token" ]; then
path="${path}&page_token=${token}"
fi
resp=$(databricks api get "$path")
jq -r '.jobs[]? | "\(.job_id)\t\(.settings.name)"' <<<"$resp"
token=$(jq -r '(.next_page_token // empty) | @uri' <<<"$resp")
[ -n "$token" ] || break
doneThree details matter here:
.jobs[]?tolerates a response with nojobskey, which is what an empty workspace returns.@uripercent-encodes the token. Page tokens can contain characters that are not safe in a URL.- Pagination conventions differ between APIs. Some use
next_page_token, some useoffsetandlimit, and some return everything at once. Read the reference page for each endpoint before you write the loop.
Reading errors
When the API rejects a request, the CLI prints the error message and exits non-zero. The usual causes are the same as with curl:
| Status | Typical cause | What to check |
|---|---|---|
| 400 | Malformed or invalid body or parameters | JSON syntax, field names, required fields |
| 401 | Missing, expired, or wrong credentials | `databricks current-user me` with the same profile |
| 403 | Authenticated but not permitted | The identity's permissions on that resource |
| 404 | Wrong path, or the resource doesn't exist | API version in the path, the ID you passed |
| 429 | Rate limit exceeded | Add a delay and retry |
A 404 on a path you copied from the docs often means the host is wrong. The command is talking to a different workspace than you intended, or an account-level path is being sent to a workspace host. Run databricks auth profiles and confirm which host the profile points to. Use --debug to see the exact URL.
The CLI does not retry 429 responses for you in a way you can control from the command line. In scripts that call many endpoints, add a sleep between calls and wrap the call in a small retry function.
retry() {
local attempts=5 delay=5 n=1
until "$@"; do
if [ "$n" -ge "$attempts" ]; then
echo "Failed after ${attempts} attempts: $*" >&2
return 1
fi
echo "Attempt ${n} failed, retrying in ${delay}s" >&2
sleep "$delay"
n=$((n + 1))
delay=$((delay * 2))
done
}
retry databricks api get "/api/2.1/jobs/runs/get?run_id=456"This retries every failure, including a 400 that can never succeed. For a production script, inspect the error text and retry only on 429 and 5xx responses.
Choosing between the tools
| Tool | Best when | Drawback |
|---|---|---|
| Typed command (`databricks jobs ...`) | A command exists for the task | Not available for every endpoint |
| `databricks api` | No typed command exists, or you want the exact REST shape | No pagination, no schema checks |
| `curl` or `requests` | The machine has no CLI, or you embed calls in another program | You manage tokens and refresh yourself |
| SDK | You write Python and want pagination and typed objects | Needs a release that includes the endpoint |
A practical rule: use the typed command first, fall back to databricks api when it is missing, and move to the SDK once a script grows loops, branching, or error handling that is awkward in bash.
Account-level paths
Account-level endpoints, such as account-wide user or workspace management, live on a different host and need a profile configured for the account. If databricks api get /api/2.0/accounts/... returns 404 against a workspace profile, that is the reason. The last chapter of this tutorial covers the difference.
Exercise
Write one command that starts a run of job 123 using a profile named staging, with the body stored in a file named body.json. Then say why databricks api get /api/2.1/jobs/list may not return every job in a large workspace.
Show answer
databricks api post /api/2.1/jobs/run-now --json @body.json -p staging, where body.json contains at least {"job_id": 123}. The api command returns a single response and does not follow pages. When more jobs exist than fit in one page, the response includes a next_page_token. You must pass it back as page_token in a follow-up request, repeating until no token is returned.
Exercise
A script uses databricks api get /api/2.1/jobs/runs/get?run_id=456&x=1 and behaves strangely. What is the likely shell problem, and how do you fix it?
Show answer
The unquoted & makes the shell run the command in the background and treat x=1 as a separate command, so the path is cut off at run_id=456. Quote the whole path: databricks api get "/api/2.1/jobs/runs/get?run_id=456&x=1".