Rayfin

Functions

Run server-side TypeScript in Rayfin with typed frontend calls, typed secrets, app-identity connections, and raw SQL for queries the data client cannot express.

Changed in 1.36

Rayfin functions are server-side TypeScript user-defined functions (UDFs) authored with @microsoft/fabric-user-data-functions, run in the Fabric runtime, and invoked from your frontend through RayfinClient. Use them for trusted backend logic, secrets, and external resource calls that must not run in the browser.

Note

Functions are generally available in Rayfin 1.36 and no longer use a RAYFIN_FEATURE_FLAGS=functions gate. They are not available in every Fabric region or tenant; confirm deployment in the target workspace before you depend on them.

Use a function for trusted server work

Use a function when code needs a server boundary:

  • Secrets and API keys — read values with ctx.Secrets, never from client code.
  • Business logic users must not edit — enforce rules that a browser client could bypass.
  • Server-side validation — validate before writing data or calling external services.
  • Aggregation and transformation — combine data before returning it to the frontend.
  • External resources — call Fabric SQL, OneLake, Fabric REST, Azure AI Foundry, Azure DevOps, or your own Entra-protected API with server-side tokens.
  • Bulk imports — parse and validate pasted or uploaded CSV on the server. See Bulk import from CSV.
  • Reports the data client cannot express — run parameterized T-SQL against the app database. See Raw SQL in functions. Experimental
  • Custom sign-in claims — compute app-defined claims when a user signs in. See Dynamic auth claims. New in 2.0

For normal reads and writes against your Rayfin entities, call client.data.<Entity> from the frontend instead of adding a function hop.

Enable and scaffold functions

Run functions init from an existing Rayfin project:

npx rayfin functions init

The command creates the functions package, enables the service in rayfin/rayfin.yml, installs dependencies, builds, and generates the initial types.ts schema.

rayfin/rayfin.yml
services:
  functions:
    enabled: true
    path: rayfin/functions
    auth:
      type: application
    buildCommand: npm run build

auth.type: application is required when Functions are enabled. New 1.36 scaffolds write it automatically; existing apps must add it explicitly and run a full npx rayfin up to apply the remote setting. Standalone commands such as npx rayfin up functions deploy upload code but do not migrate the remote auth mode.

Match the worker package to the CLI

Changed in 2.0

Your functions compile against @microsoft/fabric-user-data-functions, the worker package that also hosts them. rayfin functions init pins it to the exact version of the CLI that ran, and it must stay on the CLI's version: the Rayfin 2.0 CLI writes runtime metadata schema 3.0, which earlier workers cannot read.

Before rayfin dev, rayfin dev functions apply, rayfin functions init, or rayfin up generates types or builds a deployment, the CLI checks the installed worker. An incompatible worker stops the command with the install command to run:

  • The installed @microsoft/fabric-user-data-functions@1.36.2 only reads runtime metadata schema '2.0' and compatible minors, but this CLI writes '3.0'. Deploying would make function invocations fail because the worker rejects the runtime metadata.
    ↳ Upgrade @microsoft/fabric-user-data-functions by running `npm install @microsoft/fabric-user-data-functions@2.0.0` in <functions-path>

Run that command in the functions package — services.functions.path, rayfin/functions by default — then retry:

cd rayfin/functions
npm install @microsoft/fabric-user-data-functions@2.0.0

A plain npm install keeps the version already pinned in the functions package.json, and rerunning rayfin functions init without --force preserves it, so neither upgrades the worker.

Understand the two identities

Deployed functions use two authentication paths:

Access pathAPIIdentity used
Rayfin datactx.getDataClient()The signed-in caller's Rayfin token and database permissions.
External resourcesctx.Tokens.<Audience>, or ctx.Tokens['<custom audience>'] for your own APIThe Fabric app's application identity. For current Fabric apps, this is the owner of the Fabric app item.

Grant external resources to the app identity. Declaring an audience or deploying the function does not grant SQL, Storage, Fabric REST, Azure AI, Azure DevOps, or custom API permissions.

Two more server-side capabilities follow from these paths:

Project structure

rayfin/
  data/
  functions/
    src/
      function_app.ts
      types.ts
      secrets.generated.ts
    host.json
    local.settings.json
    package.json
    tsconfig.json
  • function_app.ts registers handlers with udf.func(...).
  • types.ts is generated for frontend calls; never hand-edit it.
  • secrets.generated.ts is generated from the top-level secrets: list in rayfin.yml; never hand-edit it.
  • local.settings.json is local-only and can contain local development secrets.

Development and deployment loop

Prefer npx rayfin dev during development. It provisions or reuses the Fabric backend, emits frontend environment files, starts the frontend, and starts the local Functions host when services.functions.enabled is true.

Use these focused commands when needed:

TaskCommand
Scaffold or refresh the functions packagenpx rayfin functions init
Run only the local Functions hostnpx rayfin dev functions apply
Deploy everything, including functionsnpx rayfin up
Deploy only function codenpx rayfin up functions deploy
Skip function deployment in a full deploynpx rayfin up --exclude-services functions

In this section

PromptAdd a Rayfin function for server-side logic
In my Rayfin project, add a server-side function for this task: <describe the trusted backend logic>. Use Rayfin 2.0. If rayfin/functions/ does not exist, run `npx rayfin functions init`. Ensure rayfin/rayfin.yml has services.functions.enabled: true and services.functions.auth.type: application, and that the functions package depends on @microsoft/fabric-user-data-functions at the exact version of the Rayfin CLI. Register the handler in rayfin/functions/src/function_app.ts with `udf.func(name, handler, [])`. Use `RayfinContext<AppSchema>` for typed data access, `ctx.user.id` to stamp owner fields, `ctx.Secrets.<NAME>` for declared secrets, and `RayfinContext<AppSchema, AudienceType.X>` plus `ctx.Tokens.X` for external resources. Do not expose secrets or tokens to the frontend. Run the smallest build or typegen command that validates the change, then show how the frontend calls it through `client.functions.<name>.invoke(...)` on a client that registers `functionsService<AppFunctionsSchema>()`.
Something wrong on this page?Report an issueEdit this page

On this page