﻿---
title: Connect to Elasticsearch with an SDK
description: Install an official {{es}} language client, connect it with your endpoint and API key, and verify the connection.
url: https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/solutions/search/set-up/connect-with-an-sdk
products:
  - Elastic Documentation
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available
---

# Connect to Elasticsearch with an SDK
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.
<note>
  To get started without installing a client, work with Elasticsearch data in the Kibana [Dev Tools Console](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/explore-analyze/query-filter/tools/console), or send requests with `curl`. For `curl` examples, refer to the [Elasticsearch API documentation](https://www.elastic.co/docs/api/doc/elasticsearch/).
</note>


## Before you begin

You need the following:
- A running Elasticsearch deployment or project. If you don't have one yet, refer to [Get Elasticsearch running for search](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/solutions/search/set-up/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](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/solutions/search/set-up/api-key-and-endpoints).


## Connect a client

The following steps show you how to set your endpoint and API key, install a client for your language, and verify the connection.
<stepper>
  <step title="Connect to {{es}}">
    Set your Elasticsearch endpoint and API key as environment variables:
    <tab-set>
      <tab-item title="macOS and Linux">
        ```bash
        export ES_URL="YOUR_PROJECT_URL" 
        export ES_API_KEY="YOUR_API_KEY"
        ```
      </tab-item>

      <tab-item title="Windows PowerShell">
        ```powershell
        $Env:ES_URL = "YOUR_PROJECT_URL" 
        $Env:ES_API_KEY = "YOUR_API_KEY"
        ```
      </tab-item>
    </tab-set>
  </step>

  <step title="Install and initialize a client">
    Install the client for your language. For a list of available clients, refer to [Elasticsearch clients](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/reference/elasticsearch-clients).
    <tab-set>
      <tab-item title="Python">
        ```bash
        pip install elasticsearch
        ```
        For supported Python versions and other requirements, refer to the [Python client documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-py/tree/main/reference/getting-started#_requirements).
      </tab-item>

      <tab-item title="TypeScript">
        ```bash
        npm install @elastic/elasticsearch
        npm install --save-dev typescript tsx
        npm pkg set type=module
        ```
        For supported Node.js versions and other requirements, refer to the [JavaScript client documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-js/tree/main/reference).
      </tab-item>

      <tab-item title="PHP">
        ```bash
        composer require elasticsearch/elasticsearch
        ```
        For supported PHP versions and other requirements, refer to the [PHP client documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-php/tree/main/reference).
      </tab-item>

      <tab-item title="Ruby">
        ```bash
        bundle add elasticsearch
        ```
        For supported Ruby versions and other requirements, refer to the [Ruby client installation documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-ruby/tree/main/reference/installation).
      </tab-item>

      <tab-item title="C#/.NET">
        ```bash
        dotnet add package Elastic.Clients.Elasticsearch
        ```
        For supported .NET versions and other requirements, refer to the [.NET client documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-net/tree/main/reference).
      </tab-item>

      <tab-item title="Java">
        In the generated `build.gradle` file, add the client dependency to the `dependencies` block and set `mainClass` in the `application` block:
        ```groovy
        dependencies {
            implementation "co.elastic.clients:elasticsearch-java:VERSION"
        }

        application {
            mainClass = "quickstart.App"
        }
        ```
        Replace `VERSION` with the version from the [latest Java client release](https://github.com/elastic/elasticsearch-java/releases). For supported Java versions and other requirements, refer to the [Java client documentation](https://docs-v3-preview.elastic.dev/elastic/elasticsearch-java/tree/main/reference).
      </tab-item>

      <tab-item title="Go">
        ```bash
        go get github.com/elastic/go-elasticsearch/v9
        ```
        For supported Go versions and other requirements, refer to the [Go client installation documentation](https://www.elastic.co/docs/reference/elasticsearch/clients/go/installation).
      </tab-item>
    </tab-set>
    After installation, initialize the client:
    <tab-set>
      <tab-item title="Python">
        ```python
        import os
        from elasticsearch import Elasticsearch

        es = Elasticsearch(
            os.environ["ES_URL"],
            api_key=os.environ["ES_API_KEY"],
        )
        ```
      </tab-item>

      <tab-item title="TypeScript">
        ```typescript
        import { Client } from "@elastic/elasticsearch";

        const es = new Client({
          node: process.env.ES_URL,
          auth: { apiKey: process.env.ES_API_KEY! },
        });
        ```
      </tab-item>

      <tab-item title="PHP">
        ```php
        <?php

        require __DIR__ . "/vendor/autoload.php";

        use Elastic\Elasticsearch\ClientBuilder;

        $es = ClientBuilder::create()
            ->setHosts([getenv("ES_URL")])
            ->setApiKey(getenv("ES_API_KEY"))
            ->build();
        ```
      </tab-item>

      <tab-item title="Ruby">
        ```ruby
        require "elasticsearch"

        es = Elasticsearch::Client.new(
          url: ENV.fetch("ES_URL"),
          api_key: ENV.fetch("ES_API_KEY")
        )
        ```
      </tab-item>

      <tab-item title="C#/.NET">
        ```csharp
        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);
        ```
      </tab-item>

      <tab-item title="Java">
        ```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)
                );
            }
        }
        ```
      </tab-item>

      <tab-item title="Go">
        ```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)
        	}
        }
        ```
      </tab-item>
    </tab-set>
  </step>

  <step title="Verify your connection">
    Call the info API to confirm that your endpoint URL and API key work.
    <tab-set>
      <tab-item title="Python">
        ```python
        print(es.info())
        ```
      </tab-item>

      <tab-item title="TypeScript">
        ```typescript
        console.log(await es.info());
        ```
      </tab-item>

      <tab-item title="PHP">
        ```php
        echo $es->info()->asString();
        ```
      </tab-item>

      <tab-item title="Ruby">
        ```ruby
        puts es.info
        ```
      </tab-item>

      <tab-item title="C#/.NET">
        ```csharp
        var info = await es.InfoAsync();
        Console.WriteLine($"{info.ClusterName} {info.Version.Number}");
        ```
      </tab-item>

      <tab-item title="Java">
        ```java
        System.out.println(es.info());
        es._transport().close();
        ```
      </tab-item>

      <tab-item title="Go">
        ```go
        res, err := es.Info()
        if err != nil {
        	log.Fatal(err)
        }
        defer res.Body.Close()
        log.Println(res)
        ```
      </tab-item>
    </tab-set>

    <dropdown title="Example responses">
      A successful response includes the cluster name and version. The exact output depends on your client and your deployment.
      <tab-set>
        <tab-item title="Python">
          ```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'}
          ```
        </tab-item>

        <tab-item title="TypeScript">
          ```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'
          }
          ```
        </tab-item>

        <tab-item title="PHP">
          ```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"
          }
          ```
        </tab-item>

        <tab-item title="Ruby">
          ```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"}
          ```
        </tab-item>

        <tab-item title="C#/.NET">
          ```text
          my-deployment 9.2.0
          ```
        </tab-item>

        <tab-item title="Java">
          ```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"}}
          ```
        </tab-item>

        <tab-item title="Go">
          ```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"
          }
          ```
        </tab-item>
      </tab-set>
    </dropdown>
  </step>
</stepper>


## Next steps

Elasticsearch is up and running, and your application is connected. Next, learn how to ingest your own data in [Ingest for search use cases](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/8603/solutions/search/ingest-for-search).