DEV Community

Cover image for How to Store a Long, Multi-line Private Key in an Environment Variable
Claudio F.
Claudio F.

Posted on • Updated on

How to Store a Long, Multi-line Private Key in an Environment Variable

Most of the time you store simple, straight forward private keys for your deployments, but sometimes your project requires some complex ones like this one below.

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCt95m8QfrcIR+o
0a7S1hTtcbt7NhEJLiVELktu1XsFSitazhwvrRuVGn1c+cqgQbmAp7x/xoaYcLfY
tAaKR9v1xNEIBEwA/h1k+TFl7xJqH6Q07LBVYR0kVWiXgUHo6XWhlJEy4PCC5Yqo
eV2ZsA4Jjsh8c0cWTEFX9cCCT8cRIOfMeR/TcAeF/bPEOnmrbJJLV+5ivJUCHpio
[...]
tQeRV0IfFdnW8V3y6XI+xFYBOkx7D4BGFF8fUNr0iYMCWvICu2cJlWqi6Qmlk+LJ
br9Sl3Y2c1bXQbsW2ZKQJTANKBxeY1i9Au1pdCUCgYEAzbqB5omxKXcXx/0ZI/ao
cIJ9hQ6S18JIniG7DFyyUzqHansnqD1Qqxefl1fFLcuWktUivfbr7v5FoWUjwKLQ
JOJOVI3DZCsOumPrpEpjRa4cCQEjNPqYHL4voIR5IHRr+4iKSVrXUcmhdyqxjonG
11Avbqh479wA4VYksAF9AM4=
-----END PRIVATE KEY-----
Enter fullscreen mode Exit fullscreen mode

When your project is ready to launch, but you're getting stuck in a few minor, silly errors like Failed to parse private key: Error: Invalid PEM formatted message, you know that the problem is how the environment variable is stored, but have tried a lot of things to try to get it to work.

Well... hoping to save someone some time, I've ran across this pretty elegant solution in GitHub.

Store your multi-line Private Key like this:

FIREBASE_PRIVATE_KEY='{"privateKey":"-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE...-----END PRIVATE KEY-----"}'
Enter fullscreen mode Exit fullscreen mode

and use it in the code as following:

const { privateKey } = JSON.parse(process.env.FIREBASE_PRIVATE_KEY)
Enter fullscreen mode Exit fullscreen mode

Peace!

Top comments (6)

Collapse
 
lukfcsl profile image
Lucas Farias • Edited

Hi Claudio, thank you for the post!
I'm still facing an issue, where the variable is not being read correctly from the environments variables set through Vercel's UI.
I'm getting:

Build error occurred
20:28:41.478    SyntaxError: Unexpected token ' in JSON at position 0
20:28:41.478        at JSON.parse (<anonymous>)
20:28:41.478        at Module.23aj (/vercel/workpath0/.next/serverless/pag
Enter fullscreen mode Exit fullscreen mode

this is because the key is undefined or sort of

Collapse
 
lukfcsl profile image
Lucas Farias

Fixed it removing the single quotes wrapping the string.
It's all working perfect now, thank you!!!

Collapse
 
inezabonte profile image
Ineza Bonté Grévy • Edited

Still facing the issue
JSON.parse: unexpected character at line 1 column 1 of the JSON data

My key is in the .env.local file

FIREBASE_PRIVATE_KEY={"privateKey":"-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE...-----END PRIVATE KEY-----"}
Enter fullscreen mode Exit fullscreen mode

and I'm retrieving it like this

const { privateKey } = JSON.parse(process.env.FIREBASE_PRIVATE_KEY);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nanythery profile image
Nadine M. Thêry • Edited

Hi! old thread here, but in case it helps, this solution worked for me for entering our Google Cloud Credentials in our Next Js project, but with the following adjustments:

  • The multiline key should be an actual valid JSON, otherwise it will fail just like happend to @inezabonte. To solve this I manually added all the breaklines with \n at the end of all the lines (including the last one). And created a long string with them. Just in case, use a JSON validator to be sure that your key is properly formatted.

Thank you @cfofiu for the main approach to this.

Collapse
 
jdogcoder profile image
Jasper Mayone

Still having no luck with this.

Collapse
 
anderaa27 profile image
Ander • Edited

Hi,
another way to store your multiline key is using the multiline string type (backticks)..`...

BACKTICKS = `
Example :
RSA_PKEY=(BACKTICKS)..............YOUR KEY..............(BACKTICKS)