Using Environment Variables in Node.js
Developers need to have different values for some variables on different environments. For example, a base URL of https://sandbox.something.com
vs https://something.com
. This guide will show you how this can be achieved.
Node.js SDK
In Node.js integrations, the Secrets Manager is used for defining not only secrets, but also environment variables.
Things to Know
The .env
file will automatically be ignored and should not be checked in to your repo.
Using variables in .env file
Create a file in the root of your project called .env
if you do not already have one. Inside of that file, define variables as needed. This file can be used to define variables that differ by environment, a URL for example. Within your code, the key is accessed by using process.env.{key}
.
Local vs. Sandbox/Prod Differences
When developing on your local machine, values of your variables will be taken directly from .env
file.
When developing on sandbox or production, values of your variables will be taken directly from Secrets Manager
Secrets and Variables on Local Environment
On local environment, the value will be taken directly from the .env
file:
mytoken=12345contentApiHost=https://api.sandbox.{myOrg}.arcpublishing.comconsole.log(process.env['mytoken'])// Output: 12345
console.log(process.env['contentApiHost'])// Output: https://api.sandbox.{myOrg}.arcpublishing.com
Secrets and Variables on Sandbox and Production
On Sandbox and Production environments, the value will be taken from Secrets Manager. In this case I would add to Secrets Manager with a payload:
{ "secretName": "mytoken", "secretValue": "67890"}
// Second POST{ "secretName": "contentApiHost", "secretValue": "https://api.sandbox.{myOrg}.arcpublishing.com"}console.log(process.env['mytoken'])// Output: 67890 (notice this is not the same from .env since it's not local)
console.log(process.env['contentApiHost'])// Output: https://api.{myOrg}.arcpublishing.com
Example of using secrets within a request
mytoken=12345
Using it within a request:
const ARC_PERSONAL_ACCESS_TOKEN = process.env['mytoken']
const response = await axios.get(someUrl, { headers: { Authorization: `Bearer ${ARC_PERSONAL_ACCESS_TOKEN}` }})
Environment Configuration
The Node SDK allows clients to specify environment specific configuration via dotenv files in the root directory of the running application. See expected folder structure below.
.├── .env.development├── .env.production├── .env.sandbox├── .gitignore├── README.md├── package.json├── src│ ├── eventsHandlers│ │ ├── defaultHandler.js
Utilize the .env.{my-environment}
files in the root directory of your integration to provide environment specific configuration to your application. Do not store secrets or api keys in these files, for those see the Secrets section.
Secrets
Secrets are managed via the Integrations Admin API. Secrets that you add to your integration via that API get placed as environment variables available to your application on the process.env
object.
To test secrets while running the local development server, you should create a file called .env
in the root directory of your project and store them there.
Secrets and Variables on Local Environment
On local environment, the value will be taken directly from the .env
file:
mytoken=12345contentApiHost=https://api.sandbox.{myOrg}.arcpublishing.com
console.log(process.env['mytoken'])// Output: 12345
console.log(process.env['contentApiHost'])// Output: https://api.sandbox.{myOrg}.arcpublishing.com
Secrets and Variables on Sandbox and Production
On Sandbox and Production environments, the value will be taken from Secrets Manager. In this case I would add to Secrets Manager with a payload:
{ "secretName": "mytoken", "secretValue": "67890"}
console.log(process.env['mytoken'])// Output: 67890 (notice this is not the same from .env since it's not local)
Remember that these .env
variables are packaged with your deploy, so you’ll need to re-deploy if you add or change a value in Secrets Manager.
Example of using secrets within a request
.env
file (do not commit this file)
mytoken=12345
Using it within a request:
const ARC_PERSONAL_ACCESS_TOKEN = process.env['mytoken']
const response = await axios.get(someUrl, { headers: { Authorization: `Bearer ${ARC_PERSONAL_ACCESS_TOKEN}` }})
Help us improve our documentation! Let us know if you do not find what you are looking for by posting suggestions to Our Ideas Portal.