Node.js / Express

Secure Your Anthropic API Key in Node.js

In Node.js Express apps, process.env is accessible to any code running in the same process. A compromised dependency or a verbose /debug route can leak your Anthropic key. KeyVault Edge tokens fail validation outside your bound hosts.

The Problem

This is what most Node.js codebases look like today. The ANTHROPIC_API_KEY key (sk-ant-...) lives directly in the process environment. If your repo, your CI logs, or your deployment config leaks — so does your key.

JavaScript
// server.js — real Anthropic key in process.env
import Anthropic from "@anthropic-ai/sdk";
import express from "express";
import "dotenv/config";

const client = new Anthropic(); // picks up ANTHROPIC_API_KEY automatically
const app = express();
app.use(express.json());

app.post("/api/generate", async (req, res) => {
  const { prompt } = req.body;
  const msg = await client.messages.create({
    model: "claude-opus-4-5",
    max_tokens: 1024,
    messages: [{ role: "user", content: prompt }],
  });
  res.json({ text: msg.content[0].text });
});

app.listen(3000);

The Fix

Send the request to the KeyVault Edge proxy at https://proxy.keyvaultedge.com with the provider's usual path, your kvt_ token in X-KV-Token and the provider's base URL in X-KV-Target. The proxy refuses requests whose Origin isn't on the token's allowed hosts, then injects your real ANTHROPIC_API_KEY, so it never sits in your app's environment.

JavaScript
// server.js — KVE_TOKEN is host-bound; no Anthropic key in process
import express from "express";
import "dotenv/config";

const app = express();
app.use(express.json());

app.post("/api/generate", async (req, res) => {
  const { prompt } = req.body;

  const response = await fetch("https://proxy.keyvaultedge.com/v1/messages", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-KV-Token": process.env.KVE_TOKEN, // kvt_... token, server-side only
      "X-KV-Target": "https://api.anthropic.com",
      // Must match one of the token's allowed hosts
      Origin: process.env.APP_ORIGIN,
    },
    body: JSON.stringify({
      model: "claude-opus-4-5",
      max_tokens: 1024,
      messages: [{ role: "user", content: prompt }],
    }),
  });

  const data = await response.json();
  res.json({ text: data.content[0].text });
});

app.listen(3000);

Setup in 4 steps

1

Create a KVE token for your Node.js server

At keyvaultedge.com, create a new token with your sk-ant- key. Add your server's hostname as an allowed host. Requests whose Origin doesn't match are refused.

2

Replace ANTHROPIC_API_KEY in .env

Remove the Anthropic key from your environment and add KVE_TOKEN instead.

Shell
# .env
KVE_TOKEN=kvt_...your_token
APP_ORIGIN=https://yourdomain.com  # one of the token's allowed hosts
3

Swap the SDK for a native fetch call

Node.js 18+ includes fetch. Point the request at https://proxy.keyvaultedge.com with the provider's usual path (e.g. /v1/messages), send your token in X-KV-Token, the provider's base URL (https://api.anthropic.com) in X-KV-Target, and an Origin header matching one of the token's allowed hosts. The body is the provider's normal request body; the proxy sets the provider's auth header with your real key and forwards it.

4

Scope the token to the endpoints you use

In the KeyVault Edge dashboard, restrict the token to POST /v1/messages and set a rate limit and monthly cost cap. A leaked token then can't be used for anything else, and its spend is bounded.

What this protects, and what it doesn't

  • Your real ANTHROPIC_API_KEY stays at the edge, so it can't leak from your repo, logs, bundle or server environment.
  • The kvt_ token is still a credential. A copied token can be replayed by anyone who can send the right headers, including a fake Origin from a server. The protection is that each token has its own rate limits, monthly cost cap and endpoint scope, and you can revoke it in one click without rotating the provider key.
  • The cost cap is estimated from the usage the provider reports, so a request already in flight can overshoot it slightly.

Plans and limits are on the pricing page.

Start protecting your keys

Your Anthropic key is one leaked .env file away from being exploited. With KeyVault Edge the real key stays at the edge, and the token that replaces it is capped, scoped and revocable in one click.