Skip to main content
More

Types

Use Rivet Actors TypeScript context and helper types to derive actor contexts, actions, events, connections, and registry interfaces.

Context Types

Context types define what properties and methods are available in different parts of the actor lifecycle.

import { actor } from "rivetkit";
import { db } from "rivetkit/db";

const counter = actor({
  db: db({
    onMigrate: async (db) => {
      await db.execute(
        "CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY CHECK (id = 1), count INTEGER NOT NULL)",
      );
    },
  }),

  // CreateContext in onCreate hook
  onCreate: async (c, input: { initial: number }) => {
    await c.db.execute("INSERT INTO counter (id, count) VALUES (1, ?)", input.initial);
  },

  // ActionContext in actions
  actions: {
    increment: async (c) => {
      await c.db.execute("UPDATE counter SET count = count + 1 WHERE id = 1");
      const [row] = (await c.db.execute(
        "SELECT count FROM counter WHERE id = 1",
      )) as { count: number }[];
      return row.count;
    }
  }
});

Extracting Context Types

When writing helper functions that work with actor contexts, use context extractor types like CreateContextOf or ActionContextOf to extract the appropriate context type from your actor definition.

import { actor, CreateContextOf, ActionContextOf } from "rivetkit";

const gameRoom = actor({
  onCreate: (c, input: { roomId: string }) => {
    initializeRoom(c, input.roomId);
  },

  createVars: () => ({ players: [] as string[] }),

  actions: {
    addPlayer: (c, playerId: string) => {
      validatePlayer(c, playerId);
      c.vars.players.push(playerId);
    }
  }
});

// Extract CreateContext type for onCreate hook
function initializeRoom(
  context: CreateContextOf<typeof gameRoom>,
  roomId: string
) {
  console.log(`Initializing room: ${roomId}`);
  // context.vars is not available here (not created yet)
}

// Extract ActionContext type for actions
function validatePlayer(
  context: ActionContextOf<typeof gameRoom>,
  playerId: string
) {
  // Full context available in actions
  if (context.vars.players.includes(playerId)) {
    throw new Error("Player already in room");
  }
}

All Context Types

Each lifecycle hook and handler has a corresponding *ContextOf type, exported from "rivetkit". Pass typeof myActor as the type parameter.

Hook / HandlerContext Type
onCreateCreateContextOf
createVarsCreateVarsContextOf
onBeforeConnectBeforeConnectContextOf
onConnectConnectContextOf
onDisconnectDisconnectContextOf
onDestroyDestroyContextOf
onMigrateMigrateContextOf
onWakeWakeContextOf
onSleepSleepContextOf
onBeforeActionResponseBeforeActionResponseContextOf
actions.*ActionContextOf
runRunContextOf
workflow orchestration helpers (root, loop, try, race, join branches)WorkflowContextOf
workflow step / tryStep run + rollback helpersWorkflowStepContextOf
onRequestRequestContextOf
onWebSocketWebSocketContextOf

ActorContextOf, ConnContextOf, and ConnInitContextOf are general-purpose base context types useful for helper functions that don’t correspond to a specific hook.

Workflow context extractors are exported from both "rivetkit" and "rivetkit/workflow".

Edit this page Last updated November 10, 2026