Load your private key from an environment variable

How to load your private key from an environment variable

Your private key generated using the Vizzly CLI command, creates your keys in a .PEM format. These can be loaded onto your servers either as a file that can be read directly by your application or loaded as an environment variable that your application has permission to access.

To load your certificate as an environment variable, the private key contents should be base64 encoded and added as an environment variable. Then on your servers it should be decoded before use.

For example, if your private key was

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEID8TG2j/eOeu9z/r6GQQe5H52VH6x68bIAwi+hdwxurcoAoGCCqGSM49
AwEHoUQDQgAE2hcCqjWJuNWXVIqrZH2Twh8sq1fsz8PAprYtGblWjQdAwi1J3dwj
3V5GnV8+CtxbGJVtj3BfoSD6EOXBfoK5Bg==
-----END EC PRIVATE KEY-----

Then your environment variable should be exported in the following way;

export VIZZLY_PRIVATE_KEY='LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tDQpNSGNDQVFFRUlEOFRHMmovZU9ldTl6L3I2R1FRZTVINTJWSDZ4NjhiSUF3aStoZHd4dXJjb0FvR0NDcUdTTTQ5DQpBd0VIb1VRRFFnQUUyaGNDcWpXSnVOV1hWSXFyWkgyVHdoOHNxMWZzejhQQXByWXRHYmxXalFkQXdpMUozZHdqDQozVjVHblY4K0N0eGJHSlZ0ajNCZm9TRDZFT1hCZm9LNUJnPT0NCi0tLS0tRU5EIEVDIFBSSVZBVEUgS0VZLS0tLS0='

which you would then load into your application by using the code snippet

function loadVizzlyPrivateKey() {
  const base64String = process.env['VIZZLY_PRIVATE_KEY'];
 
  return Buffer.from(base64String, 'base64').toString()
}

Base64 helper

Vizzly offers a CLI command to build the base64 encoded string from your vizzly-private.pem file.

Example usage

vizzly format-private-pem-for-env --private-key "./vizzly-private.pem"