---
question: How do I publish an event from a module?
kind: task
via_web: na
via_cli: na
via_python: available
via_api: na
via_config: na
ref_python: engine.services.events.publish
prereq: A module, and the event service in `config.yaml` (`sqlite` by default). See [Events](/capabilities/build/platform/events-overview).
next:
  - /capabilities/build/platform/stream-events
  - /capabilities/build/platform/events-overview
---

# Record an event

An event is a type, named by the IRI of its class, and a JSON payload. Publishing it appends it to the log, then broadcasts it to live subscribers. If the broadcast fails, the log row still stands.

## Web UI

Not applicable. The event log is append-only and Nexus only reads it: **Events** in the sidebar and **System** › **Data** › **Events**, for super admins. Use the Python steps on this page.

## CLI

Not applicable. There is no `abi` command that publishes an event. Use the Python steps on this page. The command list is the [CLI reference](/user-guide/reference/cli).

## Python

### Declare the service

```python title="src/my_module/__init__.py"
from naas_abi_sdk import BaseModule, ModuleDependencies


class ABIModule(BaseModule):
    dependencies = ModuleDependencies(services=("events",))
```

### Name the event type

Pick an IRI you own for the type, one per kind of event:

```python
ORDER_SHIPPED = "http://ontology.naas.ai/abi/orders/OrderShipped"
```

To give the type a place in the schema graph, declare the class in a `.ttl` file under the module's `ontologies/` folder, as a subclass of `abi:LogProcess`; the module sends it to the engine when it starts, with `triple_store` declared. See [Create an ontology](/capabilities/build/knowledge-graph/creating-an-ontology).

### Publish

```python
from naas_abi_sdk.services.event import Event

stored = await self.engine.services.events.publish(
    Event(ORDER_SHIPPED, {"order_id": "A-1042"})
)
```

`stored` is a `StoredEvent`: `id`, `event_type`, `seq`, `timestamp` and the JSON `payload` as bytes. The event gets a new `id` and the current UTC `timestamp` unless you pass them. `publish` raises `TypeError` when the object is not an `Event`.

### Read it back

```python
recent = await self.engine.services.events.query(ORDER_SHIPPED, newest_first=True, limit=10)
```

With a type IRI, `query` returns the payloads as dicts. Live listeners are [Stream events](/capabilities/build/platform/stream-events).

## REST API

Not applicable. The event log is append-only: the System routes of the Nexus API read it, and a write answers 405. Use the Python steps on this page.

## Config

Not applicable. An event is a call. `services.event` only picks where the log lives: see [Events](/capabilities/build/platform/events-overview#backends).
