Call FileMaker scripts from an app
A Web Viewer app interacts with FileMaker using two distinct pathways: direct CRUD operations through the FileMaker Data API (see Use a typed data client) or custom FileMaker scripts. Use FileMaker scripts for operations that require transactional integrity or backend business logic. For guidance on selecting the appropriate method, see Splitting app logic.
Calling scripts from the Web Viewer
The @adk/fmdapi library exports two primary functions for executing FileMaker scripts:
-
fmFetch(scriptName, data): Executes a script asynchronously and returns a JavaScript Promise resolving with the script result. Use this when the application expects data back from FileMaker. -
callFMScript(scriptName, data, option?): Dispatches a script execution call without waiting for a return value. Use this for fire-and-forget actions or navigation, specifying an optionalFMScriptOptionparameter when needed.
import { fmFetch } from "@adk/fmdapi";
type DashboardResult = {
openInvoices: number;
overdueInvoices: number;
};
const dashboard = await fmFetch<DashboardResult>("Get Dashboard", {
accountId: "123",
});
import { FMScriptOption, callFMScript } from "@adk/fmdapi";
callFMScript(
"Navigate Away From Web Viewer",
{ layout: "Contacts" },
FMScriptOption.RESUME,
);
fmFetch passes the data argument directly as the script parameter without an outer envelope:
{ "accountId": "123" }
In FileMaker Pro, read parameters from the root level of Get ( ScriptParameter ) rather than looking under a nested data key:
Set Variable [ $param ; Get ( ScriptParameter ) ]
Set Variable [ $account ; JSONGetElement ( $param ; "accountId" ) ]
The script returns results to the Web Viewer using the Exit Script step. fmFetch evaluates the call through window.FileMaker.PerformScriptAsync, parsing JSON return strings automatically.
For complete API signatures, supported script options, and error-handling mechanics, see Script calls.
Script requirements
Scripts executed through fmFetch must adhere to several design practices:
-
Read parameters at root level: Parse parameters directly from
Get ( ScriptParameter ). -
Return a result on all execution paths: Ensure that every logical branch, including error-capture and validation paths, concludes with an
Exit Scriptstep. Halting a script prematurely leaves the client Promise pending indefinitely. -
Validate sub-actions before confirming success: If the script invokes Perform Script on Server or subscripts, verify execution results and
Get ( LastError )before returning a success payload. -
Maintain structured return schemas: Return structured JSON payloads (such as
{ ok: true, message: "" }) that the client application can validate at runtime using Zod or a comparable validator. -
Do not alter the Web Viewer layout context: Scripts execute within the window hosting the Web Viewer. To prevent interrupting the web client, isolate record modifications inside a hidden off-screen window or run tasks using Perform Script on Server.
import { fmFetch } from "@adk/fmdapi";
import { z } from "zod/v4";
const saveResultSchema = z.object({
ok: z.boolean(),
message: z.string().optional(),
});
const result = saveResultSchema.parse(
await fmFetch("Save Contact Preferences", { contactId: "123" }),
);
Recommended script structure
Structure FileMaker scripts called by fmFetch using the following standard template:
Set Variable [ $param ; Get ( ScriptParameter ) ]
# Parse root-level parameters
Set Variable [ $accountId ; JSONGetElement ( $param ; "accountId" ) ]
# Execute business logic in an off-screen window if context changes are required
# ...
Set Variable [ $result ; JSONSetElement ( "{}" ; "ok" ; True ; JSONBoolean ) ]
Exit Script [ Text Result: $result ]