Skip to main content
Auth helpers provide pluggable authentication strategies for outbound HTTP requests. They are used by createRemoteTokenIssuer and any other component that needs to authenticate with an external service. All strategies implement the AuthHeaderProvider type:
A provider returns a dictionary of HTTP headers to attach to outbound requests. The returned object may be empty (for noAuth) or contain one or more headers such as Authorization.

Strategy Comparison


noAuth

Returns empty headers. Use for local development, trusted networks, or public endpoints that do not require authentication.
Signature:

sharedSecretAuth

Returns a static header with a secret value. Use when both services share a pre-configured API key or internal secret.
Signature:

signedJwtAuth

Signs a short-lived JWT using the SDK’s AccessTokenIssuer. Use when the backend validates JWTs signed by your Key0 instance (particularly useful with RS256 key pairs).
Signature:
The generated JWT includes the following claims:

oauthClientCredentialsAuth

Fetches an access token from an OAuth 2.0 provider using the Client Credentials grant. Caches the token in memory and automatically re-fetches when it expires (with a 10-second buffer).
Signature:
Behavior details:
  • Sends a POST request with Content-Type: application/x-www-form-urlencoded.
  • Expects a JSON response with access_token (string) and expires_in (number, seconds).
  • Caches the token and re-fetches 10 seconds before expiry.
  • 10-second request timeout via AbortController.
  • Throws Key0Error with code INTERNAL_ERROR and HTTP 500 if the token request fails.

createRemoteTokenIssuer

Creates a fetchResourceCredentials callback that delegates token issuance to a remote HTTP endpoint. Use this when your backend (not Key0) is responsible for issuing API keys or custom tokens after payment.
Signature:

RemoteTokenIssuerConfig

Request format: The callback sends a POST with Content-Type: application/json and the IssueTokenParams as the body. Expected response: A JSON object with token (string, required) and optionally tokenType (string, defaults to "Bearer"). Error handling:

Standalone Service Example

Deploy Key0 as a standalone service using auth helpers for backend calls.

Backend Integration Example

Wire up remote token issuance with your existing backend.

Factory

createKey0() — the top-level factory that accepts auth-related config.