StateMachines is the client class of @usestatemachines/sdk. This page describes version 0.6. To move from 0.5, see Upgrade from SDK 0.5 to 0.6.
Constructor options
The SDK does not load
.env files. A missing key, or a key that contains anything other than printable ASCII, including a space, throws TypeError in the constructor. Invalid attemptTimeoutMs throws RangeError.
Workspace scope
An API key belongs to one workspace. AworkspaceId option does not grant it access to another workspace. Member access tokens need a workspace for workspace-scoped calls, supplied in the constructor or the individual input.
The constructor default applies to environments.create(), environments.list(), and snapshots.list(). It also applies to auditEvents.list() when there is no environmentId filter. An explicit input workspaceId takes precedence. Calls that identify a resource by ID use that resource’s workspace and still check authorization.
sm.me() returns the principal, the user when the caller is a member, the current organization, the organizations the caller belongs to, permissions, workspace, and limits. workspaceId and limits can be null. Limits include concurrentEnvironments and the default and maximum timeLimitMinutes.
sm.workspaces.list({ archived, limit, cursor }) reads workspace metadata. The archived filter is the string 'true' or 'false', with 'false' as the default. An API key sees its own workspace. sm.workspaces.get(workspaceId) reads one workspace. The public SDK does not create or archive workspaces.
Public operations
A snapshot is plain data and has no bound methods. Requests and audit events covers recorded bodies, time windows, and audit events.
List filters
Everylist() method and apps.versions() accept limit and cursor. Read every page of a list shows the cursor loop. The additional filters are:
Environment labels use
key=value strings. Every supplied label must match. A status array selects any listed status. Environment and snapshot lists are newest first. A page is a read at that time, not a frozen export of the full result set.
Operation options
wait defaults to true. With wait: false, creation and resume return after acceptance. timeoutMs bounds the subsequent wait, starting after the initial operation returns. It is not a deadline for the combined creation and waiting time. With wait: false, timeoutMs has no effect.
A create call without idempotencyKey generates one UUID and reuses it across automatic retries. Separate create calls generate separate keys. Operation recovery explains how to preserve one key across application retries.
signal cancels caller-side work. It does not roll back an accepted operation. Only environment create, pause, resume and delete, snapshot create, and the two wait() methods accept signal.
A wait’s timeoutMs defaults to 600000 (10 minutes). Polling starts at 1 second, multiplies the interval by 1.5 up to 5 seconds, and varies each interval at random by up to 20 percent. Each poll read times out after 30 seconds.
Environment helpers
Returned environments haveconnect(), pause(), resume(), delete(), refresh(), and snapshots.create() bound to their ID. The bound snapshot helper accepts { name? } and the same creation options as sm.snapshots.create().
Helpers are not enumerable. JSON.stringify(env) and object spread preserve API data, not methods. A parsed JSON environment must be read again through sm.environments.get(id) to obtain helpers.
An environment object is one resource read. Operations return new objects. They do not change a previously returned object’s status. refresh() reads the current state. Explicit waits are on sm.environments.wait(), not on the environment object.
Runtime and type support
The SDK is ESM only. Node.js support follows the package’sengines field. TypeScript projects need fetch types through the DOM library or Node.js types. Response timestamps are Date objects in the SDK. Direct HTTP responses use timestamp strings.
Public types and the error classes are exported from @usestatemachines/sdk. Response enums can contain values introduced after the installed SDK, so callers need a fallback for unfamiliar statuses. Polling keeps waiting on an unfamiliar status until its deadline.
The SDK belongs in trusted server processes. A management API key must never be included in browser JavaScript. Cloudflare Workers use an explicitly supplied key. See Use the SDK in Cloudflare Workers.