convex-secret-store
v0.3.0
Published
A secret store component for Convex.
Maintainers
Readme
Convex Secret Store
Store API keys, tokens and other secrets in your Convex app, encrypted at rest, with key rotation, expiry and an audit log.
The component encrypts each value with keys you keep in your deployment's environment variables, so a copy of the database alone cannot reveal it. Only your own Convex functions can call the component, so your code decides who reads which secret.
When to use it
| You need | Use | | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | | Secrets your users or tenants add at runtime, such as a customer's key | This component | | To rotate the encryption key without rewriting every value | This component | | A record of when each secret was created, changed or deleted | This component | | A few fixed keys that only you set, such as your own OpenAI key | Plain Convex environment variables | | Protection from someone with access to your Convex deployment | A dedicated secrets manager. This component cannot give you that, because its keys live in the deployment. |
Quick start
You need a Convex project on convex 1.39 or later.
Install the package.
npm install convex-secret-storeMount the component in
convex/convex.config.tsand pass it your keys.import { defineApp } from "convex/server"; import { v } from "convex/values"; import secretStore from "convex-secret-store/convex.config"; const app = defineApp({ env: { SECRET_STORE_KEYS: v.string() }, }); app.use(secretStore, { env: { SECRET_STORE_KEYS: app.env.SECRET_STORE_KEYS }, }); export default app;Create a key and set it on your deployment. Each entry is
version:value, and the value is 32 random bytes in base64.npx convex env set SECRET_STORE_KEYS "1:$(openssl rand -base64 32)" npx convex devYou should see
Convex functions ready!. If the variable is missing, the push stops and names the variable to set.Create the client once, in
convex/secrets.ts.import { SecretStore } from "convex-secret-store"; import { components } from "./_generated/api.js"; export const secrets = new SecretStore(components.secretStore);Save a secret and read it back, in
convex/demo.ts.import { v } from "convex/values"; import { internalMutation, internalQuery } from "./_generated/server.js"; import { secrets } from "./secrets.js"; export const save = internalMutation({ args: { value: v.string() }, handler: async (ctx, { value }) => { await secrets.put(ctx, { namespace: "acme", name: "openai", value }); }, }); export const check = internalQuery({ args: {}, handler: async (ctx) => { const result = await secrets.get(ctx, { namespace: "acme", name: "openai", }); return result.ok ? "found" : result.reason; }, });npx convex run demo:save '{"value":"sk-test"}' npx convex run demo:checkThe second command prints
"found". Before a public function passes a namespace tosecrets, check that the caller may use it. The security model explains why.
Common questions
I set SECRET_STORE_KEYS with npx convex env set, and the push still
fails.
The component reads the variable only through your app config. Declare it in
defineApp({ env }) and pass app.env.SECRET_STORE_KEYS, as in step 2.
An older guide passes keys to
new SecretStore(components.secretStore, { keys }).
That was the 0.1 API. Since 0.2, keys live in SECRET_STORE_KEYS, and the
client takes only the component.
How do I move to a new key?
Add a new version and keep the old one, for example
npx convex env set SECRET_STORE_KEYS "1:<old>,2:<new>". New writes use the
highest version right away. Call secrets.rotate(ctx) once, wait until
secrets.isRotationComplete(ctx) returns true, then set the variable to the
new key only. The
keys guide
has the full steps.
What happens if I remove a key that secrets still use?
Those secrets read as key_unavailable, and rotation leaves them alone.
secrets.orphanKeyVersions(ctx) lists the missing versions. Add the key back,
and the secrets read again.
Can I read these secrets outside Convex?
Yes. convex-secret-store/envelope has the same encryption with no Convex
dependency, so a Worker or a script can read and write compatible values. See
using it outside Convex.
Are names and metadata encrypted?
No. Only the value is encrypted. Names, namespaces, metadata, expiry times and audit events are stored as plain text, so keep sensitive data out of them.
How you know it's working
secrets.getreturns{ ok: true, value }for a secret you saved.- In the Convex dashboard, the component's
secretstable shows ciphertext, never your value. - After a rotation,
secrets.isRotationComplete(ctx)returnstrue. - A secret with a
ttlMsshows as expired on an open page, without a reload.
Learn more
- Guides: keys and rotation, expiry, cleanup, using it outside Convex and upgrading from 0.2.
- Reference lists every function with its arguments, results and errors.
- Security model covers what the encryption protects against and what it does not.
- The example app shows paging, rotation and cleanup in a small Vite app.
Found a bug, or want a feature? Open an issue.
Development
npm install
npm run dev
npm run build
npm test
npm run typecheck
npm run lintDesign decisions are in docs/adr/, and domain terms are in
CONTEXT.md.
