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.
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 initThe command creates the functions package, enables the service in rayfin/rayfin.yml, installs dependencies, builds, and generates the initial types.ts schema.
services:
functions:
enabled: true
path: rayfin/functions
auth:
type: application
buildCommand: npm run buildauth.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
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.0A 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 path | API | Identity used |
|---|---|---|
| Rayfin data | ctx.getDataClient() | The signed-in caller's Rayfin token and database permissions. |
| External resources | ctx.Tokens.<Audience>, or ctx.Tokens['<custom audience>'] for your own API | The 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:
ctx.user.idis the signed-in caller's subject id, for stamping owner fields. See Writing a function. New in 2.0ctx.sqlruns raw SQL as the app, not as the caller, so entity permissions do not apply. See Raw SQL in functions. Experimental
Project structure
rayfin/
data/
functions/
src/
function_app.ts
types.ts
secrets.generated.ts
host.json
local.settings.json
package.json
tsconfig.jsonfunction_app.tsregisters handlers withudf.func(...).types.tsis generated for frontend calls; never hand-edit it.secrets.generated.tsis generated from the top-levelsecrets:list inrayfin.yml; never hand-edit it.local.settings.jsonis 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:
| Task | Command |
|---|---|
| Scaffold or refresh the functions package | npx rayfin functions init |
| Run only the local Functions host | npx rayfin dev functions apply |
| Deploy everything, including functions | npx rayfin up |
| Deploy only function code | npx rayfin up functions deploy |
| Skip function deployment in a full deploy | npx rayfin up --exclude-services functions |
In this section
- Writing a function — register handlers, use
RayfinContextandctx.user.id, and keep generated types current. - Secrets in functions — declare secrets with the CLI and read them through
ctx.Secrets. - Connections — use application-auth tokens for Fabric, Azure, and your own Entra-protected APIs.
- Raw SQL in functions — run parameterized T-SQL against the app database with
ctx.sql. New in 2.0 Experimental - Calling functions from your app — register
functionsServiceand invoke functions throughRayfinClient. - Bulk import from CSV — validate and import pasted CSV in a function, with per-row results.
- Run functions locally — start, route, debug, and troubleshoot the local host.
- Deploying functions — deploy functions with
rayfin uporrayfin up functions deploy.
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>()`.React integration
Add a Rayfin AuthProvider, useAuth hook, route guard, Fabric SSO startup, and sign-in button with error display to a React app.
Writing a function
Register Rayfin functions with udf.func, use RayfinContext for typed data access and the caller's identity, and keep generated frontend types in sync.