Create a cross-cluster API key Generally available

POST /_security/cross_cluster/api_key

Create an API key of the cross_cluster type for the API key based remote cluster access. A cross_cluster API key cannot be used to authenticate through the REST interface.

IMPORTANT: To authenticate this request you must use a credential that is not an API key. Even if you use an API key that has the required privilege, the API returns an error.

Cross-cluster API keys are created by the Elasticsearch API key service, which is automatically enabled.

NOTE: Unlike REST API keys, a cross-cluster API key does not capture permissions of the authenticated user. The API key’s effective permission is exactly as specified with the access property.

A successful request returns a JSON structure that contains the API key, its unique ID, and its name. If applicable, it also returns expiration information for the API key in milliseconds.

By default, API keys never expire. You can specify expiration information when you create the API keys.

Cross-cluster API keys can only be updated with the update cross-cluster API key API. Attempting to update them with the update REST API key API or the bulk update REST API keys API will result in an error.

highlight#highlightFromAnchor" href="#topic-required-authorization"> Required authorization

  • Cluster privileges: manage_security
External documentation
application/json

Body Required

Responses

  • 200 application/json
    details#setActive"> Hide response attributes Show response attributes object
    • api_key string Required

      Generated API key.

    • expiration number

      Time unit for milliseconds

    • id string Required
    • name string Required
    • encoded string Required

      API key credentials which is the base64-encoding of the UTF-8 representation of id and api_key joined by a colon (:).

POST /_security/cross_cluster/api_key
{
  "name": "my-cross-cluster-api-key",
  "expiration": "1d",   
  "access": {
    "search": [  
      {
        "names": ["logs*"]
      }
    ],
    "replication": [  
      {
        "names": ["archive*"]
      }
    ]
  },
  "metadata": {
    "description": "phase one",
    "environment": {
      "level": 1,
      "trusted": true,
      "tags": ["dev", "staging"]
    }
  }
}
resp = client.security.create_cross_cluster_api_key(
    name="my-cross-cluster-api-key",
    expiration="1d",
    access={
        "search": [
            {
                "names": [
                    "logs*"
                ]
            }
        ],
        "replication": [
            {
                "names": [
                    "archive*"
                ]
            }
        ]
    },
    metadata={
        "description": "phase one",
        "environment": {
            "level": 1,
            "trusted": True,
            "tags": [
                "dev",
                "staging"
            ]
        }
    },
)
const response = await client.security.createCrossClusterApiKey({
  name: "my-cross-cluster-api-key",
  expiration: "1d",
  access: {
    search: [
      {
        names: ["logs*"],
      },
    ],
    replication: [
      {
        names: ["archive*"],
      },
    ],
  },
  metadata: {
    description: "phase one",
    environment: {
      level: 1,
      trusted: true,
      tags: ["dev", "staging"],
    },
  },
});
response = client.security.create_cross_cluster_api_key(
  body: {
    "name": "my-cross-cluster-api-key",
    "expiration": "1d",
    "access": {
      "search": [
        {
          "names": [
            "logs*"
          ]
        }
      ],
      "replication": [
        {
          "names": [
            "archive*"
          ]
        }
      ]
    },
    "metadata": {
      "description": "phase one",
      "environment": {
        "level": 1,
        "trusted": true,
        "tags": [
          "dev",
          "staging"
        ]
      }
    }
  }
)
$resp = $client->security()->createCrossClusterApiKey([
    "body" => [
        "name" => "my-cross-cluster-api-key",
        "expiration" => "1d",
        "access" => [
            "search" => array(
                [
                    "names" => array(
                        "logs*",
                    ),
                ],
            ),
            "replication" => array(
                [
                    "names" => array(
                        "archive*",
                    ),
                ],
            ),
        ],
        "metadata" => [
            "description" => "phase one",
            "environment" => [
                "level" => 1,
                "trusted" => true,
                "tags" => array(
                    "dev",
                    "staging",
                ),
            ],
        ],
    ],
]);
curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d '{"name":"my-cross-cluster-api-key","expiration":"1d","access":{"search":[{"names":["logs*"]}],"replication":[{"names":["archive*"]}]},"metadata":{"description":"phase one","environment":{"level":1,"trusted":true,"tags":["dev","staging"]}}}' "$ELASTICSEARCH_URL/_security/cross_cluster/api_key"
client.security().createCrossClusterApiKey(c -> c
    .access(a -> a
        .replication(r -> r
            .names("archive*")
        )
        .search(s -> s
            .names("logs*")
        )
    )
    .expiration(e -> e
        .time("1d")
    )
    .metadata(Map.of("environment", JsonData.fromJson("{\"level\":1,\"trusted\":true,\"tags\":[\"dev\",\"staging\"]}"),"description", JsonData.fromJson("\"phase one\"")))
    .name("my-cross-cluster-api-key")
);
Request example
Run `POST /_security/cross_cluster/api_key` to create a cross-cluster API key.
{
  "name": "my-cross-cluster-api-key",
  "expiration": "1d",   
  "access": {
    "search": [  
      {
        "names": ["logs*"]
      }
    ],
    "replication": [  
      {
        "names": ["archive*"]
      }
    ]
  },
  "metadata": {
    "description": "phase one",
    "environment": {
      "level": 1,
      "trusted": true,
      "tags": ["dev", "staging"]
    }
  }
}
Response examples (200)
A successful response from `POST /_security/service/elastic/fleet-server/credential/token`.
{
  "created": true,
  "token": {
    "name": "Jk5J1HgBuyBK5TpDrdo4",
    "value": "AAEAAWVsYXN0aWM...vZmxlZXQtc2VydmVyL3Rva2VuMTo3TFdaSDZ"
  }
}