Events and Event Handlers

Events are the primary way to trigger asynchronous work in Hot. Event handlers run when specific events occur, enabling decoupled, scalable workflows.

One event can select zero, one, or multiple handlers. Each selected handler invocation becomes a run in the event's stream. See the Platform Execution Model for the complete event, run, task, and stream lifecycle.

Event Handlers

Define event handlers using the on-event metadata:

::myapp::users ns

// Handle user creation events
on-user-created meta {on-event: "user:created"}
fn (event) {
  // event.data contains the event payload
  send-welcome-email(event.data.email)
  create-default-settings(event.data.id)
}

Event handlers can be grouped under an agent by adding agent: TypeName to the metadata. This enables per-agent run tracking, health metrics, and observability in the Hot App.

One Run per Event (once)

Every handler for an event type runs for each event, in every project deployed to the environment. A handler in a package that several projects depend on therefore runs once for each of those projects. Add once: true to run it one time instead:

record-signup meta {on-event: "user:created", once: true}
fn (event) {
  write-audit-entry(event.data.id)
}

Of all the handlers for an event type that set once: true, Hot runs one for each event. Handlers without it all run as usual. The built-in hot:schedule:new and hot:schedule:cancel handlers use it, so one event creates or cancels one schedule however many projects an environment has.

Event Schemas

Hot events appear in two related shapes:

1) Hot language shape (send)

When publishing from Hot code, the event shape is:

FieldTypeDescription
typeStrEvent name (for example "user:created")
dataAnyEvent payload

send("user:created", {...}) uses this shape, and a handler receives it as its event argument: event.type and event.data, nothing else. For the stream or run a handler is part of, call info().

2) HTTP API shape (POST /v1/events)

When publishing through the Hot API, the request body is:

FieldTypeRequiredDescription
event_typestringYesEvent name
event_datajsonYesEvent payload
stream_iduuidNoAppend to an existing stream instead of creating a new one
event_iduuidNoYour own ID for the event, which makes the publish safe to send again. Without one, Hot generates an ID and every publish is a new event

Response payload fields include:

FieldDescription
event_idPublished event UUID
stream_idStream UUID containing this event
event_typeEvent name
event_dataEvent payload
event_timeEvent timestamp

System Events in hot-std

hot-std defines built-in handlers for several reserved hot:* event types. These power core platform behavior.

Event TypePurposeExpected Payload (event.data)
hot:callExecute a function asynchronously{fn: "::ns/var", args: [...]}
hot:scheduleInternal scheduler trigger for scheduled functions{fn: "::ns/var", args: [...]}
hot:schedule:newCreate a dynamic one-time or recurring schedule{fn: "::ns/var", args: [...], schedule: "..."}
hot:schedule:cancelCancel a dynamic schedule{schedule-id: "uuid"} or {fn: "::ns/var"}

These handlers are defined in ::hot::lang.

The hot: namespace is reserved for system behavior. Prefer your own event namespace (for example user:created, billing:invoice-paid) for application events.

Deploy Events

Hot sends hot:deploy:activated once each time a build goes live, after the activation has committed. No built-in handler takes it, so it does nothing until you define one.

Field in event.dataTypeDescription
projectStrName of the project that was deployed
build-idStrUUID of the build that went live
carried-schedulesIntDynamic schedules the activation carried to this build from the build it replaced
cancelled-schedulesIntDynamic schedules the activation cancelled because an identical schedule is on this build

Like any event, it reaches every handler for its type in the environment. In an environment with several projects, check project to act only on your own.

Sending Events

Send events from your code to trigger handlers:

// Send an event (send is a core function)
send("user:created", {
  id: user-id,
  email: email,
  name: name
})

Or send events via the Hot API:

curl -X POST https://api.hot.dev/v1/events \
  -H "Authorization: Bearer $HOT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"event_type": "user:created", "event_data": {"id": "123", "email": "user@example.com"}}'

Background Jobs

Any function can be executed as a background job by sending a hot:call event:

# Execute a function asynchronously via event
curl -X POST https://api.hot.dev/v1/events \
  -H "Authorization: Bearer $HOT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "hot:call",
    "event_data": {
      "fn": "::myapp::jobs/process-order",
      "args": [{"order_id": "12345"}]
    }
  }'

Or from Hot code:

// Queue a background job
send("hot:call", {
  fn: "::myapp::jobs/process-order",
  args: [{order_id: "12345"}]
})

The response includes an event_id. You can retrieve runs triggered by the event:

curl https://api.hot.dev/v1/events/$EVENT_ID/runs \
  -H "Authorization: Bearer $HOT_API_KEY"

Retries

Event handlers can retry automatically when they fail:

on-payment-received meta {
  on-event: "payment:received",
  retry: 3
}
fn (event) {
  update-account-balance(event.data)
}

For full retry configuration (attempts, delay, backoff, jitter, and limits), see Retries.

Testing Handlers

hot test doesn't publish events: a test delivers them to their handlers with ::hot::test/deliver, at a time it can fix. See Events and schedules under hot test.