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, it should be wrapped in a JSON formatted string which is stored in the environment variable then loaded and parsed.

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, where the new lines are replaced with \n characters.

export VIZZLY_PRIVATE_KEY='{"value":"-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEID8TG2j/eOeu9z/r6GQQe5H52VH6x68bIAwi+hdwxurcoAoGCCqGSM49\nAwEHoUQDQgAE2hcCqjWJuNWXVIqrZH2Twh8sq1fsz8PAprYtGblWjQdAwi1J3dwj\n3V5GnV8+CtxbGJVtj3BfoSD6EOXBfoK5Bg==\n-----END EC PRIVATE KEY-----"}'

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

function loadVizzlyPrivateKey() {
  return JSON.parse(process.env['VIZZLY_PRIVATE_KEY']).value;
}