Node.js
Learn how to fetch Doppler secrets from inside your Node.js application using the secrets download API.
SDK Information
We're planning on building an SDK for Node.js that makes fetching secrets from Doppler easier inside your application. If you'd like to get news about future updates around this, please reach out to us at [email protected].
In the meantime, you can use the secrets download API or our gitops-secrets-nodejs library to retrieve secrets at runtime.
See below for a production-ready implementation.
Until a version of our Node.js SDK has been released you can use the below code samples to fetch secrets directly from Doppler's API.
Service Token
The first step is to create a Service Token to provide read-only access to the project and config you wish to supply secrets for:

Then expose the Service Token value to your environment using the DOPPLER_TOKEN
environment variable:
export DOPPLER_TOKEN='dp.st.dev.xxxx'
Async
Create a doppler-secrets.js
file and fetch secrets from the API using either the built-in https
module or a third-party package such as axios:
// doppler-secrets.js
const https = require('https')
module.exports.getSecrets = async () => {
return new Promise(function(resolve, reject) {
https.get(`https://${process.env.DOPPLER_TOKEN}@api.doppler.com/v3/configs/config/secrets/download?format=json`, (res) => {
let secrets = ''
res.on('data', (data) => secrets += data);
res.on('end', () => resolve(JSON.parse(secrets)))
}).on('error', (e) => reject(e))
})
}
// doppler-secrets.js
const axios = require('axios')
module.exports.getSecrets = async () => {
const response = await axios.get(`https://${process.env.DOPPLER_TOKEN}@api.doppler.com/v3/configs/config/secrets/download?format=json`)
return response.data
}
Then use an async require to fetch the secrets in your application:
// app.js
const doppler = require('./doppler-secrets')
;(async() => {
const secrets = await doppler.getSecrets()
})()
Sync
Using async code to fetch secrets may have the undesirable side effect of making previously synchronous code now async.
A workaround for fetching secrets asynchronously is to provide the additional option for executing doppler-secrets.js
as a script and parsing the JSON output from stdout
:
// doppler-secrets.js
const https = require('https')
module.exports.getSecrets = async () => {
return new Promise(function(resolve, reject) {
https.get(`https://${process.env.DOPPLER_TOKEN}@api.doppler.com/v3/configs/config/secrets/download?format=json`, (res) => {
let secrets = ''
res.on('data', data => secrets += data);
res.on('end', () => resolve(JSON.parse(secrets)))
}).on('error', (e) => reject(e))
})
}
// If executed as a script
if(require.main === module) {
(async () => {
const secrets = await this.getSecrets()
process.stdout.write(JSON.stringify(secrets))
})()
}
// doppler-secrets.js
const axios = require('axios')
module.exports.getSecrets = async () => {
const response = await axios.get(`https://${process.env.DOPPLER_TOKEN}@api.doppler.com/v3/configs/config/secrets/download?format=json`)
return response.data
}
// If executed as a script
if(require.main === module) {
(async () => {
const secrets = await this.getSecrets()
process.stdout.write(JSON.stringify(secrets))
})()
}
Then execute doppler-secrets.js
synchronously:
// app.js
const secrets = JSON.parse(
require('child_process').execSync('node doppler-secrets.js')
)
Awesome Work!
Now you know how to fetch Doppler secrets from inside your Node.js application using the secrets download API.
Updated about 1 year ago