---
title: Use AI Search with an OAuth token
description: Query a Databricks AI Search endpoint with the Python SDK or HTTP using a service principal OAuth token over the network-optimized path for lower latency.
canonical_url: https://docs.databricks.com/gcp/en/ai-search/vector-search-oauth-token
---

# Use AI Search with an OAuth token

[Open notebook version of this page](https://docs.databricks.com/notebooks/source/generative-ai/vector-search-oauth-token.html)

This notebook shows how to call an AI Search endpoint using the AI Search SDK or HTTP with a fresh OAuth token. In both cases, the network optimized path is used, as is recommended for any production workload.

The HTTP calls for creating a token and calling the endpoint can be implemented in a language of your choice. For production applications, keep in mind that the token must be refreshed every 60 minutes. To prevent errors due to a stale token, Databricks recommends refreshing it at an interval of less than 60 minutes.

## Setup

```python
%pip install databricks-sdk
%pip install databricks-ai-search
dbutils.library.restartPython()
```

```python
import requests
import json
import random
from databricks.sdk import WorkspaceClient
import logging
```

## Configuration Constants

For details on how to create a service principal, see the [Databricks documentation](https://docs.databricks.com/gcp/en/ai-search/endpoint-load-test.md#step-1).

```python
# Define the secret ID and secret for the service principal
CLIENT_ID = dbutils.secrets.get(scope="scope", key="service_principal_client_id")
CLIENT_SECRET = dbutils.secrets.get(scope="scope", key="service_principal_client_secret")

# You can get it by clicking the copy button next to the index name
INDEX_NAME="UC_CATALOG_INDEX_NAME"
ENDPOINT_NAME="ENDPOINT_NAME"

workspace_url = f"https://{spark.conf.get('spark.databricks.workspaceUrl')}"
```

## Query AI Search using the Python client

For details, see the [API documentation](https://api-docs.databricks.com/python/vector-search/databricks.vector_search.html#databricks.vector_search.index.VectorSearchIndex.similarity_search).

```python
from databricks.ai_search.client import AISearchClient

vsc_dp = AISearchClient(
  service_principal_client_id=CLIENT_ID,
  service_principal_client_secret=CLIENT_SECRET,
  workspace_url=workspace_url)

index = vsc_dp.get_index(endpoint_name=ENDPOINT_NAME, index_name=INDEX_NAME)
index.similarity_search(["text"], query_vector=[0]*2560, num_results=5, debug_level=0)
```

## Call AI Search using an HTTP request

This section shows how to call an AI Search index using HTTP, which you can then implement in a tool or language of your choice.

### Generate an OAuth token using an API call

To call an endpoint using a network optimized path, you need an OAuth token. The following code creates the token.

```python
url = f"{workspace_url}/oidc/v1/token"
deets = json.dumps([
                {
                    "type": "unity_catalog_permission",
                    "securable_type": "table",
                    "securable_object_name": INDEX_NAME,
                    "operation": ("ReadVectorIndex"),
                },
            ])
payload = { 'grant_type': 'client_credentials', 'scope': 'all-apis', 'authorization_details': deets}

response = requests.post(
    url=url,
    auth=(CLIENT_ID, CLIENT_SECRET),
    headers={"Content-Type": "application/x-www-form-urlencoded"},
    data=payload,
)
if response.status_code != 200:
    logging.error(f"OAuth token request failed: {response.status_code} - {response.text}")
    response.raise_for_status()

token_data = response.json()
access_token = token_data.get("access_token")
if not access_token:
    raise ValueError("Failed to get access token")
```

### Query AI Search using Python client

To use JavaScript or some other tool to query AI Search, create the query and send it to the AI Search endpoint.

```python
index = vsc_dp.get_index(endpoint_name=ENDPOINT_NAME, index_name=INDEX_NAME)
index_url = index.index_url
print(index_url)

headers = {"Authorization": f"Bearer {access_token}"}
payload = {
    "query_vector": [0]*2560,
    "num_results": 5,
    "columns": ["text"]}

response = requests.post(index_url+"/query", headers=headers, data=json.dumps(payload))
print(response.status_code)
print(json.dumps(response.json(), indent=2))
```

## Example notebook

#### Use AI Search with an OAuth token

[Get notebook](https://docs.databricks.com/notebooks/source/generative-ai/vector-search-oauth-token.html)
