Node.js Client for HashiCorp Vault

0.12.0 · active · verified Sun Apr 19

node-vault is a JavaScript client library for interacting with HashiCorp's Vault HTTP API, primarily designed for Node.js environments. The current stable version is 0.12.0, requiring Node.js 18.0.0 or higher. The project demonstrates a consistent release cadence with several minor and patch releases in the past year, indicating active maintenance. It provides a comprehensive wrapper around the Vault API, simplifying operations such as secret management (read, write, update, delete, list), authentication (e.g., Kubernetes Auth, token-based), and server lifecycle management (init, unseal). A key differentiator is its direct support for TypeScript with included definitions and its focus on being a reliable, actively developed client for Node.js users needing to integrate with Vault. It also allows configuration via environment variables for common Vault settings.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the node-vault client, write, read, list, update, and delete a secret using environment variables for configuration. It includes basic error handling.

import vault from 'node-vault';

async function runVaultOperations() {
  const vaultClient = vault({
    apiVersion: 'v1',
    endpoint: process.env.VAULT_ADDR ?? 'http://127.0.0.1:8200',
    token: process.env.VAULT_TOKEN ?? '', // Recommended to use VAULT_TOKEN env var for actual usage
  });

  if (!vaultClient.token) {
    console.warn("VAULT_TOKEN environment variable not set. Operations requiring authentication will likely fail.");
  }

  const secretPath = 'secret/data/my-app/config';
  const dataToWrite = {
    value: 'super-secret-data-' + Date.now(),
    environment: 'development'
  };

  try {
    console.log(`Writing secret to ${secretPath}...`);
    await vaultClient.write(secretPath, { data: dataToWrite });
    console.log('Secret written successfully.');

    console.log(`Reading secret from ${secretPath}...`);
    const result = await vaultClient.read(secretPath);
    console.log('Secret read:', result.data.data);

    console.log('Listing secrets in secret/metadata/my-app/');
    const listResult = await vaultClient.list('secret/metadata/my-app/');
    console.log('Listed keys:', listResult.data.keys);

    console.log(`Updating secret at ${secretPath}...`);
    await vaultClient.update(secretPath, { data: { updatedField: 'newValue' } });
    console.log('Secret updated successfully.');

    console.log(`Deleting secret at ${secretPath}...`);
    await vaultClient.delete(secretPath);
    console.log('Secret deleted successfully.');

  } catch (error: any) {
    console.error('Vault operation failed:', error.message);
    if (error.response?.data) {
      console.error('Vault API Error Details:', error.response.data);
    }
    if (error.message.includes('permission denied')) {
        console.error('Ensure your Vault token has appropriate policies (read, write, list, delete) for secret/data/my-app/.');
    }
  }
}

runVaultOperations();

view raw JSON →