Rayfin

@microsoft/rayfin-functions

SDK reference for functionsService, FunctionClient, FunctionsSchema, InvokeOptions, FunctionsError, and typed client.functions invocations.

Changed in 1.36

@microsoft/rayfin-functions is the client-side SDK package behind client.functions.<name>.invoke(...). Server-side function authoring uses @microsoft/fabric-user-data-functions; see Writing a function.

Installation

Changed in 2.0

Install the package in every app that calls functions, at the same version as @microsoft/rayfin-client. In 1.x the client pulled it in; the 2.0 client depends only on @microsoft/rayfin-lib.

npm install @microsoft/rayfin-functions@2.0.0

Exports

ExportKindPurpose
functionsService<TSchema>()FunctionRegisters typed client.functions on a client built with RayfinClient.create. New in 2.0
FunctionClient<TInput, TOutput>ClassClient for one named function.
FunctionsErrorClassFunction invocation error extending SdkError.
createFunctionsApi<TSchema>(apiClient)FunctionCreates the typed functions proxy for code that already owns an ApiClient.
FunctionInvocationResponse<TOutput>TypeTransport envelope returned by the backend before invoke() unwraps output.
InvokeOptionsTypePer-call headers and timeout.
FunctionsSchemaTypeMap of function names to { input, output }. Import it from this package; @microsoft/rayfin-client no longer re-exports it. Changed in 2.0
TypedFunctionClients<TSchema>TypeMap from schema keys to typed FunctionClient instances.

functionsService

New in 2.0

function functionsService<TSchema extends FunctionsSchema>(): ClientServiceFactory<
  TypedFunctionClients<TSchema>
>;

Register the factory under services when you create the client. The key you choose becomes the namespace on the client; use functions:

src/services/rayfinClient.ts
import { RayfinClient } 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';

export const rayfinClient = RayfinClient.create({
  baseUrl: import.meta.env.VITE_RAYFIN_API_URL,
  publishableKey: import.meta.env.VITE_RAYFIN_PUBLISHABLE_KEY,
  auth: browserSession({ storage: true }),
  services: {
    data: dataService<AppSchema>(),
    functions: functionsService<AppFunctionsSchema>(),
  },
});

export type AppClient = typeof rayfinClient;

The service uses the client's shared, authenticated transport and delegates to createFunctionsApi. Set functionsBaseUrl on RayfinClient.create to change function routing only; data, connectors, and storage keep using baseUrl. It replaces 1.x's new RayfinClient<AppSchema, AppFunctionsSchema>(...); see Migrate from 1.x.

FunctionsSchema

type FunctionsSchema = Record<string, { input: any; output: any }>;

Generated projects export an AppFunctionsSchema from rayfin/functions/src/types.ts:

rayfin/functions/src/types.ts
export type AppFunctionsSchema = {
  greet: {
    input: { firstName: string; lastName: string };
    output: string;
  };
  getEntries: {
    input: Record<string, never>;
    output: { id: string; message: string }[];
  };
};

Pass that schema as the type argument of functionsService<AppFunctionsSchema>(). Schemas provide compile-time types only; they do not validate inputs or outputs at run time.

FunctionClient

class FunctionClient<TInput = any, TOutput = any> {
  constructor(apiClient: ApiClient, functionName: string);

  invoke(
    ...args: TInput extends void | Record<string, never>
      ? [params?: undefined, options?: InvokeOptions]
      : [params: TInput, options?: InvokeOptions]
  ): Promise<TOutput>;
}

invoke() returns the function output directly as Promise<TOutput>. It does not return the transport envelope.

Function shapeCall form
Input functioninvoke(params) or invoke(params, options)
No-input functioninvoke() or invoke(undefined, options)

A lone argument is always treated as function input, never as options.

InvokeOptions

interface InvokeOptions {
  headers?: Record<string, string>;
  timeoutMs?: number;
}

timeoutMs defaults to 250,000 ms and is capped at that same Fabric UDF host limit. Non-positive, non-finite, or missing values use the default.

Invocation route

When the client was created with functionsBaseUrl, FunctionClient posts to:

${functionsBaseUrl}/api/<functionName>

Otherwise it posts to the deployed Fabric route:

${baseUrl}/functions/<functionName>/invoke

Errors

FunctionClient.invoke() throws on failure. Catch FunctionsError before a generic error handler when you need function-specific messages or codes. Import NetworkError and SdkError by name from @microsoft/rayfin-client. Changed in 2.0

src/features/greet.ts
import { NetworkError } from '@microsoft/rayfin-client';
import { FunctionsError } from '@microsoft/rayfin-functions';
import { rayfinClient } from '../services/rayfinClient';

export async function logGreeting(): Promise<void> {
  try {
    const result = await rayfinClient.functions.greet.invoke({
      firstName: 'Ada',
      lastName: 'Lovelace',
    });
    console.log(result);
  } catch (error) {
    if (error instanceof FunctionsError) {
      console.error(error.code, error.message);
    } else if (error instanceof NetworkError) {
      console.error('Network failure while invoking the function.');
    } else {
      throw error;
    }
  }
}
FailureError
Backend response contains errors or non-success statusFunctionsError with code FUNCTION_EXECUTION_ERROR.
Unexpected invocation failureFunctionsError with code UNKNOWN_FUNCTION_ERROR.
Network failureNetworkError, propagated unchanged.
Other SDK failureSdkError, propagated unchanged.

The backend invocationId is logged with console.debug as [rayfin-functions] <name> invocationId=<id>.

Full exported surface

export { functionsService } from './functionsService.js';
export { FunctionsError, createFunctionsApi, FunctionClient } from './Functions.js';
export type {
  FunctionInvocationResponse,
  InvokeOptions,
  TypedFunctionClients,
} from './Functions.js';
export type { FunctionsSchema } from './FunctionsSchema.js';

FunctionInvocationResponse

interface FunctionInvocationResponse<TOutput = any> {
  functionName: string;
  invocationId: string;
  status: string;
  output: TOutput;
  errors: Array<string | Record<string, any>>;
}

This is the wire envelope. invoke() unwraps and returns output, auto-parsing JSON-encoded output strings and inner { output: <value> } envelopes when present.

PromptRegister typed functions on my Rayfin 2.0 client
In my Rayfin 2.0 app, make `client.functions` typed by the generated functions schema. Add @microsoft/rayfin-functions to the frontend package.json at the same exact version as @microsoft/rayfin-client. In the existing `RayfinClient.create({...})` call, keep the auth adapter and every other service, and add `functions: functionsService<AppFunctionsSchema>()` to `services`, importing `functionsService` from @microsoft/rayfin-functions and `AppFunctionsSchema` from rayfin/functions/src/types. Replace any import of `FunctionsSchema` from @microsoft/rayfin-client with an import from @microsoft/rayfin-functions. Do not set `functionsBaseUrl` unless the app already uses the local `/.rayfin/api` route. Run the type check and fix what it reports.
Something wrong on this page?Report an issueEdit this page

On this page