More than likely, the dependency dotenv does not exist when the lambda function executes, because you did not package it into your function.
When using Netlify’s dashboard to store the private key, you won’t have a .env. This is good news, because you don’t want to store that key in your repository. This is only needed in your case for local development.
Remove require('dotenv').config() from your function, because it is not needed in this case on Netlify, because process.env.STRIPE_SECRET_KEY will exist on Netlify at the time the function executes.
Add the key during local development with a different process (maybe using cross-env at the command line instead)
I’d suggest reading this article that goes into extensive details on best practices with environment variables:
But, TL;DR, I wouldn’t use dotenv at all - I’d be using javascript (during build!) to fetch from the env var you have set in the netlify UI. Saner to put sensitive values in our UI than in your repo (where anyone who gets access to your code can find it).
This was copied from https://functions-playground.netlify.com/
Once you have set the value of GREETING in the Netlify UI you have access to it in the Lambda function. It works. I’m using it. There are a number of useful snippets and associated information there - worth a look.
Yet, when I use an environment variable either locally in my .env file or in the Netlify admin for the Stipe secret key, I cannot get any Stripe transactions to work. If the secret key is hardcoded in the charge.js file, everything works.
Is there something special about the Stripe secret key? Has anyone gotten this working and if so, how?
All my other Netlify environment variables are working (Firebase, etc.) except for Stripe.
You would not be able to use the .env file for a lambda function. The admin Environment variable should work and should be accessed via process.env.YOUR_STRIPE_KEY.
Hi @witcradg, I’ve been reading up on Lambda functions and environment variables. I went through the functions-playground and ended up creating a Netlify function based on this GREETING example.
It works great! I can see my environment variable displayed on a webpage when I hit the endpoint in my browser. But, I’d like to use it in my app instead.
Sorry for the noob question, how do I retrieve the environment variable (in my case an API key) programmatically and use it in my server.js code?
I couldn’t find any examples of this. I’m probably missing something simple here. Any help is appreciated!
As I mentioned earlier, that part is working great. That was easy.
It works great! I can see my environment variable displayed on a webpage when I hit the endpoint in my browser. But, I’d like to use it in my app instead.
I just never saw a full lifecycle example start-to-finish. I figured it out on my own and posted my solution as a gist.