Windmill Client SDK
The Windmill Client is the official JavaScript/TypeScript SDK for interacting with the Windmill API, designed for both browser and Node.js environments. It simplifies API calls, variable management, and task execution within the Windmill ecosystem. Currently at version 1.688.0, the package follows a rapid release cycle, suggesting frequent updates and feature additions. Its primary purpose is to provide a robust, type-safe interface for integrating Windmill functionalities into applications, abstracting away direct HTTP requests and handling authentication and data serialization. It's a key tool for developers building applications that leverage Windmill for backend tasks, workflows, or serverless functions by providing a consistent client experience across different JavaScript runtimes.
Common errors
-
Error: Request failed with status code 401 (Unauthorized)
cause The provided API token is missing, invalid, or expired.fixVerify that 'apiToken' is correctly set during client initialization and that the token has the necessary permissions and is still active in your Windmill account. Check for typos or leading/trailing spaces. -
Error: Request failed with status code 404 (Not Found)
cause The requested resource (e.g., variable, script path) does not exist or the client lacks permission to access it.fixDouble-check the exact path to the Windmill resource (e.g., 'u/foo/my_variable'). Ensure the API token has read/execute permissions for that specific resource in your Windmill instance. -
ReferenceError: fetch is not defined
cause Running in an older Node.js environment where the native Fetch API is not globally available, and 'undici' is not being correctly picked up or polyfilled.fixUpgrade your Node.js environment to version 18 or newer. If an upgrade is not possible, ensure your build process or runtime environment explicitly makes a global 'fetch' API available, although the SDK's 'undici' dependency should handle this automatically in most modern Node setups.
Warnings
- breaking Major versions (e.g., v1 to v2) may introduce breaking changes to the API client's methods, parameter signatures, or class constructors to align with evolving Windmill API specifications. Always review the release notes when upgrading major versions.
- gotcha Hardcoding API tokens or exposing them in client-side browser code is a significant security risk. API tokens grant access to your Windmill resources.
- gotcha The SDK relies on the Fetch API. In older Node.js environments (< v18), the global 'fetch' might not be available, leading to runtime errors unless a polyfill or 'undici' is correctly configured or bundled.
Install
-
npm install windmill-client -
yarn add windmill-client -
pnpm add windmill-client
Imports
- *
const wmill = require('windmill-client');import * as wmill from 'windmill-client';
- getVariable
import { getVariable } from 'windmill-client'; - Windmill
import { Windmill } from 'windmill-client';
Quickstart
import { Windmill } from 'windmill-client';
async function runExample() {
// Initialize the client. For Node.js, ensure WINDMILL_API_TOKEN is set in environment variables.
// For browsers, an API token might be provided client-side or retrieved securely.
// Replace 'https://app.windmill.dev/api/' with your Windmill instance URL if self-hosting.
const client = new Windmill({
apiUrl: process.env.WINDMILL_API_URL || 'https://app.windmill.dev/api/',
// In a real application, retrieve the token securely (e.g., from environment variables,
// a secure store, or a browser cookie/local storage with appropriate security measures).
// This example assumes it's available as an environment variable for Node.js.
apiToken: process.env.WINDMILL_API_TOKEN ?? '' // Ensure token is provided securely
});
try {
// Retrieve a variable from Windmill
// Replace 'u/foo/my_variable' with the actual path to your variable
const myVariable = await client.getVariable('u/foo/my_variable');
console.log('Retrieved variable:', myVariable);
// Example: Run a script
// await client.runScript('u/bar/my_script', { payload: { data: 'test_data' } });
// console.log('Script executed successfully.');
} catch (error) {
console.error('Error interacting with Windmill:', error);
if (error instanceof Error) {
console.error('Error message:', error.message);
}
}
}
runExample();