Skip to content

Runtime auth

By default a capability uses one key, stored as a secret on the capability Worker. That fits one app and one provider account.

Terminal window
cf workers secrets update STRIPE_API_KEY --worker capa-stripe --type secret_text

Runtime auth lets one deployed capability serve many accounts. Pass the key for this call in options.auth.

await env.STRIPE.paymentIntents.create(
{ amount: 2500, currency: "usd" },
{ auth: { apiKey: tenant.stripeKey } },
);

The per-call key wins. If you pass none, the capability uses its secret. If it has no secret either, the call throws: set the capability API key secret or pass options.auth.apiKey.

ModeWhere the key comes fromUse it for
Worker secretSTRIPE_API_KEY on capa-stripeOne app, one provider account
Runtime authoptions.auth on each callOne capability, many accounts
Gateway vaultEach user’s vault in capa-gatewayAgents acting for many users

options is always the last argument.

interface CallAuth {
apiKey?: string;
username?: string;
password?: string;
headers?: Record<string, string>;
}
FieldUse it for
apiKeyBearer and Private-Token APIs, such as Stripe, GitHub, and GitLab.
username and passwordBasic auth APIs, such as HTML/CSS to Image.
headersExtra headers a provider requires, such as Twitch Client-Id.

Twitch needs a token and a client ID on every request:

await env.TWITCH.channels.list({
query: { broadcaster_id: "141981764" },
auth: {
apiKey: tenant.twitchToken,
headers: { "Client-Id": tenant.twitchClientId },
},
});

The gateway keeps each user’s keys in a vault. When an agent’s code calls a capability, the gateway reads that user’s key and adds it as options.auth. The agent’s code never sees the key, and the capability needs no secret of its own.

Runtime auth doesRuntime auth does not
Set the key or credentials for one callChange the upstream base URL per call
Add provider auth headersStore tenants or keys
Fall back to the Worker secretRefresh OAuth tokens
  • Evidence records hold the method, the URL, and the status. They never hold auth values or request headers.
  • Errors do not echo key values.
  • Use the narrowest key the provider offers, such as a restricted Stripe key or a fine-grained GitHub token.