Secure Your OpenAI API Key in Node.js
The Problem
This is what most Node.js codebases look like today. The OPENAI_API_KEY key (sk-proj-...) lives directly in the process environment. If your repo, your CI logs, or your deployment config leaks — so does your key.
// server.js — real OpenAI key in process.env
import express from "express";
import OpenAI from "openai";
import "dotenv/config";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const app = express();
app.use(express.json());
app.post("/api/chat", async (req, res) => {
const { messages } = req.body;
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages,
});
res.json(completion.choices[0].message);
});
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 OPENAI_API_KEY, so it never sits in your app's environment.
// server.js — real key never in this process; KVE_TOKEN is host-bound
import express from "express";
import "dotenv/config";
const app = express();
app.use(express.json());
app.post("/api/chat", async (req, res) => {
const { messages } = req.body;
const response = await fetch("https://proxy.keyvaultedge.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-KV-Token": process.env.KVE_TOKEN, // kvt_... token, server-side only
"X-KV-Target": "https://api.openai.com",
// Must match one of the token's allowed hosts
Origin: process.env.APP_ORIGIN,
},
body: JSON.stringify({ model: "gpt-4o", messages }),
});
const data = await response.json();
res.json(data.choices[0].message);
});
app.listen(3000);Setup in 4 steps
Create a sanitized token at keyvaultedge.com
Sign in, click Tokens → New Token. Paste your OpenAI key, set provider to OpenAI, and add your server hostname as an allowed host. Copy the KVE_TOKEN.
Update your .env file
Replace OPENAI_API_KEY with KVE_TOKEN. If you use dotenv, no other change is needed to your env loading code.
# .env (before)
# OPENAI_API_KEY=sk-proj-YOUR_REAL_KEY
# .env (after)
KVE_TOKEN=kvt_...your_token
APP_ORIGIN=https://yourdomain.com # one of the token's allowed hostsReplace the OpenAI SDK with native fetch to the proxy
Node.js 18+ has built-in fetch. Point the request at https://proxy.keyvaultedge.com with the provider's usual path (e.g. /v1/chat/completions), send your token in X-KV-Token, the provider's base URL (https://api.openai.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.
Set rate limits in the dashboard
Open the token in your KeyVault Edge dashboard and set a requests-per-minute limit matching your expected load. This prevents a compromised client from burning through your OpenAI quota.
What this protects, and what it doesn't
- Your real OPENAI_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.
More on layered defences in how to protect your OpenAI API key. Plans and limits are on the pricing page.
Start protecting your keys
Your OpenAI 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.