Setting up / injecting enviroment variables for functions

Hi :wave:

I have used lambda stripe functions (netlify-cli) to build a payment flow. Now I have found out that I need to go to productions I feel a bit lost how to swap out my test-api key for a production-api key …

I have tried to add the following keys in the Netlify App

TEST_PRIVATE_KEY (local, staging, branch)
PRIVATE_KEY (production)

Ideally, I would like to swap keys with TEST_ prefix on production with production keys. I was hoping to solve it like this ENV !== “production” ? TEST_PRIVATE_KEY: PRIVATE_KEY

After googling and browsing I have learned that it is not possible to pass .toml vars into functions and thus setting the desired ENV var. Is this correct, is this still the case or am I am missing something? What would be a good workaround for this?

I would duplicate your functions but call one of them -dev and keep the dev keys in that function. You can also point Stripe Testing Mode webhooks to that -dev function as well

2 Likes

Thanks for the pragmatic solution! :+1: For this project, I guess it will work out fine for now since we got two functions that we use.

I wonder how to deal with it if you got more functions to let’s say 14? :thinking: This would mean that I would have to do a lot of manual work in order to update/deploy them.

Why 14? If I would want to use Fauna DB based on the example below that would (create, delete-batch, delete, read-all, read, update) two different DB models (Todo, User) and have 2 stripe functions to handle payments.

Is there or will be there a better way to set this up?
Am I missing the point of how to set up dev, test, production env when working with functions?

Sorry for the stupid questions, I am new to working with Netlify functions and would like to understand what the proper workflow should be when it comes to development, testing, shipping to production.

Hi @GoJAM, we do intend to pass netlify.toml environment variables in to functions, but this functionality is not there yet. One option you have is to bundle with webpack and use a plugin that injects environment variables during the build. That way you can use all the environment variables that are available during the build in your function deployment. Note that you don’t have to do this with webpack, but that is one way of doing it.

1 Like

That is awesome news @futuregerald. Thank you for your reply! :slight_smile: