Skip to content

What is Bonsai ​

Bonsai is a small, safe, typed expression language for JavaScript applications. It evaluates text such as

bonsai
user.plan == "pro" && orders.filter(.paid).map(.total).sum() > 100 // => true

against data you pass in, without executing JavaScript. It is meant for logic that people other than your developers write: pricing and eligibility rules, saved filters, formula fields, notification templates, and feature conditions.

Throughout this site, // => value after an expression shows its result, and the host is your application: the data it passes in (the context) and the functions it adds (host functions).

Common tasks ​

TaskWhere
Save a rule written by a user, safelyA hardened setup
Show why a rule passed or failedReasons
Autocomplete and diagnostics in an editorEditor Support
Add Bonsai to a CodeMirror editorCodeMirror
Run a filter in SQL or MongoDBDatabase Filters
Add your own functionHost Functions
Handle errorsErrors
Set limits for untrusted inputLimits
Upgrade from 0.xMigrating from 0.x
Build feature flag targetingRecipe: Feature Flag Targeting
Let users filter a table in SQLRecipe: User-Defined Filters in SQL
Explain eligibility decisionsRecipe: Eligibility Rules with Reasons

What you get ​

  • Familiar syntax. JavaScript operators and names: a.b, ?., ??, template literals, filter, map, includes, toUpperCase. Every function also works as a method, so sum(xs) and xs.sum() are the same call.
  • Static checking. Declare the shape of your data once with t and get errors for typos, wrong types, and possibly-null values before an expression is saved, with suggestions and exact source ranges. The TypeScript types of the context and the result are inferred from the same declaration.
  • Safety by construction. No globals, no prototype access, no calling functions found in data, no conversion hooks, no mutation. Every evaluation terminates and is bounded by a step budget, size limits, an optional timeout, and an AbortSignal.
  • Speed. Expressions compile once to closures (no eval, safe under a strict Content Security Policy) and are cached per environment.
  • Editor support. A language service in bonsai-js/service provides completions, hover, and diagnostics without evaluating anything.
  • No dependencies. Runs in Node.js 22 and newer, current Bun, and modern ESM browsers.

What it is not ​

Bonsai is not a general-purpose language. There are no loops, no recursion, no assignment, and no user-defined functions beyond lambdas passed to built-ins. That is deliberate: it is what makes every expression terminate and keeps the checker precise. When an expression needs something the language does not have, you add a host function.

Where to go next ​

Released under the MIT License.