|
| 1 | +--- |
| 2 | +manager: nitinme |
| 3 | +author: haileytap |
| 4 | +ms.author: haileytapia |
| 5 | +ms.service: azure-ai-search |
| 6 | +ms.topic: include |
| 7 | +ms.date: 07/09/2025 |
| 8 | +--- |
| 9 | + |
| 10 | +In this quickstart, you use role-based access control (RBAC) and Microsoft Entra ID to establish a keyless connection to your Azure AI Search service. You then use Python in Visual Studio Code to interact with your service. |
| 11 | + |
| 12 | +Keyless connections provide enhanced security through granular permissions and identity-based authentication. We don't recommend hard-coded API keys, but if you prefer them, see [Connect to Azure AI Search using keys](../../search-security-api-keys.md). |
| 13 | + |
| 14 | +<!-- This quickstart is a prerequisite for other quickstarts that use Microsoft Entra ID with role assignments. --> |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | ++ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 19 | + |
| 20 | ++ An [Azure AI Search service](../../search-create-service-portal.md) in any region or tier. |
| 21 | + |
| 22 | ++ The [Azure CLI](/cli/azure/install-azure-cli) for keyless authentication with Microsoft Entra ID. |
| 23 | + |
| 24 | ++ [Visual Studio Code](https://code.visualstudio.com/) with the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) and [Jupyter package](https://jupyter.org/install). |
| 25 | + |
| 26 | +[!INCLUDE [Setup](./search-get-started-rbac-setup.md)] |
| 27 | + |
| 28 | +## Sign in to Azure |
| 29 | + |
| 30 | +Before you connect to your Azure AI Search service, use the Azure CLI to sign in to the subscription that contains your service. This step establishes your Microsoft Entra identity, which `DefaultAzureCredential` uses to authenticate requests in the next section. |
| 31 | + |
| 32 | +To sign in: |
| 33 | + |
| 34 | +1. On your local system, open a command-line tool. |
| 35 | + |
| 36 | +1. Sign in to Azure. If you have multiple subscriptions, select the one whose ID you obtained in [Get service information](#get-service-information). |
| 37 | + |
| 38 | + ```azurecli |
| 39 | + az login |
| 40 | + ``` |
| 41 | + |
| 42 | +## Connect to Azure AI Search |
| 43 | + |
| 44 | +> [!NOTE] |
| 45 | +> This section illustrates the basic Python pattern for keyless connections. For comprehensive guidance, see a specific quickstart or tutorial, such as [Quickstart: Run agentic retrieval in Azure AI Search](../../search-get-started-agentic-retrieval.md). |
| 46 | +
|
| 47 | +You can use Python notebooks in Visual Studio Code to send requests to your Azure AI Search service. For request authentication, use the `DefaultAzureCredential` class from the Azure Identity library. |
| 48 | + |
| 49 | +To connect using Python: |
| 50 | + |
| 51 | +1. On your local system, open Visual Studio Code. |
| 52 | + |
| 53 | +1. Create a `.ipynb` file. |
| 54 | + |
| 55 | +1. Create a code cell to install the `azure-identity` and `azure-search-documents` libraries. |
| 56 | + |
| 57 | + ```python |
| 58 | + pip install azure-identity azure-search-documents |
| 59 | + ``` |
| 60 | + |
| 61 | +1. Create another code cell to authenticate and connect to your search service. |
| 62 | + |
| 63 | + ```python |
| 64 | + from azure.identity import DefaultAzureCredential |
| 65 | + from azure.search.documents.indexes import SearchIndexClient |
| 66 | + |
| 67 | + service_endpoint = "PUT-YOUR-SEARCH-SERVICE-ENDPOINT-HERE" |
| 68 | + credential = DefaultAzureCredential() |
| 69 | + client = SearchIndexClient(endpoint=service_endpoint, credential=credential) |
| 70 | + |
| 71 | + # List existing indexes |
| 72 | + indexes = client.list_indexes() |
| 73 | + |
| 74 | + for index in indexes: |
| 75 | + index_dict = index.as_dict() |
| 76 | + print(json.dumps(index_dict, indent=2)) |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Set `service_endpoint` to the value you obtained in [Get service information](#get-service-information). |
| 80 | + |
| 81 | +1. Select **Run All** to run both code cells. |
| 82 | + |
| 83 | + The output should list the existing indexes (if any) on your search service, indicating a successful connection. |
| 84 | + |
| 85 | +### Troubleshoot 401 errors |
| 86 | + |
| 87 | +If you encounter a 401 error, follow these troubleshooting steps: |
| 88 | + |
| 89 | ++ Revisit [Configure role-based access](#configure-role-based-access). Your search service must have **Role-based access control** or **Both** enabled. Policies at the subscription or resource group level might also override your role assignments. |
| 90 | + |
| 91 | ++ Revisit [Sign in to Azure](#sign-in-to-azure). You must sign in to the subscription that contains your search service. |
| 92 | + |
| 93 | ++ Make sure your endpoint variable has surrounding quotes. |
| 94 | + |
| 95 | ++ If all else fails, restart your device to remove cached tokens and then repeat the steps in this quickstart, starting with [Sign in to Azure](#sign-in-to-azure). |
0 commit comments