Week 9: Use Environment Variables in your Custom Functions

Hopefully you’ve found Custom Functions at this point. They’re crazy useful! You can do almost anything with JavaScript/Typescript in NodeJS, including things like installing packages via NPM/Yarn.

Let’s just say you installed the Twilio SDK and now have some code like this staring you in the face…

Use Environment Variables in your Custom Functions (screenshot 1)

Sure, you could easily replace this string with your actual API key and then deploy your code to a public repo and then have a bot steal the key and then pay Twilio $10,000’s for some thieves API usage. You could totally do that…or use Environment Variables!

Use Environment Variables in your Custom Functions (screenshot 2)

Environment Variables can be created in the 8base Backend Console’s settings area. Their values are programmatically accessible from within your Custom Functions so that, 1) you can safely read their values, and 2) you can safely change their values without editing your code.

Once set, just access them using process.env.KEY_NAME, like this:

Use Environment Variables in your Custom Functions (screenshot 3)

If you have any questions about this, definitely ask them in the 8base Community!

Happy Developing!

Sebastian