How to access MongoDB credentials on


Access credentials for MongoDB from within a application using Node.js.


  • an active project
  • properly configured .platform/services.yaml for the given service
  • SSH key configured on account if developing locally
  • MongoDB installed if developing locally
  • properly configured MongoDB relationship like so, in
    database: "dbmongo:mongodb"

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 directly

Steps (Config Reader)

1. Install the library

Install the configuration library. See the documentation for minimum requirements.

$ npm install platformsh-config --save    

2. Create a Config object

Creating a Config object provides access to the environment.

const config = require("platformsh-config").config();   

3. Read the credentials

// Get the credentials to connect to the MongoDB service.    
const credentials = config.credentials('database');    

Steps (Manual)

1. Load and decode the environment variable

relationships = JSON.parse(new Buffer(process.env['PLATFORM_RELATIONSHIPS'], 'base64').toString());    

2. Read the credentials

credentials = relationships['database'];   


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

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

  "username": "main",
  "scheme": "mongodb",
  "service": "dbmongo",
  "ip": "",
  "hostname": "",
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "database.internal",
  "rel": "mongodb",
  "path": "main",
  "query": {
    "is_master": true
  "password": "main",
  "type": "mongodb:3.6",
  "port": 27017


Using either the language-specific Configuration library or direct access methods for environment variables, an application can get MongoDB credentials in Node.js. supports configuration libraries for multiple languages. The Node.js configuration library can be useful for connecting to MongoDB without creating the full DSN string using formattedCredentials:

// Define formatted credentials
var client = await MongoClient.connect(config.formattedCredentials('database', 'mongodb'));

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