Sub topic 7
REST API basics
This chapter assumes you can set environment variables in a shell, read JSON, and know what an HTTP method and status code are. It also assumes you have a Databricks workspace and either an OAuth login or a personal access token (PAT), covered in the authentication chapter.
The shape of a request
Every Databricks REST call is an HTTPS request with three parts:
- Host. The workspace URL, for example
https://<workspace-host>. Each workspace has its own host, so the host selects which workspace you act on. - Path. It names the API version, the service, and the operation, such as
/api/2.1/jobs/list. - Authorization header. It carries a credential in the form
Authorization: Bearer <token>. A bearer token grants access to whoever presents it, so treat it like a password.
Methods and versions
Databricks uses ordinary HTTP methods. GET reads data and passes parameters in the query string. POST creates something or starts an action, and it takes a JSON body. Some endpoints use PUT, PATCH, or DELETE.
The version in the path matters. The Jobs API is available at /api/2.1/, while other services use /api/2.0/. Take the version from the API reference page for the endpoint you call. Do not assume it matches the version used by another service.
A first call
Export the host and token once, then call a read-only endpoint. The -sS flags hide the progress meter but still print errors.
export DATABRICKS_HOST="https://<workspace-host>"
export DATABRICKS_TOKEN="<your-token>"
curl -sS "$DATABRICKS_HOST/api/2.1/jobs/list?limit=5" \
-H "Authorization: Bearer $DATABRICKS_TOKEN"The reply is a JSON object. For jobs, the list sits under a jobs key, and the object may also contain has_more and next_page_token fields. You will use those in the pagination chapter.
The same call in Python, using the requests library:
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
headers = {"Authorization": f"Bearer {os.environ['DATABRICKS_TOKEN']}"}
resp = requests.get(f"{host}/api/2.1/jobs/list", headers=headers, params={"limit": 5})
resp.raise_for_status()
for job in resp.json().get("jobs", []):
print(job["job_id"], job["settings"]["name"])Use .get("jobs", []) because the key is missing when the workspace has no jobs. Indexing it directly raises a KeyError on an empty workspace.
Common errors
Check the HTTP status code before you parse the body.
| Status | Usual cause | What to check |
|---|---|---|
| 400 | Malformed JSON or a bad parameter | The request body and field names |
| 401 | Missing, expired, or wrong token | The token value and the host it belongs to |
| 403 | Valid identity without permission | The identity's access to that object |
| 404 | Wrong path, version, or ID | The URL, including `/api/2.1/` |
| 429 | Too many requests | Slow down and retry later |
A 401 often means the token belongs to a different workspace than the host you called. A trailing slash on the host also produces a bad URL, which is why the Python example strips it.
Exercise
A GET request to $DATABRICKS_HOST/api/2.1/jobs/list returns status 401, and the token was created yesterday and has not been revoked. Name two likely causes and how you would check each.
Show answer
First, the token may belong to a different workspace than the host in the URL. Check that the host and the token come from the same workspace. Second, the Authorization header may be malformed, for example a missing Bearer prefix or an empty $DATABRICKS_TOKEN variable. Run echo "${DATABRICKS_TOKEN:+set}" to confirm the variable is set, and check the header text.