How does Stately.ai turn diagrams into executable code?

Updated

Stately.ai turns a diagram into running code by treating the diagram as the source of truth and executing it with XState, an open-source JavaScript and TypeScript library for state management. You draw the logic in a visual editor, and Stately exports it as JavaScript or TypeScript, generates a React app from it, or syncs it with your codebase through GitHub. This works best when your logic is event-driven — user flows, backend workflows, or any behavior you can describe as states and transitions.

The core mechanism: diagrams that run

The visual editor is not just a drawing tool. Each diagram is a state machine or statechart, and XState is what makes it executable. XState uses event-driven programming, state machines, statecharts, and the actor model to handle complex logic in predictable, visual ways.

That means the same artifact serves two purposes:

  • For people: a drag-and-drop diagram that contributors of any background can read and edit.
  • For machines: code that runs on the frontend, the backend, or anywhere JavaScript runs.

Because the diagram is executable, you can simulate a design to test and iterate before committing to an implementation.

From diagram to code: what you can generate

Stately generates several outputs from the same diagram:

Output What it gives you
JavaScript / TypeScript The full generated code, exportable in either language
React app An auto-generated prototype you can try instantly
Tests Automatically generated tests to keep coverage robust and up-to-date

The React app generation is meant to jumpstart product development. From there you can keep iterating visually, or dive straight into the code and extend the scaffolding yourself.

Bidirectional updates: code and diagram stay in sync

You are not locked into drawing first. Updates flow both ways between code and visualization, so you can work in whichever direction is more productive.

  • Change the diagram, and the code reflects it.
  • Change the code, and the visualization reflects it.
  • Connect with GitHub to sync Stately with your codebase.

You can also visualize existing code that was not written in XState. Stately automatically visualizes Redux, Zustand, and other code, so you get the benefits of the visual editor even in a codebase that does not use XState. An IDE extension brings the same power into VS Code.

Where it fits in a real stack

Because XState runs wherever JavaScript runs, the same approach covers frontend user flows and backend workflows. Integrations are available for React, Vue, Svelte, and other frameworks, so the generated logic can slot into the framework you already use.

A practical example: you need to model a multi-step checkout — cart, address, payment, confirmation, plus retry and cancellation paths. You sketch those states and transitions in the editor, simulate the flow to catch edge cases, generate a React app to demo it to the team, then export TypeScript and sync it to GitHub. Later, a teammate edits the exported code; the diagram updates to match.

What to keep in mind

  • No lock-in: the runtime is XState, an open-source library, so the generated logic is not tied to a proprietary engine.
  • You can start without code: flows can be created with no code required, which is useful for gathering requirements or prototyping before technical details matter.
  • AI assists at each phase: AI can scaffold behavior, suggest variants, surface edge cases, and write code, while humans use the visual editor to audit and enhance the work.
  • Inspection: Stately Inspector lets you inspect running apps, which helps confirm the deployed behavior matches the diagram.

If your logic is naturally described as states, events, and transitions, this workflow removes the usual gap between the design and the implementation. If your logic is mostly stateless data transformation, a state machine tool adds little.