nsc login for local development, or provide an explicit token source when the default credential resolution does not fit your environment.
Workload identity federation
Federation lets an external workload exchange its OIDC identity for short-lived, scoped Namespace credentials. Prefer this approach for automated workloads instead of storing a shared, long-lived token. Federation happens before the TypeScript SDK starts. The SDK does not exchange third-party OIDC tokens directly. A provider integration or the Namespace CLI establishes the Namespace credential, thencreateComputeClient() loads it through the default credential resolution.
The general flow is:
- Configure a Namespace trust relationship that restricts the accepted OIDC issuer, subject, audience, and grants.
- Obtain an OIDC token from the workload’s identity provider.
- Exchange that token for short-lived Namespace credentials.
- Start the TypeScript application and create a client without an explicit
tokenSource.
Applications running inside a Namespace workload already receive a workload credential.
createComputeClient() discovers it automatically through NSC_TOKEN_FILE or /var/run/nsc/token.json, so no external exchange step is needed.Exchange an OIDC token
Configure a trust relationship, obtain the provider’s OIDC token, and exchange it before starting the application:loadDefaults() can find it.
Provider-specific setup
See the federation guides for OpenID Connect, GitHub Actions, CircleCI, Google Cloud, AWS, and RWX. These guides cover obtaining the provider token and configuring trust with Namespace; the SDK consumes the resulting Namespace credential in the same way for every provider.Local development with nsc login
For local, interactive development, install the Namespace CLI and authenticate in the browser:
nsc login is the short alias for nsc auth login. Both open a browser where you select the workspace to log in to.
The SDK reads the credentials written by nsc login from the platform-specific user configuration directory. You do not need to load them explicitly:
Default credential resolution
createComputeClient() uses loadDefaults when tokenSource is omitted. Credentials are resolved in this order:
- The token file named by
NSC_TOKEN_FILE. - The workload token at
/var/run/nsc/token.json, when that file exists. - The current user’s Namespace token file.
loadDefaults()
Loads the first available token source using the default resolution order above.
Example
Pass the function itself to defer file access until the client first needs a token:loadDefaults() to load credentials immediately.
API reference
Arguments and options
This function does not accept arguments.Return value
Returns a promise that resolves to the first availableTokenSource.
loadUserToken()
Loads credentials for the user who ran nsc login. Use this when an application must not fall back to workload credentials.
Example
await loadUserToken() instead when you want missing or invalid credentials to fail during client setup.
API reference
Arguments and options
This function does not accept arguments.Return value
Returns a promise that resolves to the current user’sTokenSource.
loadWorkloadToken()
Loads a workload bearer token from NSC_TOKEN_FILE, or from /var/run/nsc/token.json when the environment variable is unset. It does not fall back to user credentials.
Example
bearer_token. Workload bearer tokens are returned directly and are not refreshed through a user session.
API reference
Arguments and options
This function does not accept arguments.Return value
Returns a promise that resolves to a workloadTokenSource.
fromBearerToken()
Creates a token source from an existing bearer token. Use this when another trusted component supplies the credential.
Example
API reference
Arguments and options
string
required
The bearer token used to authenticate Namespace requests.
Return value
Returns aTokenSource that issues the supplied bearer token.
TokenSource
A token source issues a bearer token with at least the requested remaining lifetime.
number
required
The requested minimum remaining token lifetime in milliseconds.
boolean
Requests a fresh token instead of a cached token. A custom source should honor this when it can refresh credentials.
ComputeClientOpts.tokenSource accepts either a TokenSource or a zero-argument function that returns one, synchronously or asynchronously. The SDK invokes provider functions on first use and retries resolution on a later request if the provider rejects.
The client keeps issued tokens in memory and reuses a token while its JWT exp claim satisfies the requested lifetime. Tokens without an exp claim are treated as non-expiring. Concurrent requests that need a token share a compatible in-flight issuance request. Compute RPCs request at least five minutes of remaining validity.
User credentials created by nsc login include a session credential. The SDK uses it to issue short-lived bearer tokens and refreshes them before they have too little validity for an operation. Explicit and workload bearer tokens have no session-based refresh.
bearerAuthInterceptor()
Create a Connect interceptor that authenticates compute RPCs with tokens from a TokenSource. Use it when supplying a custom transport through ComputeClientOpts.transport.
Example
WhenComputeClientOpts.transport is set, the SDK uses that Connect transport as-is and ignores region and tokenSource. The transport is then responsible for authentication, so add bearerAuthInterceptor() to it:
region instead.
API reference
Arguments and options
TokenSourceInput
required
A
TokenSource, or a zero-argument function that returns one synchronously or asynchronously. The interceptor wraps it with the SDK’s in-memory token cache. Passing a source already wrapped by the SDK is a no-op.number
default:"300000"
The minimum remaining token validity requested for every RPC, in milliseconds. The default is the SDK-wide five-minute minimum.
Return value
Returns a ConnectInterceptor that obtains a token before each request and adds it as an Authorization: Bearer header. Valid tokens are reused, and concurrent requests share a compatible in-flight issuance request.
The interceptor does not refresh a token and retry a request after the server rejects its authentication. The resulting Connect error is returned to the caller.
Omitting the interceptor from a custom transport leaves compute RPCs unauthenticated.
Authentication errors
loadDefaults() and loadUserToken() reject with NotLoggedInError when the user token file is missing. Prompt the user to run nsc login or handle the error by name:
NSC_TOKEN_FILE, or when you call loadUserToken() or loadWorkloadToken() directly. loadDefaults() is more forgiving: it ignores any failure while accessing or loading the standard workload token at /var/run/nsc/token.json and falls back to the user token, so a corrupt file at that path is not reported.
Token issuance failures surface as their underlying errors, and rejected RPC authentication surfaces as a Connect error with code Unauthenticated.
Related documentation
Compute Client
Configure authentication, region, and transport.
Create and manage instances
Create, inspect, extend, suspend, and destroy instances.