Connect to Elasticsearch with an SDK
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
Generally available
You can use an official Elasticsearch client library to connect your application to Elasticsearch from code. This page shows how to install and initialize the client for each supported language.
To get started without installing a client, work with Elasticsearch data in the Kibana Dev Tools Console, or send requests with curl. For curl examples, refer to the Elasticsearch API documentation.
You need the following:
- A running Elasticsearch deployment or project. If you don't have one yet, refer to Get Elasticsearch running for search.
- The Elasticsearch endpoint URL, which the client uses to reach Elasticsearch, and an API key, which the client uses to authenticate. If you don't have them yet, refer to Get your Elasticsearch endpoint and API key.
The following steps show you how to set your endpoint and API key, install a client for your language, and verify the connection.
-
Set environment variables
Set your Elasticsearch endpoint and API key as environment variables:
Shell export ES_URL="YOUR_PROJECT_URL" export ES_API_KEY="YOUR_API_KEY"- For example,
https://my-project.es.us-east-1.aws.elastic.cloud:443.
Powershell $Env:ES_URL = "YOUR_PROJECT_URL" $Env:ES_API_KEY = "YOUR_API_KEY"- For example,
https://my-project.es.us-east-1.aws.elastic.cloud:443.
- For example,
-
Install and initialize a client
Install the client for your language. For a list of available clients, refer to Elasticsearch clients.
Shell pip install elasticsearchFor supported Python versions and other requirements, refer to the Python client documentation.
Shell npm install @elastic/elasticsearch npm install --save-dev typescript tsx npm pkg set type=moduleFor supported Node.js versions and other requirements, refer to the JavaScript client documentation.
Shell composer require elasticsearch/elasticsearchFor supported PHP versions and other requirements, refer to the PHP client documentation.
Shell bundle add elasticsearchFor supported Ruby versions and other requirements, refer to the Ruby client installation documentation.
Shell dotnet add package Elastic.Clients.ElasticsearchFor supported .NET versions and other requirements, refer to the .NET client documentation.
In the generated
build.gradlefile, add the client dependency to thedependenciesblock and setmainClassin theapplicationblock:Groovy dependencies { implementation "co.elastic.clients:elasticsearch-java:VERSION" } application { mainClass = "quickstart.App" }Replace
VERSIONwith the version from the latest Java client release. For supported Java versions and other requirements, refer to the Java client documentation.Shell go get github.com/elastic/go-elasticsearch/v9For supported Go versions and other requirements, refer to the Go client installation documentation.
After installation, initialize the client:
Python import os from elasticsearch import Elasticsearch es = Elasticsearch( os.environ["ES_URL"], api_key=os.environ["ES_API_KEY"], )TypeScript import { Client } from "@elastic/elasticsearch"; const es = new Client({ node: process.env.ES_URL, auth: { apiKey: process.env.ES_API_KEY! }, });Php <?php require __DIR__ . "/vendor/autoload.php"; use Elastic\Elasticsearch\ClientBuilder; $es = ClientBuilder::create() ->setHosts([getenv("ES_URL")]) ->setApiKey(getenv("ES_API_KEY")) ->build();Ruby require "elasticsearch" es = Elasticsearch::Client.new( url: ENV.fetch("ES_URL"), api_key: ENV.fetch("ES_API_KEY") )C# using Elastic.Clients.Elasticsearch; using Elastic.Transport; var url = Environment.GetEnvironmentVariable("ES_URL") ?? throw new InvalidOperationException("ES_URL is not set."); var apiKey = Environment.GetEnvironmentVariable("ES_API_KEY") ?? throw new InvalidOperationException("ES_API_KEY is not set."); var settings = new ElasticsearchClientSettings(new Uri(url)) .Authentication(new ApiKey(apiKey)); var es = new ElasticsearchClient(settings);Java package quickstart; import co.elastic.clients.elasticsearch.ElasticsearchClient; public class App { public static void main(String[] args) throws Exception { String serverUrl = System.getenv("ES_URL"); String apiKey = System.getenv("ES_API_KEY"); ElasticsearchClient es = ElasticsearchClient.of(b -> b .host(serverUrl) .apiKey(apiKey) ); } }Go package main import ( "log" "os" "github.com/elastic/go-elasticsearch/v9" ) func main() { es, err := elasticsearch.New( elasticsearch.WithAddresses(os.Getenv("ES_URL")), elasticsearch.WithAPIKey(os.Getenv("ES_API_KEY")), ) if err != nil { log.Fatal(err) } } -
Verify your connection
Call the info API to confirm that your endpoint URL and API key work. A successful response includes the cluster name and version. The exact output depends on your client and your deployment.
Python print(es.info())Example response
Python {'name': 'instance-0000000000', 'cluster_name': 'my-deployment', 'cluster_uuid': 'QIpRB53HS1KSVYWtjP_rqg', 'version': {'number': '9.2.0', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '25d88452371273dd27356c98598287b669a03eae', 'build_date': '2025-10-21T10:06:21.288851013Z', 'build_snapshot': False, 'lucene_version': '10.3.1', 'minimum_wire_compatibility_version': '8.19.0', 'minimum_index_compatibility_version': '8.0.0'}, 'tagline': 'You Know, for Search'}TypeScript console.log(await es.info());Example response
TypeScript { name: 'instance-0000000000', cluster_name: 'my-deployment', cluster_uuid: 'QIpRB53HS1KSVYWtjP_rqg', version: { number: '9.2.0', build_flavor: 'default', build_type: 'docker', build_hash: '25d88452371273dd27356c98598287b669a03eae', build_date: '2025-10-21T10:06:21.288851013Z', build_snapshot: false, lucene_version: '10.3.1', minimum_wire_compatibility_version: '8.19.0', minimum_index_compatibility_version: '8.0.0' }, tagline: 'You Know, for Search' }Php echo $es->info()->asString();Example response
JSON { "name" : "instance-0000000000", "cluster_name" : "my-deployment", "cluster_uuid" : "QIpRB53HS1KSVYWtjP_rqg", "version" : { "number" : "9.2.0", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "25d88452371273dd27356c98598287b669a03eae", "build_date" : "2025-10-21T10:06:21.288851013Z", "build_snapshot" : false, "lucene_version" : "10.3.1", "minimum_wire_compatibility_version" : "8.19.0", "minimum_index_compatibility_version" : "8.0.0" }, "tagline" : "You Know, for Search" }Ruby puts es.infoExample response
Ruby {"name" => "instance-0000000000", "cluster_name" => "my-deployment", "cluster_uuid" => "QIpRB53HS1KSVYWtjP_rqg", "version" => {"number" => "9.2.0", "build_flavor" => "default", "build_type" => "docker", "build_hash" => "25d88452371273dd27356c98598287b669a03eae", "build_date" => "2025-10-21T10:06:21.288851013Z", "build_snapshot" => false, "lucene_version" => "10.3.1", "minimum_wire_compatibility_version" => "8.19.0", "minimum_index_compatibility_version" => "8.0.0"}, "tagline" => "You Know, for Search"}C# var info = await es.InfoAsync(); Console.WriteLine($"{info.ClusterName} {info.Version.Number}");Example response
Text my-deployment 9.2.0Java System.out.println(es.info()); es._transport().close();Example response
Text InfoResponse: {"cluster_name":"my-deployment","cluster_uuid":"QIpRB53HS1KSVYWtjP_rqg","name":"instance-0000000000","tagline":"You Know, for Search","version":{"build_date":"2025-10-21T10:06:21.288851013Z","build_flavor":"default","build_hash":"25d88452371273dd27356c98598287b669a03eae","build_snapshot":false,"build_type":"docker","lucene_version":"10.3.1","minimum_index_compatibility_version":"8.0.0","minimum_wire_compatibility_version":"8.19.0","number":"9.2.0"}}Go res, err := es.Info() if err != nil { log.Fatal(err) } defer res.Body.Close() log.Println(res)Example response
Text [200 OK] { "name" : "instance-0000000000", "cluster_name" : "my-deployment", "cluster_uuid" : "QIpRB53HS1KSVYWtjP_rqg", "version" : { "number" : "9.2.0", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "25d88452371273dd27356c98598287b669a03eae", "build_date" : "2025-10-21T10:06:21.288851013Z", "build_snapshot" : false, "lucene_version" : "10.3.1", "minimum_wire_compatibility_version" : "8.19.0", "minimum_index_compatibility_version" : "8.0.0" }, "tagline" : "You Know, for Search" }
Elasticsearch is up and running, and your application is connected. Next, learn how to ingest your own data in Ingest for search use cases.