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

# Type-Safe Events

> Define validated event payloads and event result types.

Events are strongly typed and validated across the supported runtimes.

Repository example files:

* [`examples/simple.py`](https://github.com/ArchiveBox/abxbus/blob/main/examples/simple.py)
* [`abxbus-ts/examples/simple.ts`](https://github.com/ArchiveBox/abxbus/blob/main/abxbus-ts/examples/simple.ts)
* [`abxbus-rust/tests/test_base_event.rs`](https://github.com/ArchiveBox/abxbus/blob/main/abxbus-rust/tests/test_base_event.rs)
* [`abxbus-go/README.md`](https://github.com/ArchiveBox/abxbus/blob/main/abxbus-go/README.md)

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from typing import Any
    from abxbus import BaseEvent

    class OrderCreatedEvent(BaseEvent[dict[str, Any]]):
        order_id: str
        customer_id: str
        total_amount: float
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts theme={null}
    import { BaseEvent } from 'abxbus'
    import { z } from 'zod'

    const OrderCreatedEvent = BaseEvent.extend('OrderCreatedEvent', {
      order_id: z.string(),
      customer_id: z.string(),
      total_amount: z.number(),
      event_result_type: z.object({ ok: z.boolean() }),
    })
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    use abxbus::{
        event,
        event_bus::EventBus,
        BaseEvent,
    };
    use futures::executor::block_on;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
    struct OrderResult {
        ok: bool,
    }

    event! {
        struct OrderCreatedEvent {
            order_id: String,
            customer_id: String,
            total_amount: f64,
            event_result_type: OrderResult,
            event_result_schema: r#"{
                "type": "object",
                "properties": {"ok": {"type": "boolean"}},
                "required": ["ok"]
            }"#,
        }
    }

    let bus = EventBus::new(Some("OrdersBus".to_string()));
    bus.on(OrderCreatedEvent, |event: OrderCreatedEvent| async move {
        Ok(OrderResult {
            ok: event.total_amount > 0.0,
        })
    });

    let event = bus.emit(OrderCreatedEvent {
        order_id: "order-123".to_string(),
        customer_id: "customer-456".to_string(),
        total_amount: 42.50,
        ..Default::default()
    });

    let typed_result = block_on(event.event_result())?
        .expect("handler result");
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    type OrderCreatedEvent struct {
    	OrderID     string  `json:"order_id"`
    	CustomerID  string  `json:"customer_id"`
    	TotalAmount float64 `json:"total_amount"`
    }

    type OrderResult struct {
    	OK bool `json:"ok"`
    }

    bus := abxbus.NewEventBus("OrdersBus", nil)
    bus.On(func(event OrderCreatedEvent) (OrderResult, error) {
    	return OrderResult{OK: event.TotalAmount > 0}, nil
    })

    result, err := bus.Emit(OrderCreatedEvent{
    	OrderID:     "order-123",
    	CustomerID:  "customer-456",
    	TotalAmount: 42.50,
    }).EventResult()
    if err != nil {
    	panic(err)
    }
    typedResult, err := abxbus.EventResultAs[OrderResult](result)
    if err != nil {
    	panic(err)
    }
    _ = typedResult
    ```
  </Tab>
</Tabs>

* Python payload validation is powered by Pydantic models.
* TypeScript payload and result validation is powered by Zod schemas.
* Rust uses `event!` to keep event fields and event metadata in one block. Primitive result schemas are derived automatically; structured result schemas are supplied through `event_result_schema`.
* Go uses typed struct helpers for static shape at call sites, validates typed handler payloads against the declared payload struct, and derives JSON Schema from the declared result type for runtime result validation.
