Query route-optimized serving endpoints

Query a route-optimized model serving or feature serving endpoint from an application or AI coding agent with an endpoint-scoped OAuth token. This page covers the route-optimized URL, the token, and an end-to-end script.

Requirements

Quick start: end-to-end query recipe

Run the following script in a shell. It creates a service principal, mints an endpoint-scoped token, and queries the endpoint. The sections below explain each step.

# 1. Set the variables for your environment.
export DATABRICKS_HOST="https://<workspace-host>"
export ENDPOINT_NAME="<your-endpoint>"
export WORKSPACE_ID="<workspace-id>"

# 2. Create an account-level service principal and an OAuth secret for it.
SP_ID=$(databricks account service-principals create \
  --json '{"displayName":"my-app","active":true}' --output json | jq -r '.id')
SECRET_JSON=$(databricks account service-principal-secrets create "$SP_ID" --output json)
export CLIENT_ID=$(databricks account service-principals get "$SP_ID" --output json | jq -r '.applicationId')
export CLIENT_SECRET=$(echo "$SECRET_JSON" | jq -r '.secret')

# 3. Assign the service principal to the workspace and grant CAN_QUERY on the endpoint.
databricks account workspace-assignment update "$WORKSPACE_ID" "$SP_ID" \
  --json '{"permissions":["USER"]}'
ENDPOINT_ID=$(databricks serving-endpoints get "$ENDPOINT_NAME" --output json | jq -r '.id')
databricks permissions update serving-endpoints "$ENDPOINT_ID" \
  --json "{\"access_control_list\":[{\"service_principal_name\":\"$CLIENT_ID\",\"permission_level\":\"CAN_QUERY\"}]}"

# 4. Mint an endpoint-scoped OAuth token. `authorization_details` is required for
# route-optimized endpoints. A plain `scope=all-apis` token is rejected with
# 401 "Missing authorization details" when used against the route-optimized URL.
TOKEN=$(curl -sS -X POST -u "$CLIENT_ID:$CLIENT_SECRET" \
  --data-urlencode "grant_type=client_credentials" \
  --data-urlencode "scope=all-apis" \
  --data-urlencode "authorization_details=[{\"type\":\"workspace_permission\",\"object_type\":\"serving-endpoints\",\"object_path\":\"/serving-endpoints/$ENDPOINT_ID\",\"actions\":[\"query_inference_endpoint\"]}]" \
  "$DATABRICKS_HOST/oidc/v1/token" | jq -r '.access_token')

# 5. Invoke the endpoint at its route-optimized URL.
RO_URL=$(databricks serving-endpoints get "$ENDPOINT_NAME" --output json | jq -r '.endpoint_url')
curl -sS -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"inputs":[[0.12,0.34]]}' "https://$RO_URL"

A successful run prints a JSON response from the endpoint instead of an error.

Fetch the route-optimized URL

Warning

Starting September 22, 2025, all newly created route-optimized endpoints must be queried only through the route-optimized URL. Endpoints created after this date don't support querying through the workspace URL.

If your route-optimized endpoint was created before September 22, 2025, it accepts both URLs:

  • The route-optimized URL, which the endpoint returns in its endpoint_url field.
  • The standard workspace URL, https://<databricks-workspace>/serving-endpoints/<endpoint-name>/invocations. It doesn't get the benefits of route optimization.

A route-optimized endpoint has a URL in this format:

https://<unique-id>.<n>.serving.azuredatabricks.net/<workspace-id>/serving-endpoints/<endpoint-name>/invocations

<n> is a single digit.

Copy the endpoint_url value instead of building the URL. To get it from the CLI, run the command from the quick start:

databricks serving-endpoints get "$ENDPOINT_NAME" --output json | jq -r '.endpoint_url'

The command prints the URL without a scheme. Add https:// to the front before you call it.

In the Serving UI, the URL appears on the endpoint details page:

Endpoint details page in the Serving UI showing the route-optimized endpoint URL to copy for queries

For the REST API, see Get serving endpoint. The URL is in the endpoint_url field, which is populated only for route-optimized endpoints.

Fetch an OAuth token and query the endpoint

Route-optimized endpoints accept an OAuth token that carries authorization_details for the endpoint. Tokens with authorization_details are valid for up to 1 hour, so refresh them before they expire.

Databricks recommends service principals for production applications.

Fetch a test token in the Serving UI

For development and testing, select the Use button on the endpoint details page, select the Fetch Token tab, and select Fetch OAuth Token. The token is valid for 1 hour. Send the query with the endpoint URL and this token. The query example is under Fetch an OAuth token manually.

Fetch an OAuth token programmatically

For production, embed a service principal in your application to fetch OAuth tokens programmatically. Follow the steps in Authorize service principal access to Azure Databricks with OAuth through step 2 to create the service principal, assign permissions, and create an OAuth secret. Then give the service principal at least Can Query permission on the endpoint. See Manage permissions on a model serving endpoint.

For Python, see Query with the Databricks SDK.

Fetch an OAuth token manually

Use this method when your client can't use the Serving UI or the Databricks SDK. When you fetch the token manually, you must specify authorization_details in the request.

  • Construct the <token-endpoint-URL> by replacing https://<workspace-host> with the workspace URL of your Azure Databricks deployment in https://<workspace-host>/oidc/v1/token. For example, https://my-workspace.0.azuredatabricks.net/oidc/v1/token.
  • Replace <client-id> with the service principal's client ID, which is also known as an application ID.
  • Replace <client-secret> with the service principal's OAuth secret that you created.
  • Replace <endpoint-id> with the endpoint ID of the route-optimized endpoint. This is the alpha-numeric ID of the endpoint that you can find in the hostName of the endpoint URL. For example, if the serving endpoint is https://abcdefg.0.serving.azuredatabricks.net/9999999/serving-endpoints/test, the endpoint ID is abcdefg.
  • Replace <action> with the action permission given to the service principal. The action can be query_inference_endpoint or manage_inference_endpoint.

Request the token with a POST to the token endpoint:

export CLIENT_ID=<client-id>
export CLIENT_SECRET=<client-secret>
export ENDPOINT_ID=<endpoint-id>
export ACTION=<action>  # for example, 'query_inference_endpoint'

curl --request POST \
--url <token-endpoint-URL> \
--user "$CLIENT_ID:$CLIENT_SECRET" \
--data 'grant_type=client_credentials&scope=all-apis' \
--data-urlencode 'authorization_details=[{"type":"workspace_permission","object_type":"serving-endpoints","object_path":"'"/serving-endpoints/$ENDPOINT_ID"'","actions": ["'"$ACTION"'"]}]'

The response is JSON with an access_token field. Use its value as the token in the next step.

Query the endpoint with the route-optimized URL and the token. Set URL to that URL with https:// in front:

URL="<endpoint-url>"
OAUTH_TOKEN="<token>"

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OAUTH_TOKEN" \
  -d '{"dataframe_split": {"columns": ["feature_1", "feature_2"], "data": [[0.12, 0.34], [0.56, 0.78]]}}' \
  "$URL"

For a custom model, the request body must hold exactly one of the keys dataframe_split, dataframe_records, inputs, or instances. Use the format that matches your model's input signature. For each format, see Format scoring requests.

A successful call returns JSON from the endpoint.

To fetch the token and query the endpoint from Python without the SDK, use requests. The script reads the same three environment variables as the curl request. Replace <token-endpoint-URL> and <endpoint-url> as in the steps above, then run the following:

import json
import os

import requests

CLIENT_ID = os.environ["CLIENT_ID"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
ENDPOINT_ID = os.environ["ENDPOINT_ID"]
ACTION = "query_inference_endpoint"  # Can also be `manage_inference_endpoint`
TOKEN_URL = "<token-endpoint-URL>"
URL = "<endpoint-url>"

token_response = requests.post(
    TOKEN_URL,
    auth=(CLIENT_ID, CLIENT_SECRET),
    data={
        "grant_type": "client_credentials",
        "scope": "all-apis",
        "authorization_details": json.dumps([{
            "type": "workspace_permission",
            "object_type": "serving-endpoints",
            "object_path": f"/serving-endpoints/{ENDPOINT_ID}",
            "actions": [ACTION],
        }]),
    },
)
token_response.raise_for_status()
oauth_token = token_response.json()["access_token"]

response = requests.post(
    URL,
    headers={"Authorization": f"Bearer {oauth_token}"},
    json={"dataframe_split": {"columns": ["feature_1", "feature_2"], "data": [[0.12, 0.34], [0.56, 0.78]]}},
)
print(response.status_code, response.json())

A successful run prints 200 and the endpoint's JSON response. The Databricks SDK fetches the token for you. See Query with the Databricks SDK.

Query with the Databricks SDK

The Databricks Python SDK provides an API to query a route-optimized endpoint directly.

Note

The Databricks SDK is also available in Go, see Databricks SDK for Go.

This example needs the following:

  • Serving endpoint name (the SDK fetches the correct endpoint URL based on this name)
  • Service principal client ID
  • Service principal secret
  • Workspace hostname

Install the SDK with pip install databricks-sdk, then run the following:

from databricks.sdk import WorkspaceClient
import databricks.sdk.core as client

endpoint_name = "<Serving-Endpoint-Name>" ## Insert the endpoint name here

# Initialize Databricks SDK
c = client.Config(
    host="<Workspace-Host>", ## For example, my-workspace.cloud.databricks.com
    client_id="<Client-Id>", ## Service principal ID
    client_secret="<Secret>"   ## Service principal secret
)
w = WorkspaceClient(
    config = c
)

response = w.serving_endpoints_data_plane.query(name=endpoint_name, dataframe_records=[{"feature_1": 0.12, "feature_2": 0.34}])

The call returns a QueryEndpointResponse object. For a custom model, the output is in response.predictions.

Query from an agent or external application

AI coding assistants and external applications that query route-optimized endpoints can't use a personal access token, or an OAuth token without endpoint authorization details. For production, Databricks recommends a service principal with OAuth machine-to-machine (M2M) authentication. The quick start script covers every step, from creating the service principal to querying the endpoint.

Troubleshooting

401 Malformed token

The token is a personal access token or a cluster runtime token rather than an OAuth JSON Web Token (JWT).

Recommended solution: Use a service principal with the OAuth M2M flow to fetch an OAuth token. See Fetch an OAuth token programmatically.

401 Missing authorization details for accessing model serving endpoints

The token request omitted the authorization_details claim that limits the token to the specific endpoint. A plain scope=all-apis token is not sufficient.

Recommended solution: Pass authorization_details that references the endpoint ID and the query_inference_endpoint action when you call /oidc/v1/token. See Fetch an OAuth token manually.

400 This is a route-optimized endpoint

The request went to the workspace URL, https://<workspace>/serving-endpoints/<name>/invocations, instead of the route-optimized URL.

Recommended solution: Send the request to the URL in the endpoint_url field of GET /api/2.0/serving-endpoints/<name>. See Fetch the route-optimized URL.

401 Token is not authorized to access the requested endpoint

The token was minted with authorization_details for a different endpoint.

Recommended solution: Mint a new token with object_path set to /serving-endpoints/<endpoint-id> for the endpoint you are querying. See Fetch an OAuth token manually.

401 Expired token

The token is older than its 1-hour lifetime.

Recommended solution: Mint a new token and send the query again. See Fetch an OAuth token programmatically.

401 invalid_authorization_details returned from /oidc/v1/token

The token request returns this error description:

User is not authorized to the requested authorizations

The service principal doesn't have CAN_QUERY on the endpoint, the endpoint ID in object_path is wrong, or the action in authorization_details doesn't match the granted permission.

Recommended solution: Grant CAN_QUERY on the endpoint to the service principal, set object_path to /serving-endpoints/<endpoint-id>, and use query_inference_endpoint as the action. Then request a new token. See Manage permissions on a model serving endpoint.

Check: The token request returns an access_token field.

invalid_scope returned from /oidc/v1/token

The token request passed a scope value other than all-apis. The only supported scope for route-optimized endpoint tokens is all-apis.

Recommended solution: Send scope=all-apis. The endpoint restriction goes in authorization_details rather than in scope.

Check: The token request returns an access_token field.

Additional resources

Resource Description
Route optimization for Model Serving endpoints Learn how to enable route optimization on a serving endpoint.
Authorize service principal access to Azure Databricks with OAuth Learn how to authorize a service principal with OAuth.