Import your own code

Connect to Services

Note: If your application does not require access to services and you left .platform/services.yaml blank, feel free to proceed to the next step.

At this point you have configured your application's services as well as its build and deploy process in your .platform/services.yaml and .platform.app.yaml files. As an example, in your .platform.app.yaml file you may have defined a relationship called database:

relationships:
    database: "mysqldb:mysql"

which was configured in .platform/services.yaml with

mysqldb:
    type: mysql:10.2
    disk: 1024

In order to connect to this service and use it in your application, Platform.sh exposes its credentials in the application container within a base64-encoded JSON PLATFORM_RELATIONSHIPS environment variable.

To access this variable you can install a Platform.sh configuration reader library

PHP
Python
Node.js
Java (Maven)
Java (Gradle)
Go
composer install platformsh/config-reader
pip install platformshconfig
npm install platformsh-config --save
<dependency>
    <groupId>sh.platform</groupId>
    <artifactId>config</artifactId>
    <version>2.2.0</version>
</dependency>
compile group: 'sh.platform', name: 'config', version: 2.2.0'
go mod edit -require=github.com/platformsh/config-reader-go/v2

and access the credentials of database

PHP
Python
Node.js
Java
Go
use Platformsh\ConfigReader\Config;

$config = new Config();
$credentials = $config->credentials('database');
from platformshconfig import Config

config = Config()
credentials = config.credentials('database')
const config = require("platformsh-config").config();
const credentials = config.credentials('database');
import Config;

Config config = new Config();
Credential cred = config.getCredential('database')
import psh "github.com/platformsh/config-reader-go/v2"

config, err := psh.NewRuntimeConfig()
// Handle err

credentials, err := config.Credentials("database")
// Handle err

or read and decode the environment variable directly.

PHP
Python
Node.js
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')), TRUE);
$credentials = $relationships['database'];
import os, json, base64

relationships = json.loads(base64.b64decode(os.environ["PLATFORM_RELATIONSHIPS"]))
credentials = relationships['database']
relationships = JSON.parse(new Buffer(process.env['PLATFORM_RELATIONSHIPS'], 'base64').toString());
credentials = relationships['database'];

In either case, credentials can now be used to connect to database:

{
  "username": "user",
  "scheme": "mysql",
  "service": "mysql",
  "fragment": null,
  "ip": "169.254.197.253",
  "hostname": "czwb2d7zzunu67lh77infwkm6i.mysql.service._.eu-3.platformsh.site",
  "public": false,
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "mysql.internal",
  "rel": "mysql",
  "query": {
    "is_master": true
  },
  "path": "main",
  "password": "",
  "type": "mysql:10.2",
  "port": 3306
}

You can find out more information about Platform.sh Config Reader libraries on GitHub:

You can also find examples of how to connect to each of Platform.sh managed services in multiple languages in the Services Documentation.

Project configured, services connected - time to commit the changes and push your repository onto your project.