Examples
banshee/examples-collection/examples/api/
elasticsearch-key
Example: base64.encode for an API key header; the health of an Elasticsearch cluster.
by banshee1 installation1 version
Published on October 27, 2026. First version in this collection.
// An API key of Elasticsearch: the header is "ApiKey " followed by the id and the key joined by a colon,
// written in base64 by base64.encode. The collector reads the health of the cluster.
need('protocol:http');
$base = variable('example:elastic:url', 'text', array(
'label' => 'Address of the cluster',
'help' => 'For example https://search.example:9200.',
));
$id = variable('example:elastic:key-id', 'text', array(
'label' => 'Id of the API key',
'help' => 'The id Elasticsearch gave with the key.',
));
$key = variable('example:elastic:key', 'text', array(
'label' => 'API key',
'help' => 'The key itself, of a role that can read the health of the cluster.',
'secret' => true,
));
output('health', 'text', array('label' => 'Health of the cluster'));
watch.state('unhealthy', 'Cluster unhealthy', 'health', array('warning' => array('yellow'), 'critical' => array('red')));
// Made from the secret key, the encoded text is a secret too: it only goes into the request.
$authorization = 'ApiKey ' . base64.encode($id . ':' . $key);
$answer = http.get($base . '/_cluster/health', headers=array('Authorization' => $authorization));
if ($answer['status'] != 200) {
fail('misconfigured', 'The cluster answers ' . $answer['status']);
}
// {"cluster_name": "logs", "status": "green", ...}
$cluster = json.read($answer['body']);
value('health', $cluster['status']);