Introduction
HashiCorp Vault is the go-to solution for centralizing and securing secrets in modern environments. In 2026, cloud-native applications must avoid storing API keys or passwords in plain text. Vault provides fine-grained access control, automatic rotation, and complete auditing. This tutorial walks you through deploying Vault and integrating it into a Node.js TypeScript application step by step. You will learn how to create policies, enable secret engines, and retrieve credentials dynamically.
Prerequisites
- Node.js 20+ and npm
- Docker installed
- Basic knowledge of TypeScript and REST
- A Linux terminal or WSL
Starting Vault in dev mode
docker run -d --name vault -p 8200:8200 -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' hashicorp/vault:latestThis command launches Vault in development mode with a root token. Useful for quick testing, but never in production.
Vault client configuration
import * as vault from 'node-vault';
const client = vault({
apiVersion: 'v1',
endpoint: 'http://localhost:8200',
token: process.env.VAULT_TOKEN || 'myroot'
});
export default client;This module initializes the Vault client with the root token. Always store the token in an environment variable.
Enabling the KV v2 engine
curl -X POST -H "X-Vault-Token: myroot" \
-d '{"type":"kv","options":{"version":"2"}}' \
http://localhost:8200/v1/sys/mounts/secretEnables the KV version 2 engine under the /secret path to store versioned key-value pairs.
Creating an access policy
{
"path": {
"secret/data/app/*": {
"capabilities": ["read", "list"]
}
}
}This policy restricts access to application secrets. Apply it afterward via the API or Vault UI.
Applying the policy and creating a token
curl -X POST -H "X-Vault-Token: myroot" \
-d '{"policies":["app-policy"]}' \
http://localhost:8200/v1/auth/token/createGenerates a token limited to the app-policy. Use this token in your applications instead of the root token.
Best practices
- Always use short-lived, renewable tokens
- Enable audit logging as soon as you go to production
- Prefer dynamic secrets (database, AWS) over static ones
- Store the root token offline and disable it after initialization
- Version all your policies in Git
Common mistakes to avoid
- Leaving dev mode enabled in production
- Forgetting to renew tokens before expiration
- Storing the root token in source code
- Not enabling TLS for client-server communications
Going further
Discover our advanced courses on secret management and cloud security: https://learni-group.com/formations