Environment-Based Configuration
The easiest way to initialize theWachtClient is to use from_env(). This method reads the standard Wacht environment variables and builds the client automatically.
from_env(), the SDK expects the following environment variables to be set:
WACHT_API_KEY(Required): Your secret backend API key, starting withwk_test_orwk_live_.WACHT_PUBLISHABLE_KEY(Required): Your publishable key, starting withpk_test_orpk_live_. The SDK uses this to automatically derive your Frontend API URL.- Alternatively, you can provide
WACHT_FRONTEND_HOSTdirectly.
- Alternatively, you can provide
Programmatic Configuration
If you manage your secrets using a vault, a configuration manager, or you need to instantiate multiple clients for different deployments, you can construct theWachtConfig explicitly.
Public Key Verification
When verifying JWT session tokens (e.g., in a middleware layer), the SDK needs the deployment’s public signing key. By default, theWachtClient will lazily fetch and cache the JSON Web Key Set (JWKS) from your Frontend Host (/.well-known/jwks.json) the first time it needs to verify a token.
Hardcoding the Public Key
In serverless environments or if you want to avoid the initial HTTP request to fetch the JWKS, you can provide the public key ahead of time using theWACHT_PUBLIC_SIGNING_KEY environment variable.
If WACHT_PUBLIC_SIGNING_KEY is present in the environment (formatted as a PEM key), WachtClient::from_env() will automatically load and use it for all token verifications, completely bypassing the network fetch.