How to access Elasticsearch credentials on


Access credentials for Elasticsearch from within a application using Python.


  • an active project
  • properly configured .platform/services.yaml for the given service
  • SSH key configured on account if developing locally
  • Elasticsearch installed if developing locally
  • properly configured Elasticsearch relationship like so, in
    essearch: "searchelastic:elasticsearch"

If developing locally, remember to first open a tunnel to the project environment using the Platform CLI.

$ platform tunnel:open && export PLATFORM_RELATIONSHIPS="$(platform tunnel:info --encode)"

This will open an SSH tunnel to your current environment and expose a local environment variable that mimics the relationships array on Check the documentation and How to develop locally on with a tethered connection for more information.

Problems service credentials are made available to applications as the PLATFORM_RELATIONSHIPS environment variable, which is a base64-encoded JSON string that has to be decoded before it can be used.

There are two primary options for accessing service credentials on that can be used within an application:

  1. Using the Config Reader library
  2. Accessing environment variables manually

Steps (Config Reader)

1. Install the library

Install the Configuration Reader library. See the documentation for minimum requirements.

$ pip install platformshconfig

2. Create a Config object

Creating a Config object provides access to the environment.

from platformshconfig import Config

config = Config()

3. Read the credentials

# Get the credentials to connect to the Elasticsearch service.
credentials = config.credentials('essearch')

If the relationship is named something else, modify the credentials() call accordingly.

Steps (Manual)

1. Load and decode the environment variable

import os, json, base64

relationships = json.loads(base64.b64decode(os.environ["PLATFORM_RELATIONSHIPS"]))

2. Get the credentials

credentials = relationships['essearch']

If the relationship is named something else, modify that line accordingly.


In most cases, you will need only the scheme, host, and port properties to connect to Elasticsearch. Pass those to your Elasticsearch library’s setup routine in your application. Most of the time the other values may be ignored.

In either case, credentials is now a dictionary matching the relationship JSON object.

  "username": null,
  "scheme": "http",
  "service": "searchelastic",
  "fragment": null,
  "ip": "",
  "hostname": "",
  "public": false,
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "essearch.internal",
  "rel": "elasticsearch",
  "query": [
  "path": null,
  "password": null,
  "type": "elasticsearch:5.4",
  "port": 9200


Using either the language-specific Configuration library or direct access methods for environment variables, an application can get Elasticsearch credentials in Python. supports configuration libraries for multiple languages. The Python configuration library can be useful for inspecting the project environment:

# Checks whether the code is running in a build environment
# Checks whether the code is running in a runtime environment

and for reading environment variables as attributes of config:

# Available in Build and at Runtime
# Available at Runtime only

The APIs for each language are written to be as consistent as possible, but seek out each library’s documentation for specific differences.