Sub topic 10
Pagination, error responses, and rate limits
This chapter assumes you can already call the REST API with a bearer token (see the previous chapters) and read JSON in Python. Real scripts must also handle three things: results that arrive in pages, requests that fail, and requests that the server throttles.
Pagination
List endpoints do not return everything at once. The Jobs API jobs/list call returns a limited page of jobs. Its limit parameter caps the page size (the maximum is 100). The response includes has_more and, when more pages exist, a next_page_token. You pass that token back as page_token to fetch the next page.
If you read only the first response, you silently miss jobs. This is the most common bug in hand-written scripts.
Not every endpoint paginates the same way. Some use page_token, and some use offset and limit. Check the reference page for each endpoint.
Error responses
When a request fails, Databricks returns a non-2xx HTTP status. The body is usually JSON with an error_code and a message:
{"error_code": "RESOURCE_DOES_NOT_EXIST", "message": "Job 999 does not exist."}| Status | Typical meaning | Retry? |
|---|---|---|
| 400 | Bad request, such as `INVALID_PARAMETER_VALUE` | No. Fix the request. |
| 401 | Missing, invalid, or expired credentials | No. Re-authenticate. |
| 403 | The identity lacks permission | No. Change the permissions. |
| 404 | The resource does not exist | No. |
| 429 | Too many requests | Yes, after waiting. |
| 500, 503 | Server-side or temporary failure | Yes, with backoff. |
Rate limits
When you exceed the rate limit, the API answers 429 Too Many Requests. The response often has a Retry-After header that gives the number of seconds to wait. If it is absent, use exponential backoff: wait 1 second, then 2, then 4, and so on, up to a fixed number of attempts.
A complete example
This script combines retries with pagination. It lists every job, whatever the number of pages.
import os
import time
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
headers = {"Authorization": f"Bearer {os.environ['DATABRICKS_TOKEN']}"}
def get(path, params=None, max_attempts=5):
for attempt in range(max_attempts):
r = requests.get(f"{host}{path}", headers=headers, params=params, timeout=30)
if r.status_code == 429 or r.status_code >= 500:
time.sleep(float(r.headers.get("Retry-After", 2 ** attempt)))
continue
if not r.ok:
raise RuntimeError(f"{r.status_code}: {r.text}")
return r.json()
raise RuntimeError(f"Gave up on {path} after {max_attempts} attempts")
def list_jobs():
params = {"limit": 100}
while True:
page = get("/api/2.1/jobs/list", params)
yield from page.get("jobs", [])
token = page.get("next_page_token")
if not page.get("has_more") or not token:
break
params = {"limit": 100, "page_token": token}
for job in list_jobs():
print(job["job_id"], job["settings"]["name"])Note that page.get("jobs", []) is needed because the jobs field is absent when the workspace has no jobs.
The Databricks SDK, covered in a later chapter, implements this retry and pagination logic for you.
Exercise
A script calls jobs/list once and prints 100 jobs, but the workspace has 250. Which response fields should the script check, and what should it do with them?
Show answer
It should check has_more and next_page_token. While has_more is true, it should send the next request with page_token set to the returned next_page_token, and repeat until has_more is false.
Exercise
An API call returns 403 with error_code PERMISSION_DENIED. Should the script retry with backoff? Why or why not?
Show answer
No. A 403 means the identity lacks permission, and waiting does not change that. Only 429 and temporary 5xx errors are worth retrying. Fix the permissions instead.