To access an Agent serving endpoint without a Personal Access Token (PAT), you must use OAuth 2.0 Machine-to-Machine (M2M) authentication. This is the industry-standard approach for production applications.
1. OAuth M2M Authentication Workflow
Instead of a long-lived PAT, you use a Service Principal (an identity for your app) and a Client Secret to request short-lived (1-hour) access tokens.
Setup Steps
- Create a Service Principal: In your Databricks workspace, go to Settings > User Management > Service Principals.
- Generate a Secret: Select the service principal, go to the Secrets tab, and click Generate secret. Save the Client ID and Client Secret.
- Assign Permissions: Go to the Serving tab, select your agent endpoint, and under Permissions, grant your Service Principal Can Query permissions.
2. Access via Python (Databricks SDK)
The Databricks SDK handles the token lifecycle (fetching and refreshing) automatically if you provide the credentials.
import os
from databricks.sdk import WorkspaceClient
# Credentials should be stored in environment variables for security
w = WorkspaceClient(
host="https://<workspace-instance-name>.cloud.databricks.com",
client_id=os.environ.get("DATABRICKS_CLIENT_ID"),
client_secret=os.environ.get("DATABRICKS_CLIENT_SECRET")
)
# Querying the agent endpoint
response = w.serving_endpoints.query(
name="my-agent-endpoint",
messages=[{"role": "user", "content": "How do I use this agent?"}]
)
print(response.choices[0].message.content)
3. Access via REST API
If you aren't using the Python SDK, you must manually fetch the token first.
Step 1: Fetch the OAuth Token
# Token URL format: https://<workspace-instance>/oidc/v1/token
curl -X POST "https://<workspace-instance>.cloud.databricks.com/oidc/v1/token" \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d "grant_type=client_credentials&scope=all-apis"
Step 2: Query the Agent Endpoint
curl -X POST "https://<workspace-instance>.cloud.databricks.com/serving-endpoints/my-agent-endpoint/invocations" \
-H "Authorization: Bearer <access_token_from_step_1>" \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello agent!"}]}'
Documentation Links