Rayfin

Calling functions from your app

Invoke Rayfin functions from the frontend — register functionsService on RayfinClient, call typed invoke(), pass per-call options, handle errors, and route local Vite calls.

Changed in 1.36

Call functions from browser code through client.functions.<name>.invoke(...). The generated AppFunctionsSchema gives each function a typed input and output.

Note

Functions are generally available in Rayfin 1.36 and are not available in every Fabric region or tenant.

Register the functions service

Changed in 2.0

Install @microsoft/rayfin-functions in the frontend package at the same version as your other Rayfin packages. The 2.0 client no longer depends on it, so your package.json must declare it:

npm install @microsoft/rayfin-functions@2.0.0

Register functionsService<AppFunctionsSchema>() in services when you create the client, passing the generated AppFunctionsSchema as its type argument:

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;
  • Only names in AppFunctionsSchema are available on client.functions. Adjust the import path to where your frontend sits relative to rayfin/functions/src/types.ts.
  • client.functions exists only when you register the service. Keep the rest of your existing RayfinClient.create call — its auth adapter and other services — unchanged.
  • The bundled templates resolve deployment settings with resolveRayfinConfig() before they create the client; see Create a client.
  • In 1.x you passed the schema as the second type parameter of new RayfinClient<AppSchema, AppFunctionsSchema>(...). 2.0 removes that constructor, and @microsoft/rayfin-client no longer re-exports the FunctionsSchema type: import it from @microsoft/rayfin-functions. See Migrate from 1.x.

Invoke a function

src/features/greet.ts
import { rayfinClient } from '../services/rayfinClient';

export async function greet(): Promise<string> {
  return await rayfinClient.functions.greet.invoke({
    firstName: 'Ada',
    lastName: 'Lovelace',
  });
}

For a no-input function, call invoke():

const entries = await rayfinClient.functions.getEntries.invoke();

invoke() resolves to the function output directly, not to a transport envelope.

Pass per-call options in the second argument

InvokeOptions supports headers and a per-call timeout. Options always occupy the second argument slot.

await rayfinClient.functions.longRunning.invoke(
  { input: 'value' },
  { timeoutMs: 5_000, headers: { 'x-request-id': crypto.randomUUID() } },
);

await rayfinClient.functions.ping.invoke(undefined, { timeoutMs: 5_000 });

timeoutMs is capped at 250 seconds, the Fabric UDF host invocation ceiling.

Handle failures

Catch FunctionsError from @microsoft/rayfin-functions before generic errors. Import NetworkError by name from @microsoft/rayfin-client; in 1.x it came from @microsoft/rayfin-lib. 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 safeGreet(): Promise<string> {
  try {
    return await rayfinClient.functions.greet.invoke({
      firstName: 'Ada',
      lastName: 'Lovelace',
    });
  } catch (error) {
    if (error instanceof FunctionsError) {
      return `Function failed: ${error.message}`;
    }
    if (error instanceof NetworkError) {
      return 'Network error while calling the function.';
    }
    throw error;
  }
}
  • A non-success status or non-empty errors array throws FunctionsError with code FUNCTION_EXECUTION_ERROR.
  • Unexpected invocation failures are wrapped with code UNKNOWN_FUNCTION_ERROR.
  • The server-side invocationId is logged with console.debug for correlation.

Route local Vite calls through the adapter

The bundled Vite templates register the @microsoft/rayfin-local-dev adapter. While vite serve runs, it handles the SDK's default same-origin route, /functions/<name>/invoke:

SituationWhere the call goes
npx rayfin dev or npx rayfin dev functions apply started a local Functions hostThe local Functions host.
No local Functions host is configuredThe configured Fabric backend, so the deployed function code runs. Changed in 2.0
A local Functions host is configured but not reachableNowhere — the adapter returns HTTP 502 and never falls back to deployed code.

For an older or custom Vite app, install and register the adapter:

npm install @microsoft/rayfin-local-dev@2.0.0
vite.config.ts
import { rayfinLocalDev } from '@microsoft/rayfin-local-dev/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [rayfinLocalDev()],
});

Leave functionsBaseUrl unset so local and deployed calls use the same route. The proxy is reachable wherever the Vite dev server is, so keep Vite bound to loopback or restrict its host and allowedHosts settings to a trusted development network.

Use an explicit Functions base URL

functionsBaseUrl remains a client option, and it changes only function routing: when it is set, the SDK calls ${functionsBaseUrl}/api/<name> instead of ${baseUrl}/functions/<name>/invoke. Existing code that sets it from resolveRayfinFunctionsBaseUrl() keeps working:

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 { resolveRayfinFunctionsBaseUrl } from '@microsoft/rayfin-local-dev';
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,
  functionsBaseUrl: resolveRayfinFunctionsBaseUrl(),
  auth: browserSession({ storage: true }),
  services: {
    data: dataService<AppSchema>(),
    functions: functionsService<AppFunctionsSchema>(),
  },
});

export type AppClient = typeof rayfinClient;

resolveRayfinFunctionsBaseUrl() returns the adapter's same-origin /.rayfin base only while the Vite adapter has a local Functions host to route to; the adapter then forwards /.rayfin/api/<name> to that host and returns HTTP 502 if it is down. Otherwise the helper returns undefined, so production builds call the deployed Fabric backend.

For frameworks other than Vite, pass the framework's Functions URL variable as functionsBaseUrl only during development and leave it undefined in production — for example, NEXT_PUBLIC_RAYFIN_FUNCTIONS_URL behind an explicit development check in Next.js.

Do not call a function from the frontend until it is deployed or the local Functions host is running. A generated type proves the function was discovered at build time; it does not prove the deployed or local runtime currently has that handler loaded.

PromptCall a Rayfin function from the frontend
In my Rayfin 2.0 app, wire the frontend to call an existing function. 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 other services and add `functions: functionsService<AppFunctionsSchema>()` to `services`, importing `functionsService` from @microsoft/rayfin-functions and `AppFunctionsSchema` from rayfin/functions/src/types. Call `client.functions.<name>.invoke(...)` with the generated input type. Put per-call headers or `timeoutMs` in the second argument; for a no-input function use `invoke(undefined, options)` when passing options. Catch `FunctionsError` from @microsoft/rayfin-functions before generic errors, and import `NetworkError` from @microsoft/rayfin-client. If this is a Vite app without local function routing, add @microsoft/rayfin-local-dev and register `rayfinLocalDev()` from @microsoft/rayfin-local-dev/vite in vite.config.ts. Leave `functionsBaseUrl` unset unless existing code needs the `/.rayfin/api` route; in that case set it with `resolveRayfinFunctionsBaseUrl()`.
Something wrong on this page?Report an issueEdit this page

On this page