Get started with the Elastic CLI

The Elastic CLI lets you interact with Elastic services from the command line. Use it to explore APIs interactively, automate operations in scripts and CI/CD pipelines, or integrate Elastic operations into agent workflows.

By the end of this tutorial, you'll have installed and configured the CLI, connected it to an existing Elasticsearch deployment or Elastic Cloud Serverless project, and performed basic Elasticsearch operations.

To install the CLI, you need to install Node.js 22.12.0 or later. Node.js includes npm. Run node --version to check your installed version. The Elastic CLI is tested on Linux, macOS, and Windows, and the installation command is the same on every platform.

To follow the steps in this tutorial, you also need:

  • The Elasticsearch endpoint for an existing cluster, regardless of deployment type, or an Elastic Cloud Serverless project. If you're not sure which endpoint to use, refer to Find your Elasticsearch endpoint.
  • An API key with permission to view cluster information and list indices for the example operations in this tutorial. For your own use cases, grant only the permissions required by the APIs you plan to call.
Note

Elastic provides different API key types for different APIs. The Elastic CLI supports:

  • Elasticsearch API keys for accessing Elasticsearch and Kibana APIs in self-managed or Elastic Cloud on Kubernetes clusters and Elastic Cloud Enterprise or Elastic Cloud Hosted deployments.
  • Serverless project API keys for accessing Elasticsearch and Kibana APIs within a specific Elastic Cloud Serverless project.
  • Elastic Cloud API keys for managing organizations, Elastic Cloud Hosted deployments, and Elastic Cloud Serverless projects. In Serverless, a Cloud API key with Cloud, Elasticsearch, and Kibana API access and an appropriate role for each relevant project can also call project-level Elasticsearch and Kibana APIs. Elastic Cloud API keys can't authenticate against Elasticsearch or Kibana endpoints on Elastic Cloud Hosted.

Refer to Elastic API keys to compare all available key types.

  1. Install the CLI globally:

    npm install -g @elastic/cli
    		
  2. Verify that the elastic command is available:

    elastic --version
    		

    The command prints the installed version. If it doesn't, follow the installation guide before continuing.

The following steps create a context named quickstart with the URL and credentials for your Elasticsearch connection.

If you use Bash or zsh, keep the API key out of your shell history by capturing it in a temporary variable:

read -rs ELASTIC_API_KEY
		

Paste your API key at the hidden prompt, press Enter, and create the context, replacing <elasticsearch-url> with your endpoint:

elastic config context add quickstart \
  --es-url "<elasticsearch-url>" \
  --es-api-key "$ELASTIC_API_KEY"
unset ELASTIC_API_KEY
		

The CLI stores the API key in your operating system's credential store when one is available. Otherwise, it stores the key in the configuration file and warns you.

API keys are sensitive credentials. The hidden prompt keeps the key out of your shell history, but its value might appear briefly in process listings when passed to --es-api-key. For other ways to provide and store secrets, refer to external credentials.

Confirm that the context exists:

elastic config context list
		

The output includes quickstart. If this is your first context, the CLI also makes it the current context.

Check connectivity and authentication:

elastic --use-context quickstart status
		

A successful check displays a check mark next to the Elasticsearch endpoint, followed by its health status and node count. If the command reports an authentication or network error, verify the endpoint and API key in the configuration guide.

Request information about Elasticsearch and return the response as JSON:

elastic --use-context quickstart --json es info
		

The response includes identifying information about the connected deployment or project, including its name and version.

List the indices that your API key can access:

elastic --use-context quickstart es cat indices
		

The output displays index health, status, document counts, and storage sizes in a table. You've now installed the CLI, configured a reusable connection, and run read-only Elasticsearch API operations.