InworldJwtCache keeps one JWT per (apiKey, resources) pair in process and
refreshes it ~60s before expirationTime. Use it on Blazor/ASP.NET Core
backends to avoid hitting /auth/v1/tokens/token:generate on every request.
This example assumes using Inworld; is in scope and apiKey contains your Inworld API key.
varapiKey=Environment.GetEnvironmentVariable("INWORLD_JWT_KEY")is{Length:>0}k?k:DecodeKeyPair(Environment.GetEnvironmentVariable("INWORLD_API_KEY")).key;varapiSecret=Environment.GetEnvironmentVariable("INWORLD_JWT_SECRET")is{Length:>0}s?s:DecodeKeyPair(Environment.GetEnvironmentVariable("INWORLD_API_KEY")).secret;if(string.IsNullOrEmpty(apiKey)||string.IsNullOrEmpty(apiSecret)){thrownewAssertInconclusiveException("INWORLD_API_KEY (or INWORLD_JWT_KEY + INWORLD_JWT_SECRET) is required.");}// Create one cache per (apiKey, resources) and reuse it across requests.usingvarcache=newInworldJwtCache(apiKey,apiSecret);// First call mints a fresh JWT.varfirst=awaitcache.GetAsync();// Second call returns the same cached token (no second round trip).varsecond=awaitcache.GetAsync();// After invalidate the cache mints a new token.cache.Invalidate();varthird=awaitcache.GetAsync();// Inworld may issue identical JWTs within the same clock second — don't// rely on the token value changing; just verify we got one back.