Working with private keys

Today I learned

I found that working with private keys, specifically in Firebase and Vercel, can be a hugely frustrating ordeal. I'm not sure yet whether the issue is with Vercel as I'm not as experienced with this platform. So far it's been great for deploying my project, but the configuration around environment variables is something that has been a bit confusing for me, and I would constantly get different errors caused by the failure to read a private key or the formatting of it.

The issue was with reading the environment variable in local vs production code. I was familiar with this and all would have been fine except that it would work at certain times and then would fail the next time, even if there was no change.

Ultimately, at least presently, I found something that seems to work for my project, and wanted to note it here so that hopefully I won't have to spend hours next time!

Some background information

Vercel allows you to configure environment variables for Production, Preview, and Development, separately or combined (Project -> Settings -> Environment Variables). It also allows you to choose between plaintext, secret, and system environment variables.

What finally worked for my project

Production - use double quotes


-----END PRIVATE KEY-----\n"

Local env: '{"privateKey":"-----BEGIN PRIVATE KEY---------END PRIVATE KEY-----\n"}'

In admin: privateKey: *JSON*.*parse*(process.env.FIREBASE_PRIVATE_KEY)