Skip to Content

Custom widgets

Build React or plain TypeScript widgets that run inside Foundry UIs such as Workshop, exchange parameters and events with the host, and call the OSDK.

A custom widget is a web page you build with Vite that Foundry embeds inside a host UI such as Workshop. The host passes values into the widget as parameters, the widget sends events back to update them, and the widget can read and write your Ontology through the OSDK.

How it works

Widgets ship in a widget set, a Foundry resource with a RID such as ri.widgetregistry..widget-set.<id>. One project builds one widget set, and each HTML entrypoint in its Vite config becomes one widget in the set. You deploy the set as numbered versions, and the host loads the scripts and stylesheets that the version's manifest lists.

Each widget declares its contract in a config file built with defineConfig: an ID and name, the parameters the host can set, and the events the widget can emit. At build time, @osdk/widget.vite-plugin turns those configs into the manifest at .palantir/widgets.config.json, which tells Foundry what to show in the widget's setup panel. See Parameters and events.

At runtime the host injects window.__PALANTIR_WIDGET_API__ into the widget's page, and the two sides exchange typed messages through it:

MessageDirectionPayloadWhen
widget.readyWidget to host{ apiVersion: "1.0.0" }The widget is listening and wants its first parameter values.
host.update-parametersHost to widget{ parameters }, one async value per parameter IDParameter values or their loading states change.
widget.emit-eventWidget to host{ eventId, parameterUpdates }The widget fires one of its declared events.
widget.resizeWidget to host{ width, height } of the document body's border boxThe widget's content changes size.
widget.reloadWidget to host{}The widget needs a full reload, such as a Vite full-page reload in development.
host.set-hotkeysHost to widget{ hotkeys }The host tells the widget which key combinations to forward.
widget.hotkeyWidget to host{ hotkey }The user pressed one of those key combinations inside the widget.
host.set-themeHost to widget{ theme }The host's theme changes.

FoundryWidget from @osdk/widget.client-react sends widget.ready, widget.resize, and widget.reload for you and exposes the rest through a hook. See React widgets.

Packages

PackageUse it for
@osdk/widget.apiThe contract itself: message, manifest, and parameter value types, BrowserPermission, and MANIFEST_FILE_LOCATION. @osdk/widget.client re-exports the parts you need, so you rarely import it directly.
@osdk/widget.clientdefineConfig for widget configs, createFoundryWidgetClient for widgets without React, and createFoundryWidgetTokenProvider for OSDK clients inside a widget.
@osdk/widget.client-reactThe FoundryWidget provider and the useFoundryWidgetContext hook. Supports React 18 and 19.
@osdk/widget.vite-pluginGenerates the manifest during vite build and runs developer mode during vite. Supports Vite 6 and 7.
@osdk/create-widgetA CLI that scaffolds a React widget set project with all of the above wired in.
@osdk/cliosdk widgetset deploy and osdk widgetset version for publishing and managing versions. See Build and publish.

Create a widget set project

Before you start, you need:

  1. A widget set in Foundry and its RID, ri.widgetregistry..widget-set.<id>.
  2. A Foundry token in the FOUNDRY_TOKEN environment variable. Developer mode, osdk widgetset deploy, and installing your SDK package from Foundry's npm registry all read it.
  3. To call the Ontology, the name of your widget set's SDK package, such as @my-app/sdk, and the URL of the npm registry that hosts it. The registry URL has the form https://<stack>/artifacts/api/repositories/ri.artifacts.main.repository.<id>/contents/release/npm.

Run the CLI

npx @osdk/create-widget@latest my-osdk-widget

npm create @osdk/widget@latest runs the same CLI. It prompts for each value you do not pass as a flag:

PromptFlagAccepted values
Project namepositional [project]Letters, digits, -, and _. Defaults to my-osdk-widget.
Overwrite the existing directory?--overwriteAsked only if the directory exists. --overwrite removes its files; --no-overwrite keeps them and writes over matching paths.
Framework--templatewidget-react, listed as OSDK React: React, Vite, and Blueprint.
OSDK version--sdkVersion2.x, the only version the React template supports. Not prompted unless the value is invalid.
Foundry URL--foundryUrlMust start with https://, such as https://example.palantirfoundry.com.
Will you be using an OSDK?--skipOsdkPassing --osdkPackage or --osdkRegistryUrl answers yes. --skipOsdk answers no and conflicts with both.
OSDK package name--osdkPackageMust match @<scope>/sdk, such as @my-app/sdk.
npm registry URL--osdkRegistryUrlThe Foundry Artifacts URL ending in /contents/release/npm.
Widget set RID--widgetSetri.widgetregistry..widget-set.<id>.
None--repositoryOptional code repository RID, written to foundry.config.json for osdk widgetset deploy.

To skip every prompt:

npx @osdk/create-widget@latest my-osdk-widget \
  --template widget-react \
  --foundryUrl https://example.palantirfoundry.com \
  --widgetSet ri.widgetregistry..widget-set.<id> \
  --osdkPackage @my-app/sdk \
  --osdkRegistryUrl https://example.palantirfoundry.com/artifacts/api/repositories/ri.artifacts.main.repository.<id>/contents/release/npm

Install dependencies

cd my-osdk-widget
export FOUNDRY_TOKEN=<token>
npm install

The generated .npmrc sends FOUNDRY_TOKEN to your Foundry npm registry, so a 401 or 404 for your SDK package means the token is missing, expired, or lacks access.

Start developer mode

npm run dev

The terminal prints a setup URL. Open it to load your local widgets in Foundry, as described in Run locally.

Project layout

.gitignore
.npmrc
README.md
eslint.config.mjs
foundry.config.json
index.html
package.json
tsconfig.json
tsconfig.node.json
vite.config.ts
client.ts
main.config.ts
main.css
main.tsx
useDarkTheme.ts
vite-env.d.ts
Widget.module.css
Widget.tsx
FilePurpose
foundry.config.jsonThe Foundry URL and widget set settings that the Vite plugin and osdk widgetset deploy read. See Build and publish.
.npmrcPoints your SDK package's scope at your Foundry registry and authenticates with ${FOUNDRY_TOKEN}. Only generated with an OSDK.
vite.config.tsAdds @vitejs/plugin-react and @osdk/widget.vite-plugin, serves on port 8080 with CORS enabled, and lists ./index.html as the build input.
index.htmlThe widget's entrypoint. It loads /src/main.tsx.
src/main.config.tsThe widget config: ID widgetId, a greetingName string parameter, a counterValue number parameter, and a setCounterValue event.
src/main.tsxRenders Widget inside FoundryWidget, and inside OsdkProvider when you use an OSDK. Importing main.config.ts here is what lets the Vite plugin find it.
src/client.tsCreates the OSDK client. Only generated with an OSDK.
src/Widget.tsxReads both parameters, increments the counter every second by emitting setCounterValue, and renders a reset button.
src/useDarkTheme.tsTracks prefers-color-scheme: dark so the widget can apply Blueprint's dark theme.

Without an OSDK, the CLI omits .npmrc and src/client.ts, and package.json drops @osdk/client, @osdk/react, and the SDK package.

The package.json scripts:

ScriptCommandDescription
devviteStarts the dev server and developer mode.
buildtsc && vite buildTypechecks, builds to dist/, and writes dist/.palantir/widgets.config.json.
linteslint . --report-unused-disable-directives --max-warnings 0Lints the project.
testvitest run --passWithNoTestsRuns tests.
previewvite previewServes the production build locally.

Run locally

Developer mode loads widget code from your dev server into Foundry, so you can preview changes against real host data without deploying a version.

Start the dev server

Export FOUNDRY_TOKEN and run npm run dev. The plugin stops with FOUNDRY_TOKEN environment variable not found if the variable is missing. Instead of the usual local URL, Vite prints:

➜  Click to enter developer mode for your widget set: http://localhost:8080/.palantir/setup/

Open the setup URL

The setup page loads each HTML entrypoint so Vite parses its widget config, then builds a development manifest whose scripts point at your dev server. It sends that manifest for the widget set in foundry.config.json to Foundry, enables developer mode, and redirects you to the widget set's preview page at <foundryUrl>/workspace/custom-widgets/preview/<widgetSetRid>.

If a widget's config is never found, the page gives up after 10 attempts with Timed out waiting for widget config files to be parsed. Are you sure a widget config is imported?.

Edit your code

Component changes reach the embedded widget through Vite hot module replacement. A change to a widget config file does not: the terminal warns that the config changed, and you open the setup URL again to send the new parameters and events to Foundry.

You do not need to publish a widget set before developing on it. To place the widget in a Workshop module, publish a version once so the widget appears in Workshop's Widget setup panel. See Build and publish.

Code Workspaces

In Foundry Code Workspaces, run Vite in the code-workspaces mode instead:

package.json
{
  "scripts": {
    "dev:remote": "vite --mode code-workspaces"
  }
}

In this mode the plugin reads the Foundry URL from FOUNDRY_PROXY_URL, the token from the file named by FOUNDRY_PROXY_TOKEN, and the dev server address from DEV_SERVER_DOMAIN and DEV_SERVER_BASE_PATH, all set by the workspace. Open the preview panel to enter developer mode, and refresh it after you change a widget config. The plugin warns when you run npm run dev inside a workspace, or the code-workspaces mode outside one.

Use the OSDK in a widget

Inside Foundry, a widget authenticates through the host instead of OAuth. Create the client against the page origin with the widget token provider:

src/client.ts
import { $ontologyRid } from "@my-app/sdk";
import { createClient } from "@osdk/client";
import { createFoundryWidgetTokenProvider } from "@osdk/widget.client";

export const client = createClient(
  window.location.origin,
  $ontologyRid,
  createFoundryWidgetTokenProvider(),
);

createFoundryWidgetTokenProvider resolves to a placeholder that Foundry replaces with real credentials on each request. Pass the client to FoundryWidget, which needs it to resolve object set parameters, and to OsdkProvider so @osdk/react hooks work inside the widget:

src/main.tsx
import { OsdkProvider } from "@osdk/react";
import { FoundryWidget } from "@osdk/widget.client-react";
import { createRoot } from "react-dom/client";
import { client } from "./client.js";
import MainConfig from "./main.config.js";
import { Widget } from "./Widget.js";

createRoot(document.getElementById("root")!).render(
  <FoundryWidget config={MainConfig} client={client}>
    <OsdkProvider client={client}>
      <Widget />
    </OsdkProvider>
  </FoundryWidget>,
);

From there, query with the hooks in Query objects or read object sets the host passes in, as shown in React widgets. At build time, the Vite plugin also lists every package reachable through dependencies (not devDependencies) whose package.json has an osdk.packageRid field, with its version, in the manifest's inputSpec.

Build a widget without React

createFoundryWidgetClient from @osdk/widget.client speaks the same protocol without a framework. Outside a Foundry host or developer mode, it throws Invariant failed: [FoundryWidgetClient] Missing __PALANTIR_WIDGET_API__ in window (production builds show only Invariant failed).

src/main.ts
import { createFoundryWidgetClient } from "@osdk/widget.client";
import MainConfig from "./main.config.js";

const widget = createFoundryWidgetClient<typeof MainConfig>();

widget.hostEventTarget.addEventListener("host.update-parameters", (event) => {
  const greeting = event.detail.parameters.greetingName?.value;
  if (greeting?.type === "loaded" || greeting?.type === "reloading") {
    document.body.textContent = `Hello, ${greeting.value ?? "World"}!`;
  }
});

document.body.addEventListener("click", () => {
  widget.emitEvent("setCounterValue", {
    parameterUpdates: { counterValue: 0 },
  });
});

new ResizeObserver(() => {
  const { width, height } = document.body.getBoundingClientRect();
  widget.resize({ width, height });
}).observe(document.body);

widget.subscribe();
widget.ready();
MemberDescription
subscribe()Starts forwarding host messages to hostEventTarget. Call it before ready().
unsubscribe()Stops forwarding host messages.
ready()Sends widget.ready so the host sends the first parameter values.
emitEvent(eventId, { parameterUpdates })Sends widget.emit-event. Typed against the events in your config.
resize({ width, height })Sends widget.resize.
reload()Sends widget.reload.
hotkey(payload)Sends widget.hotkey.
sendMessage(message)Sends any widget message. Prefer the methods above.
hostEventTargetAn EventTarget that dispatches host.update-parameters, host.set-hotkeys, and host.set-theme, with the message payload in event.detail.

Next steps

On this page