How to reference a file as an environment variable in Vercel?
Answered
Seppala Siberian Sleddog posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
I'm using Google Cloud packages so they require the environment variable to be stored as a file so i have downloaded the file on my local machene and referenced it like so
and i have the file in the root of my project but i can't access it in Vercel i get this error
GOOGLE_APPLICATION_CREDENTIALS="serviceaccount.json"
and i have the file in the root of my project but i can't access it in Vercel i get this error
Error: The file at "serviceaccount.json" does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/var/task/"serviceaccount.json"'
18 Replies
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
and where is your serviceaccount.json file located?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
in the root directory
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
try reading the file, while also constructing the path using
process.cwd
to determine the project's root directory![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
reading files isn't allowed in Vercel, i have tried it, and the file should be specified in the environment variable not in the code, does
process.cwd
works in .env
files?![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
no, umm when do you plan to read the file?
if its at build time, its fine actually
if its at request time, im not sure
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
it's a request time for sending the credentials to Google API
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
its a json file, shouldn't you be able to import it directly?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
the SDK access it from this environment variable
GOOGLE_APPLICATION_CREDENTIALS
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
oh...
Is there any other way for the SDK to read the json file?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
https://cloud.google.com/speech-to-text/docs/before-you-begin here you can see how it is setup?
i found a way to authentcate using an API key but it would be nice to know how to use
serviceaccout.json
![Avatar](https://cdn.discordapp.com/avatars/414145877335080960/c7ab639b9f825e9f8db0ed902b831f2b.webp?size=256)
linesofcode
two approaches
Answer
![Avatar](https://cdn.discordapp.com/avatars/414145877335080960/c7ab639b9f825e9f8db0ed902b831f2b.webp?size=256)
linesofcode
1) store the serviceaccount json as a giant string in an env variable
2) if your env variables are exceeding vercel limit (4kb), encrypt the service account json file and store it in your codebase. During runtime decrypt it
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Seppala Siberian SleddogOP
okay thanks