Skip to main content

Class: ServiceContext

ServiceContext is a singleton that manages the service principal's WorkspaceClient and workspace ID. WarehouseResource owns warehouse bindings.

It's initialized once at app startup and provides the foundation for both service principal and user context execution.

Constructors

Constructor

new ServiceContext(): ServiceContext;

Returns

ServiceContext

Methods

createCallerContext()

static createCallerContext(
   token: string, 
   userId: string, 
   userName?: string, 
   userEmail?: string): CallerContext;

Create an immutable caller context from the existing user request headers.

Parameters

ParameterTypeDescription
tokenstringThe user's access token from x-forwarded-access-token header
userIdstringThe user's ID from x-forwarded-user header
userName?stringOptional user name
userEmail?stringOptional email from x-forwarded-email

Returns

CallerContext

Throws

Error if token is not provided


createUserContext()

static createUserContext(
   token: string, 
   userId: string, 
   userName?: string, 
   userEmail?: string): CallerContext & UserContext;

Parameters

ParameterType
tokenstring
userIdstring
userName?string
userEmail?string

Returns

CallerContext & UserContext

Deprecated

Use ServiceContext.createCallerContext.


get()

static get(): ServiceContextState;

Get the initialized service context.

Returns

ServiceContextState

Throws

Error if not initialized


getClientOptions()

static getClientOptions(): ClientOptions;

Get the client options for WorkspaceClient. Exposed for testing purposes.

Returns

ClientOptions


initialize()

static initialize(options?: {
  warehouseId?: string | boolean;
}, client?: WorkspaceClient): Promise<ServiceContextState>;

Initialize the service context. Should be called once at app startup. Safe to call multiple times - will return the same instance.

Parameters

ParameterTypeDescription
options?{ warehouseId?: string | boolean; }A resolved warehouse ID, or a boolean enabling discovery.
options.warehouseId?string | boolean-
client?WorkspaceClientOptional pre-configured WorkspaceClient to use instead of creating one from environment credentials.

Returns

Promise<ServiceContextState>


isInitialized()

static isInitialized(): boolean;

Check if the service context has been initialized.

Returns

boolean


reset()

static reset(): void;

Reset the service context. Only for testing purposes.

Returns

void

Databricks Developer Hub

Ready to ship your next agentic app in minutes?

Read docs