Rayfin

@microsoft/rayfin-client

Build the Rayfin 2.0 application client with RayfinClient.create — auth adapters, service registration, runtime config, disposal, errors, and the 1.x migration map.

Changed in 2.0

@microsoft/rayfin-client assembles one typed application client from an explicit authentication adapter and the services your app registers. Nothing is registered implicitly: client.data, client.functions, client.connectors, and client.storage exist only when you pass their adapters, and client.auth exists only with a browser-session adapter. The package depends only on @microsoft/rayfin-lib; each capability lives in its own package.

Installation

Install the client together with the adapters your app uses, all at the same exact version:

npm install @microsoft/rayfin-client@2.0.0 @microsoft/rayfin-auth@2.0.0 @microsoft/rayfin-data@2.0.0

Add @microsoft/rayfin-functions, @microsoft/rayfin-connectors, or @microsoft/rayfin-storage (same version) when you register those services. In 1.x the client pulled in auth, data, and functions transitively; in 2.0 your package.json must declare them. Changed in 2.0

Create a client

Resolve deployment configuration first, then construct the client synchronously. This is the shape the 2.0 templates generate.

src/services/rayfinClient.ts
import {
  RayfinClient,
  resolveRayfinConfig,
  type RayfinRuntimeConfig,
} from '@microsoft/rayfin-client';
import { browserSession } from '@microsoft/rayfin-auth';
import { dataService } from '@microsoft/rayfin-data';
import { functionsService } from '@microsoft/rayfin-functions';
import type { AppSchema } from '../../rayfin/data/schema';
import type { AppFunctionsSchema } from '../../rayfin/functions/src/types';

function createClient(config: {
  baseUrl: string;
  publishableKey: string;
  runtimeConfig?: RayfinRuntimeConfig;
}) {
  return RayfinClient.create({
    ...config,
    auth: browserSession({ storage: true }),
    services: {
      data: dataService<AppSchema>(),
      functions: functionsService<AppFunctionsSchema>(),
    },
  });
}

export type AppClient = ReturnType<typeof createClient>;

let client: AppClient | undefined;

export async function getRayfinClient(): Promise<AppClient> {
  if (client) return client;

  const resolved = await resolveRayfinConfig({
    apiUrl: import.meta.env.VITE_RAYFIN_API_URL,
    publishableKey: import.meta.env.VITE_RAYFIN_PUBLISHABLE_KEY,
    workspaceId: import.meta.env.VITE_FABRIC_WORKSPACE_ID,
    itemId: import.meta.env.VITE_FABRIC_ITEM_ID,
    portalUrl: import.meta.env.VITE_FABRIC_PORTAL_URL,
  });

  if (!resolved.baseUrl || !resolved.publishableKey) {
    throw new Error('Missing Rayfin configuration: run `npm run dev` or `npx rayfin up` first.');
  }

  client = createClient({
    baseUrl: resolved.baseUrl,
    publishableKey: resolved.publishableKey,
    runtimeConfig: resolved.runtimeConfig,
  });
  return client;
}

Derive the application client type with ReturnType<typeof createClient> rather than writing generics by hand: the type carries exactly the services you registered, each typed by the schema you passed to its adapter.

const rayfin = await getRayfinClient();

const todos = await rayfin.data.Todo.select(['id', 'title']).execute();
const greeting = await rayfin.functions.greet.invoke({ name: 'Ada' });
const session = rayfin.auth.getSession();

RayfinClient.create

class RayfinClient {
  static create<TAuth extends object | undefined, const TServices extends ClientServices = {}>(
    config: RayfinClientConfig<TAuth, TServices>,
  ): ComposedRayfinClient<TServices, TAuth>;

  readonly runtimeConfig?: RayfinRuntimeConfig;
  dispose(): void;
}

create is synchronous. It validates the configuration, attaches authentication to one shared HTTP transport, then runs each service factory once. The returned object exposes each registered service as a read-only property, plus auth when the adapter provides a session API.

RayfinClientConfig

OptionTypeDescription
baseUrlstringRequired. Absolute deployed backend URL, or '' for intentional same-origin routing through a dev-server proxy.
publishableKeystringRequired and nonblank. The project's public pk-* key.
authClientAuthFactoryRequired. browserSession() from @microsoft/rayfin-auth, suppliedToken() from this package, or a custom factory.
servicesRecord<string, ClientServiceFactory>Synchronous factories keyed by the namespace each exposes, such as data or storage.
runtimeConfigRayfinRuntimeConfigDeployment values from resolveRayfinConfig(), exposed as client.runtimeConfig.
functionsBaseUrlstringRoutes client.functions.<name>.invoke() to ${functionsBaseUrl}/api/<name> for local debugging; data, connectors, and storage keep using baseUrl.
headersRecord<string, string>Extra headers sent with every request.
monikerstringExplicit x-ms-workload-resource-moniker, usually the Fabric item ID. Wins over runtimeConfig.itemId.
timeoutnumberDefault request timeout in milliseconds. Default 30000; functions and connector calls use their own longer defaults.
fetchtypeof globalThis.fetchCustom fetch implementation, for example one configured with a network proxy.

Options that 1.x accepted at the top level are rejected in 2.0 Removed in 2.0:

1.x option2.0 replacement
authStorage, persistSession, autoRefreshToken, multiTabSyncPass them to browserSession({ storage, persistSession, autoRefreshToken, multiTabSync }). authStorage is now storage.
accessToken, getAccessTokensuppliedToken({ accessToken }).
onRefreshNeeded, onAuthExhaustedA custom auth factory that registers them on the transport.
useProxyRemove it and set baseUrl: ''. Passing useProxy fails with INVALID_CONFIG; an omitted baseUrl fails with MISSING_BASE_URL.

Choose authentication

Select exactly one adapter per client.

Browser session

browserSession(options?) from @microsoft/rayfin-auth creates the familiar Auth session and exposes it as client.auth. Fabric sign-in helpers from @microsoft/rayfin-auth-provider-fabric take that same client.auth.

import { browserSession } from '@microsoft/rayfin-auth';

const auth = browserSession({
  storage: true, // localStorage; false keeps the session in memory
  persistSession: true,
  autoRefreshToken: true,
  multiTabSync: true,
});

The adapter accepts the existing AuthOptions: storage, storageKeyPrefix, persistSession, autoRefreshToken, multiTabSync, and refreshTimeout.

Supplied token

Use suppliedToken(options?) in Node.js scripts, servers, and workers that already hold a token. The client has no auth property and starts no browser session.

scripts/report.ts
import { RayfinClient, suppliedToken } from '@microsoft/rayfin-client';
import { dataService } from '@microsoft/rayfin-data';
import type { AppSchema } from '../rayfin/data/schema';

const client = RayfinClient.create({
  baseUrl: process.env.RAYFIN_API_URL!,
  publishableKey: process.env.RAYFIN_PUBLISHABLE_KEY!,
  auth: suppliedToken({ accessToken: async () => process.env.RAYFIN_TOKEN ?? null }),
  services: { data: dataService<AppSchema>() },
});

try {
  const todos = await client.data.Todo.select(['id', 'title']).execute();
  console.log(todos);
} finally {
  client.dispose();
}

accessToken is a string or a callback that returns the current token synchronously or asynchronously. The callback runs per request, so it can return a rotated token; a callback that throws rejects that request before it is sent. Omit accessToken only where the backend allows publishable-key-only calls. suppliedToken replaces RayfinServerClient, which 2.0 removed.

Custom authentication factory

An application-owned token provider that needs refresh hooks can attach them in its own synchronous factory. The factory receives the shared transport and returns undefined (no client.auth).

import { RayfinClient, type ClientAuthFactory } from '@microsoft/rayfin-client';

export function createExternalAuthClient(
  baseUrl: string,
  publishableKey: string,
  getAccessToken: () => string | null,
  onRefreshNeeded: () => Promise<void>,
  onAuthExhausted: () => void,
) {
  const auth: ClientAuthFactory<undefined> = (http) => {
    http.setAccessTokenCallback(getAccessToken);
    http.setRefreshCallback(onRefreshNeeded);
    http.setAuthExhaustedCallback(onAuthExhausted);
    return undefined;
  };

  return RayfinClient.create({ baseUrl, publishableKey, auth });
}

After a 401, the transport awaits onRefreshNeeded and retries once with the current token; if the retry is also unauthorized, onAuthExhausted runs and the request fails.

Register services

NamespaceAdapterPackage
datadataService<AppSchema>()@microsoft/rayfin-data
functionsfunctionsService<AppFunctionsSchema>()@microsoft/rayfin-functions
connectorsconnectorsService<AppConnectorsSchema>({ configs, runtime?, host? })@microsoft/rayfin-connectors
storagestorageService<AppStorageSchema>()@microsoft/rayfin-storage

The schema type belongs on the adapter, not on the client. Import schema and capability types from their owning packages: EntitySchema and TypedDataClients from @microsoft/rayfin-data, FunctionsSchema from @microsoft/rayfin-functions, and ConnectorsSchema and ConnectorConfig from @microsoft/rayfin-connectors. The client package no longer re-exports them. Changed in 2.0

Every registered service shares the same authenticated transport, so transport options such as headers, timeout, fetch, and functionsBaseUrl apply to all of them. Registrations are fixed at construction: the service properties are read-only and cannot be replaced, deleted, or added later.

These names are reserved and cannot be used as service keys: auth, runtimeConfig, dispose, constructor, prototype, __proto__, then, catch, finally, and the Object.prototype member names (toString, valueOf, hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString, __defineGetter__, __defineSetter__, __lookupGetter__, __lookupSetter__). Symbol keys are not supported.

Write a custom service

A service factory receives the authenticated ApiClient and a lifecycle object. Return the service synchronously, and register cleanup as soon as you acquire a resource. This replaces subclassing the client or reaching into a protected transport.

src/services/healthService.ts
import {
  RayfinClient,
  suppliedToken,
  type ClientServiceFactory,
} from '@microsoft/rayfin-client';

const healthService: ClientServiceFactory<{ check(): Promise<unknown> }> = (
  http,
  lifecycle,
) => {
  const timer = setInterval(() => {}, 60_000);
  lifecycle.onDispose(() => clearInterval(timer));
  return { check: () => http.get('/health') };
};

export function createClient(baseUrl: string, publishableKey: string) {
  return RayfinClient.create({
    baseUrl,
    publishableKey,
    auth: suppliedToken(),
    services: { health: healthService },
  });
}

Factories and cleanup callbacks must be synchronous: a factory that returns a promise fails with INVALID_SERVICE_FACTORY, and a factory must not depend on sibling services during construction. The low-level factories — createDataApi, createFunctionsApi, createConnectorsApi, and createStorageClient — stay public for code that already owns an ApiClient.

Runtime configuration

resolveRayfinConfig(defaults, options?) loads /rayfin.config.json in a browser, overlays it on the defaults you pass, and returns { baseUrl, publishableKey, runtimeConfig }. Deployed static apps get that file from rayfin up, so one built bundle can move between environments; during rayfin dev the file is absent and the VITE_* defaults apply. See Deployment pipelines.

RayfinRuntimeConfig fieldDescription
apiUrlBackend base URL.
publishableKeyPublishable key.
workspaceId, itemId, portalUrl, tenantIdFabric coordinates for the auth broker.
applicationInsightsConnectionStringPresent when the app has Observability enabled and a resource is allocated. New in 2.0

Outside a browser there is no origin to resolve the relative path, so pass an absolute configUrl in options to load remote configuration; without it, Node.js callers get their defaults back. A configuration file that is present but unreachable, malformed, or incomplete throws RayfinConfigError with CONFIG_LOAD_FAILED, CONFIG_PARSE_FAILED, CONFIG_INVALID, or CONFIG_INCOMPLETE; only a genuinely absent file falls back to the defaults. Check the resolved baseUrl with typeof rather than truthiness if you rely on '' for same-origin routing.

Dispose a client

Call client.dispose() when the scope that owns the client ends — at the end of a script, or when a test tears down. It closes the shared transport to new requests and runs registered cleanup in reverse order, including auth cleanup.

  • New requests after disposal fail with CLIENT_DISPOSED; a request already sent can still complete, but cannot retry.
  • New browser-session refreshes reject with AUTH_DESTROYED.
  • Disposal does not sign out, revoke tokens, delete stored credentials, or wait for pending network or storage work.

Errors

Import the error classes by name. They are the canonical classes from @microsoft/rayfin-lib, so instanceof checks match errors thrown by every Rayfin package. Changed in 2.0

import { AuthError, NetworkError } from '@microsoft/rayfin-client';

try {
  await rayfin.auth.refreshSession();
} catch (error) {
  if (error instanceof AuthError) {
    console.error('Auth failed:', error.code, error.message);
  } else if (error instanceof NetworkError) {
    console.error('Network issue:', error.status, error.serverErrorCode);
  } else {
    throw error;
  }
}

RayfinClient.errors still maps SdkError, AuthError, and NetworkError to the same classes; 1.x's separate client-level AuthError class is gone.

Construction and disposal failures throw ClientCompositionError (extends SdkError), which exposes cause and cleanupErrors:

CodeMeaning
INVALID_CONFIGConfiguration is missing, not an object, or contains the removed useProxy option.
MISSING_BASE_URLbaseUrl is not a string. Use '' for same-origin routing.
MISSING_PUBLISHABLE_KEYThe publishable key is missing, not a string, or blank.
MISSING_AUTH_MODEThe auth adapter is missing or not a function.
INVALID_AUTH_CONFIGRemoved top-level auth options were combined with an adapter, or a supplied token is neither a string nor a provider.
INVALID_SERVICE_NAMEA service uses a reserved name or a symbol key.
INVALID_SERVICE_FACTORYThe service map or a factory is invalid, including an async factory.
INVALID_SERVICE_RESULTA factory returned null, a primitive, or a promise.
SERVICE_INITIALIZATION_FAILEDA factory threw without an SDK error code of its own.
CLIENT_DISPOSE_FAILEDCleanup failed during dispose(); inspect cleanupErrors.
CLIENT_DISPOSEDA request or cleanup registration was attempted after disposal.
AUTH_DESTROYEDA browser-session refresh was attempted after auth teardown.

Migrate from 1.x

2.0 has no compatibility wrappers, and renaming a constructor is not enough: register each capability explicitly. Removed in 2.0

1.x2.0
import RayfinClient from '@microsoft/rayfin-client'import { RayfinClient } from '@microsoft/rayfin-client'
new RayfinClient<AppSchema, AppFunctionsSchema>(config)RayfinClient.create({ ...config, auth: browserSession(), services: { data: dataService<AppSchema>(), functions: functionsService<AppFunctionsSchema>() } })
new ConnectorsRayfinClient<Data, Functions, Connectors>(config, runtime)Register connectorsService<Connectors>({ configs, runtime }) beside the other services. See Wiring connectors.
ExtendableRayfinClient.create({ schema, services })Register synchronous factories in services, for example storageService<AppStorageSchema>().
new RayfinServerClient({ accessToken })RayfinClient.create({ ..., auth: suppliedToken({ accessToken }) })
Subclasses of RayfinClientBaseCustom service factories that receive the shared transport.
@microsoft/rayfin-client/experimentalThe main entry plus adapters from their owning packages.
RayfinClient<AppSchema> as a typeReturnType<typeof createClient> from your own factory.
Local Node script using ConnectorsRayfinClient without hostconnectorsService({ ..., host: { type: 'cli' } }); the 2.0 adapter defaults to standalone, including in Node.js.

Call sites on client.data, client.functions, client.connectors, client.storage, and client.auth keep their APIs. See the 2.0 upgrade steps for the rest of the release.

PromptMigrate my Rayfin client construction to 2.0
Migrate the Rayfin client in my project to Rayfin 2.0. Read https://rayfin.ai/docs/reference/sdk/rayfin-client.md first. 1. Find every `new RayfinClient(`, `new ConnectorsRayfinClient(`, `new RayfinServerClient(`, `ExtendableRayfinClient`, default import of @microsoft/rayfin-client, and import from @microsoft/rayfin-client/experimental. 2. Replace each with a synchronous factory that calls `RayfinClient.create`: use `browserSession()` from @microsoft/rayfin-auth in browser code (move authStorage to `storage`, and persistSession/autoRefreshToken/multiTabSync into browserSession options), or `suppliedToken({ accessToken })` from @microsoft/rayfin-client in Node.js code. 3. Register only the services the app uses: dataService<AppSchema>() from @microsoft/rayfin-data, functionsService<AppFunctionsSchema>() from @microsoft/rayfin-functions, connectorsService<AppConnectorsSchema>({ configs, runtime }) from @microsoft/rayfin-connectors, storageService<AppStorageSchema>() from @microsoft/rayfin-storage. Remove useProxy and use baseUrl: '' if it relied on it. 4. Replace `RayfinClient<...>` type annotations with `export type AppClient = ReturnType<typeof createClient>`, and import EntitySchema, FunctionsSchema, and ConnectorsSchema from their owning packages. 5. Add @microsoft/rayfin-auth, @microsoft/rayfin-data, and any other adapter packages to package.json at the same exact version as @microsoft/rayfin-client, run npm install, then run the type-check, tests, and build and fix what they report.
Something wrong on this page?Report an issueEdit this page

On this page