Bypass the 4kb env limit?


We are running a middleman API through netlify, which connects to a lot of other APIs in return. The issue we’re having right now is that we reached the limit for setting .env variables. The 4kb limit seems pretty low, and we couldn’t find any solution in getting around this.
Has anyone come up with a way of fixing this? Is there a way of getting around the environment variables but still somehow protecting the API keys?
What we can do is split our netlify site into more, but that doesn’t seem like an elegant solution.
Thank you.

Hi, @BlackLemon. About this:

Functions deployed at Netlify are running in AWS Lambda. Lambda itself has this limitation:

Quoting that page above:

| Function environment variables | 4 KB, for all environment variables associated with the function, in aggregate |

The most common workaround for this is to make the environment variable only available to the build system and not the to deployed functions. Then, during the build process, the build writes the environment variables into the source files themselves. This keeps the data out of the Git repo but still places the data as text in the source files.

If there are other questions about this, please let us know.

1 Like

I experienced the same issue, which in my case was caused by an overly long Firebase private key. I’m sharing my solution in case it helps anyone else.

  1. I created an environment variable named FIREBASE_PRIVATE_KEY through the UI and limited it to the “Builds” scope.
  2. In the root directory of the project, I created a file called constants.firebase.ts with the following content:
const FirebasePrivateKey = "${FIREBASE_PRIVATE_KEY}";

const Firebase = { FirebasePrivateKey };

export default Firebase;
  1. I have another file called constants.ts where I store all the app environment variables for the project. This helps avoid using process.env throughout the codebase. In this file, I added:
import Firebase from "@/constants.firebase";

export const FirebasePrivateKey = Firebase.FirebasePrivateKey || process.env.FIREBASE_PRIVATE_KEY!;

This allows me to import and use FirebasePrivateKey anywhere in the code.

  1. To make it work, I had to replace the ${FIREBASE_PRIVATE_KEY} variable in constants.firebase.ts with the actual value. I used a bash command to achieve this. In the “Build & deploy” section of my site, I changed the “Build” command from:
yarn run build


envsubst < constants.firebase.ts > temp.ts && mv temp.ts constants.firebase.ts && yarn run build

This command replaces the environment variables in constants.firebase.ts with their real values (by creating a temp file in between) and then builds the project.

Et voilá! Simple and elegant. No plugins required.

Thanks for following up on this and sharing your solution!