---
url: https://kit.kambium.net/docs/first-engine.md
description: >-
  Implement a provider-free engine task, with optional worker execution and
  regression verification.
---

# Create your first engine

Build a Heartbeat engine that saves a timestamp. It runs locally without
provider credentials.

Start with the app you [ran in Desktop](https://kit.kambium.net/docs/local-development.md).

## 1. Declare the engine kind

In `engines/runtime.ts`, add this entry to `engineRuntimes`:

```ts
heartbeat: defineEngineRuntime({ kind: "heartbeat", connections: {} }),
```

Keep the existing entries and the `EngineKind` type derived from the map. The
kind identifies stored engine instances. An empty `connections` map lets this
engine run without provider accounts, and adding a kind needs no migration.

## 2. Declare your task operation

Create `engines/tasks.ts`:

```ts
import type { TaskPayloadByOperation as RegisteredTaskPayloads } from "@kambium/kit/shared/types";

type AppTaskPayloads = {
  heartbeat: { requestedAt: string };
};

declare module "@kambium/kit/shared/types" {
  interface TaskPayloadByOperation extends AppTaskPayloads {}
}

export type TaskOperation = keyof AppTaskPayloads;
export type TaskPayload<Operation extends TaskOperation> = RegisteredTaskPayloads[Operation];
```

This gives the heartbeat handler a typed payload. Import the declaration from the
engine in the next step. It still needs an executable handler; the declaration
alone does not register one. See [Task operations](https://kit.kambium.net/docs/tasks.md#declare-and-register-operations)
for naming, validation and registration rules.

## 3. Implement the task

Create `engines/heartbeat.ts`:

<!-- #region heartbeat-engine -->

```ts
import { defineEngine } from "@kambium/kit";
import { engineRuntimes } from "./runtime";
import "./tasks";

export const heartbeatEngine = defineEngine({
  runtime: engineRuntimes.heartbeat,
  connections: {},
  tasks: {
    heartbeat: {
      worker: "background",
      async handler({ context, task, run }) {
        if (!task.payload) throw new Error("Heartbeat payload is missing");
        await context.repos.syncMasterRepo.updateSync(context.sync.id, {
          last_active_at: task.payload.requestedAt,
        });
        await run.success("Heartbeat recorded");
      },
    },
  },
});
```

<!-- #endregion heartbeat-engine -->

The handler receives the current engine instance and services through `context`.
It saves `last_active_at`, then records a task success event. A failed write
throws and prevents success from being reported.

The persisted payload may be null, so the handler checks it before writing.
The [engine context reference](https://kit.kambium.net/docs/engines.md#engine-context) explains the services
supplied to each invocation.

## 4. Register the engine

In `index.ts`, add this import:

```ts
import { heartbeatEngine } from "./engines/heartbeat";
```

Add this entry to the `engines` map passed to `createServer`:

```ts
heartbeat: heartbeatEngine,
```

Keep the existing engine registrations. The generated `satisfies` check
requires a backend registration for each kind in the runtime map.

To make Heartbeat selectable in the dashboard, add a page and catalog entry.
Follow [Dashboard integration](https://kit.kambium.net/docs/dashboard.md#route-registration-and-creation).

## 5. Check your engine's result

Your app can now run Heartbeat tasks. To see one complete and verify the saved
timestamp, adapt the generated worker check. [Local task check](https://kit.kambium.net/docs/tasks.md#optional-local-task-check)

## Continue building

Replace the handler with your engine's work and adapt the check to its expected
result. For lifecycle hooks and provider bindings, see [Engine definitions](https://kit.kambium.net/docs/engines.md).
