Call REST APIs directly from agent code

Use the Unity Catalog connections proxy endpoint with the external service's own client SDK to call REST APIs directly from agent code. Point the SDK's base URL to the proxy endpoint and use your Azure Databricks token as the API key. Azure Databricks authenticates the request and automatically injects the external service's credentials from the Unity Catalog connection. Your code doesn't handle the external service's tokens directly.

Requirements

  • A Unity Catalog HTTP connection for the external service. The proxy endpoint uses this connection to authenticate and inject the service's credentials.
  • USE CONNECTION on the connection object.

OpenAI

Use DatabricksOpenAI to route calls to external OpenAI through the Unity Catalog connections proxy. First, create a Unity Catalog HTTP connection using your OpenAI API key stored as a Databricks secret:

CREATE CONNECTION openai_connection TYPE HTTP
OPTIONS (
  host 'https://api.openai.com',
  base_path '/v1',
  bearer_token secret ('<secret-scope>', '<secret-key>')
);

Then install the databricks-openai package and use the proxy URL and workspace client in your agent code:

pip install databricks-openai
from databricks_openai import DatabricksOpenAI
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

client = DatabricksOpenAI(
    workspace_client=w,
    base_url=f"{w.config.host}/api/2.0/unity-catalog/connections/openai_connection/proxy/",
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)

Slack

Configure the Slack SDK to route through the Unity Catalog connections proxy. Create a Unity Catalog HTTP connection with host https://slack.com and base path /api, then use the proxy URL as the SDK base URL:

from slack_sdk import WebClient
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

client = WebClient(
    token=w.config.authenticate()["Authorization"].split(" ")[1],
    base_url=f"{w.config.host}/api/2.0/unity-catalog/connections/slack_connection/proxy/",
)

result = client.chat_postMessage(channel="C123456", text="Hello from Databricks!")
print(result["message"]["text"])

Generic HTTP

For services without a dedicated SDK, use the requests library with the proxy URL directly:

import requests
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

response = requests.post(
    f"{w.config.host}/api/2.0/unity-catalog/connections/my_connection/proxy/api/v1/resource",
    headers={
        **w.config.authenticate(),
        "Content-Type": "application/json",
    },
    json={"key": "value"},
)

For details on the proxy endpoint, supported authentication methods, and connection setup, see Forward requests through the HTTP connection proxy.