Connect local machine to Databricks App
This recipe demonstrates how to connect from your local machine to an API deployed as a Databricks App.
While the FastAPI application running locally on http://127.0.0.1:8000 does not require a valid bearer token, this token is required for accessing Databricks Apps via the secured HTTPS URL with /api
endpoints.
Generate OAuth2 Bearer token
The easiest method to generate a valid token is to use the Databricks CLI which allows you to authenticate with the workspace where the Databricks App is hosted.
You can log in to a workspace via the following CLI command:
databricks auth login --host https://<databricks-workspace-url> --profile my-env
Upon logging in successfully via the browser, you will see that your profile has been saved. This is helpful if you need to test your app across multiple environments.
Profile my-env was successfully saved
A bearer token can then be generated with a limited time-to-live. Make sure to temporarily store the access_token
details for use later on.
databricks auth token --profile my-env
{
"access_token": "ey....<REDACTED>",
"token_type": "Bearer",
"expiry": "2025-04-14T21:11:13.933142+01:00"
}
Code snippets
The following examples below show how the FastAPI application can be called using the bearer token.
cURL
curl -X GET "https://your-app-url/api/v1/healthcheck" \
-H "Authorization: Bearer YOUR_DATABRICKS_TOKEN"
Python
import requests
response = requests.get(
"https://your-app-url/api/v1/healthcheck",
headers={"Authorization": f"Bearer YOUR_DATABRICKS_TOKEN"}
)
print(response.json())
If you would like to avoid storing the token in your code, you can leverage the profile you have created using the Databricks CLI through the Databricks SDK for Python directly.
from databricks.sdk.core import Config
import requests
config = Config(profile="my-env")
token = config.oauth_token().access_token
response = requests.get(
"https://your-app-url/api/v1/healthcheck",
headers={"Authorization": f"Bearer {token}"},
)
print(response.json())
Permissions
For local connectivity and authentication, the connecting user(s)/group(s) needs the following permissions:
CAN USE
on the target app
Dependencies
- Databricks SDK for Python -
databricks-sdk
- Requests -
requests
databricks-sdk
requests