Skip to main content

Connect to OpenSearch® cluster with Python

You can interact with your cluster with the help of the Python OpenSearch® client. It provides a convenient syntax to send commands to your OpenSearch cluster. Follow its README file for installation instructions.

To connect with your cluster, you need the Service URI of your OpenSearch cluster. You can find the connection details in the section Overview on Aiven web console. Alternatively, you can retrieve it via the avn service get command with the Aiven CLI. Notice that service_uri contains credentials; therefore, should be treated with care.

The Service URI has information in the following format:


For security reasons is recommended to use environment variables to save your credential information. You can use the dotenv Python library to manage your environment variables. Follow its README file for installation instructions.

After is installed, you need to create .env file in the root directory of your project with the SERVICE_URI on it:


And import it in your Python script.

import os
from dotenv import load_dotenv

Now, you can use it as a string variable saved in the SERVICE_URI.

You can import OpenSearch and create an instance of the class to connect with your cluster. In this example, we will be giving the full path and enabling the use_ssl to secure our connection.

from opensearchpy import OpenSearch
opensearch = OpenSearch(SERVICE_URI, use_ssl=True)

There are other ways that you can create your OpenSearch instance configuring parameters such as verify_certs, ssl_assert_hostname and others authentication details that can be configured.

Check documentation for more details.

Those are the steps to create a Python OpenSearch client class instance that can be used to perform request operations and send responses to your cluster.