Node.js HashiCorp Vault Client

1.0.2 · active · verified Wed Apr 22

node-vault-client is a pure JavaScript client library designed for interacting with HashiCorp Vault, specifically tailored for Node.js environments. Currently at version 1.0.2, this library provides core functionalities for reading, listing, and writing secrets, as well as managing authentication tokens. It supports various Vault Auth Backends, including AWS IAM, AppRole, and Token-based authentication, and crucially handles the automatic renewal of issued auth tokens to maintain session validity. While its release cadence appears intermittent, recent updates address critical issues, indicating ongoing maintenance. A key differentiator is its explicit focus on pure JavaScript implementation and built-in token lease renewal, which simplifies common operational patterns for Node.js applications integrating with Vault. It requires Node.js version 14 or higher and has a peer dependency on the `config` package.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Vault client using AppRole authentication, read a secret, and write data to Vault. It uses environment variables for sensitive credentials.

const VaultClient = require('node-vault-client');

// Initialize the Vault client. The 'boot' method handles singleton instance management.
// Ensure VAULT_APP_ROLE_ID and VAULT_APP_ROLE_SECRET_ID environment variables are set.
const vaultClient = VaultClient.boot('main', {
    api: { url: 'https://vault.example.com:8200/' }, // Replace with your Vault server URL
    auth: {
        type: 'appRole', // Supports 'appRole', 'token', 'iam', 'kubernetes'
        config: {
            role_id: process.env.VAULT_APP_ROLE_ID ?? 'your-approle-role-id',
            secret_id: process.env.VAULT_APP_ROLE_SECRET_ID ?? 'your-approle-secret-id' // Required for AppRole
        }
    },
    // Optional: Pass 'false' to disable logging, or a custom logger object.
    logger: console
});

// Read a secret from a specified path in Vault
vaultClient.read('secret/data/my-application/config')
    .then(response => {
        console.log('Successfully read secret:', response.data.data); // Vault K/V v2 stores data in .data.data
    })
    .catch(e => {
        console.error('Error reading secret:', e.message);
        // Implement robust error handling, e.g., retry logic, specific Vault error codes.
    });

// Example of writing a secret to Vault
vaultClient.write('secret/data/my-application/settings', { value: 'some_setting', enabled: true })
    .then(() => console.log('Successfully wrote secret.'))
    .catch(e => console.error('Error writing secret:', e.message));

view raw JSON →