0
0
Elasticsearchquery~5 mins

API key management in Elasticsearch

Choose your learning style9 modes available
Introduction

API key management helps you control who can access your Elasticsearch data safely. It lets you create, use, and delete keys that act like secret passwords for apps.

When you want to let an app read or write data without sharing your main password.
When you need to give limited access to a service for a short time.
When you want to track which app or user is using your Elasticsearch.
When you want to easily revoke access without changing your main credentials.
Syntax
Elasticsearch
POST /_security/api_key
{
  "name": "my-api-key",
  "role_descriptors": {
    "my-role": {
      "cluster": ["all"],
      "index": [
        {
          "names": ["my-index"],
          "privileges": ["read"]
        }
      ]
    }
  }
}

The name is a friendly label for your API key.

role_descriptors define what the key can do, like read or write on certain indexes.

Examples
This creates an API key that can only read indexes starting with 'logs-'.
Elasticsearch
POST /_security/api_key
{
  "name": "read-only-key",
  "role_descriptors": {
    "read_role": {
      "cluster": ["monitor"],
      "index": [
        {
          "names": ["logs-*"],
          "privileges": ["read"]
        }
      ]
    }
  }
}
This invalidates an API key by its ID to stop its access immediately.
Elasticsearch
DELETE /_security/api_key
{
  "ids": ["api_key_id_here"]
}
This retrieves details about an API key by its name.
Elasticsearch
GET /_security/api_key?name=my-api-key
Sample Program

This request creates an API key named 'example-key' that can monitor the cluster and read/write to 'example-index'.

Elasticsearch
POST /_security/api_key
{
  "name": "example-key",
  "role_descriptors": {
    "example-role": {
      "cluster": ["monitor"],
      "index": [
        {
          "names": ["example-index"],
          "privileges": ["read", "write"]
        }
      ]
    }
  }
}
OutputSuccess
Important Notes

Always keep your API keys secret like passwords.

You can create keys with very specific permissions to keep your data safe.

Deleting an API key immediately stops its access.

Summary

API key management lets you create and control secret keys for apps to access Elasticsearch.

You define what each key can do using roles and privileges.

Keys can be created, viewed, and deleted using Elasticsearch security API calls.