> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript quickstart

> Install the SDK, run a workflow with the bundled engine, read its typed result and verify its receipt.

Five minutes from zero to a typed workflow result inside your own app:
install the package, save one file, run it, read the outputs, verify the
record.

You need Node.js 22 or newer on macOS (arm64, x64) or Linux with glibc (x64,
arm64). No server, no global CLI and no provider key: this example runs on the
engine the package bundles, with the `mock/echo` model, which makes no model
call. On Alpine
(musl) or Windows there is no bundled engine: `new Nika()` throws
`NikaEngineUnavailable`, so point `bin` or `NIKA_BIN` at a compatible engine,
or connect to a [`nika serve`](https://docs.nika.sh/sdk/remote/serve-quickstart).

**1. Install the package**

```sh theme={"system"}
npm install @supernovae-st/nika@0.124.1
./node_modules/.bin/nika --version
```

npm brings the engine for your platform as an optional dependency: keep
optional dependencies enabled. The second line prints
`nika <version> (<build>)`: the bundled engine, at the package's version,
the engine your code will drive.

**2. Let the engine seal your runs (once per machine)**

```sh theme={"system"}
NIKA_KEYCHAIN=off ./node_modules/.bin/nika key init
```

This writes a run-signing key pair to `~/.nika/keys/` (mode `0600`). That
is where the engine your app starts and `nika serve` read it, on macOS and
on Linux: neither opens an OS keychain. With it, the engine signs each
run's record and `traceVerify(receipt)` answers `verified`. Skip it and
runs still succeed, unsigned. Keep `NIKA_KEYCHAIN=off`: without it,
`nika key init` refuses on a Linux terminal (its `--force` puts the key in
the desktop's Secret Service, where neither looks) and on a Mac whose
keychain holds a key from Nika 0.122 or older, which 0.124.1 can neither
read nor copy. In CI, point `NIKA_RUN_KEY_FILE` and `NIKA_RUN_PUB_FILE` at
a key pair you provide.

**3. Save a workflow**

Create `hello.nika` in the application directory:

```yaml hello.nika theme={"system"}
nika: hello
model: mock/echo
permits: {}
tasks:
  greeting:
    infer:
      prompt: "Say hello from the Nika SDK."
      max_tokens: 32
outputs:
  greeting: ${{ tasks.greeting.output }}
```

`mock/echo` is the engine's rehearsal model: it echoes the prompt back,
prefixed `mock(echo) ·`, with no key and no model call. `permits: {}`
grants the workflow no file, network, command or tool access.

**4. Run it from TypeScript**

Save `demo.mts` next to the workflow:

```ts demo.mts theme={"system"}
import { Nika, isNikaRunSucceeded } from '@supernovae-st/nika'

const nika = new Nika({ cwd: process.cwd() })

// The engine checks the file first: a workflow it refuses throws here and never starts.
const run = await nika.run<{ greeting: string }>('hello.nika', { maxCostUsd: 0 })
const result = await run.result()

// A failed or paused run is data you read, not an exception.
if (!isNikaRunSucceeded(result) || !result.receipt) {
  console.error(result.status, result.error?.code, result.error?.message)
  process.exit(1)
}
console.log(result.outputs?.greeting)

// Ask the engine to verify the run's tamper-evident record.
const proof = await nika.traceVerify(result.receipt)
console.log(proof.verified ? 'receipt verified' : `not verified: ${proof.reason}`)
```

```sh theme={"system"}
node demo.mts
```

```text theme={"system"}
mock(echo) · Say hello from the Nika SDK.
receipt verified
```

Node.js 22.18 or newer runs the `.mts` file as is. On an older 22.x, name
it `demo.mjs` and drop `<{ greeting: string }>`.

That is the whole loop: one file, one call, one typed result, one verified
record. Change the prompt in `hello.nika` and run again: the echo follows the
file.

> [!NOTE]
>
> **Seeing `not verified: receipt_mismatch`?** The run succeeded, but the
> engine found no run-signing key in `~/.nika/keys/`, so the record is
> unsealed. Do step 2, then run the demo again. On Linux, a key that
> `nika key init --force` put in the Secret Service can be copied there
> instead, from a terminal: `./node_modules/.bin/nika key align`.

## What `maxCostUsd: 0` does

`maxCostUsd` caps what this run may spend on metered models, at the catalog's
list prices. Before anything runs, a cost floor above the cap makes `run()`
throw `NikaOperationError` with code `NIKA-1709`, and a cloud model the
catalog cannot price makes it throw with code `run_refused`
(`price unknown: …`): nothing is called and nothing is spent. During a run,
the call that crosses the cap completes and counts, nothing new starts, and the
result is `failed` with `NIKA-1704`. Local and `mock/echo` models are never
blocked. When you switch `model:` to a paid cloud model, raise `maxCostUsd` to
the budget you accept. [Spend caps](https://docs.nika.sh/sdk/runtime/spend.md) has the details.

## Watch the events, after or during the run

A run keeps its events, so you can read them after the result:

```ts theme={"system"}
for await (const event of run.events()) {
  console.log(event.kind, event.task ?? '', event.status ?? '')
}
```

```text theme={"system"}
run.started
task.scheduled greeting
task.started greeting
task.completed greeting
engine.event
run.settled  succeeded
```

Six events for this one-task workflow on the local engine; `engine.event`
carries the engine's own `workflow_completed` frame on `event.raw`. Iterate
`run.events()` before awaiting `run.result()` to watch them live instead.
[Run events](https://docs.nika.sh/sdk/runtime/events.md) explains the vocabulary and why a server run
yields fewer.

## Audit without running

`run()` already checks the file, so you never need `check()` first. Use it to
show the audit on its own, in CI or an editor:

```ts theme={"system"}
const report = await nika.check('hello.nika', { nativeStrict: true })
console.log(report.clean, report.exitCode) // true 0
```

## Pass your app's data

`inputs` fills the workflow's declared `inputs:` with literal JSON values:

```ts theme={"system"}
const run = await nika.run('greet.nika', { inputs: { name: 'Ada', limit: 2 } })
```

The engine type-checks them before any run exists: a string `'2'` for an
integer input is refused with `NikaOperationError` (`input_type_mismatch`), an
undeclared key with `unknown_input`, and a missing required input with
`NIKA-1708`. Values stay literal: `'@env:HOME'` is the text `@env:HOME`, never
an environment lookup. Keep secrets out of `inputs`.

## What a verified receipt proves

`traceVerify(receipt)` reads the record the run already wrote. It does not
re-execute the workflow or re-ask a model.

| Fact | How you know | What it does not mean |
| - | - | - |
| **Admitted** | `run()` resolved with a `NikaRun` | that the run succeeded |
| **Succeeded** | `isNikaRunSucceeded(result)`; `result.outputs` are real | that the record is sealed |
| **Verified** | `traceVerify(receipt)` returns `{ verified: true, verdict: 'verified' }` | that the output is correct, or that a person read it |

A record is tamper-evident, not tamper-proof: verification shows it is intact
and matches the receipt. [Receipts](https://docs.nika.sh/sdk/runtime/receipts.md) covers both doors.

## Next

> [accordion] Which version is this?
>
> > [!NOTE]
> >
> > These pages describe the published `@supernovae-st/nika@0.124.1`
> > [SDK release](https://github.com/supernovae-st/nika-client/tree/v0.124.1) and its matching
> > [bundled engine](https://github.com/supernovae-st/nika/releases/tag/v0.124.1). A newer engine
> > release or SDK main commit does not change an installed package. Use Node.js
> > 22 or newer, on macOS (arm64, x64) or Linux with glibc (x64, arm64); no
> > global Nika installation is required for local mode.
>
> > [!TIP]
> >
> > **One client, local by default.** Import `Nika` from
> > the package root and omit `url`. The client takes the engine
> > from `bin`, then `NIKA_BIN` (both absolute paths), then
> > the platform package npm installed with it; it never uses a
> > `nika` found on your `PATH`. These examples target
> > the published [0.124.1 SDK release](https://github.com/supernovae-st/nika-client/tree/v0.124.1).

> [card] [How the SDK works](https://docs.nika.sh/sdk/how-it-works.md)
>
> The two doors, the lifecycle and who owns what.

> [card] [Project setup](https://docs.nika.sh/sdk/start/project-setup.md)
>
> Organize workflows, project policy and runtime state.

> [card] [Run and cancel](https://docs.nika.sh/sdk/local/run-and-cancel.md)
>
> Observe, settle and stop a run.

> [card] [Server quickstart](https://docs.nika.sh/sdk/remote/serve-quickstart.md)
>
> The same methods against your own nika serve.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.