Elastic Transport for Python

9.2.1 · active · verified Sun Mar 29

Elastic Transport for Python provides transport classes and utilities shared among various Python Elastic client libraries. It serves as a low-level HTTP client, powering the official Elasticsearch Python client and other Elastic projects. The library is currently at version 9.2.1 and follows a release cadence aligned with the major and minor versions of the Elastic Stack, with patch numbers incremented for bug fixes within a minor release.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Transport` class, either connecting to a local Elasticsearch instance or an Elastic Cloud deployment using environment variables. It then performs a basic GET request to the cluster root and prints connection details. The `transport.close()` method should be called to close HTTP connections when the transport is no longer needed.

import os
from elastic_transport import Transport, NodeConfig

# Configure nodes - replace with your Elasticsearch host(s)
# For local testing, default to 'http://localhost:9200'
# For cloud, use CLOUD_ID and API_KEY environment variables
cloud_id = os.environ.get('ELASTIC_CLOUD_ID', '')
api_key = os.environ.get('ELASTIC_API_KEY', '')

node_configs = [
    NodeConfig("http://localhost:9200")
]

if cloud_id and api_key:
    # Use Cloud ID and API Key for Elastic Cloud
    transport = Transport(
        node_configs=[], # node_configs are derived from cloud_id
        cloud_id=cloud_id,
        api_key=(api_key,)
    )
else:
    transport = Transport(node_configs=node_configs)

try:
    # Perform a GET request to the root ('/') which often returns cluster info
    # The response is a tuple of (ApiResponseMeta, deserialized_body)
    meta, body = transport.perform_request("GET", "/")
    print("Successfully connected to Elasticsearch!")
    print(f"Cluster Name: {body.get('cluster_name')}")
    print(f"Status Code: {meta.status}")
except Exception as e:
    print(f"Failed to connect or perform request: {e}")
    print("Ensure Elasticsearch is running and accessible at the specified host(s)")
    print("Or check your ELASTIC_CLOUD_ID and ELASTIC_API_KEY environment variables.")

transport.close()

view raw JSON →