---
title: Publish an event
description: Send event data from your application code.
pagination: false
sidebar:
  icon: send
---

Publish an event from the function that performs the operation, so other functions can react to its result.

## Publish from a function

> **Before you begin**
>
> Complete the [event runtime installation and
> binding](/guides/events/react-to-order-events#install-and-bind-the-selected-event-runtime) before
> publishing. Acceptance requires the selected profile; a function's `publishes` list grants a
> checked capability but does not install its runtime package.

First, [define `orders.created`](/docs/modules/events/define). Then publish it from the `defineFunction` that creates the order:

```ts title="src/services/orders/functions/create-order.function.ts"
import { defineFunction } from "@relkit/app/functions";
import { z } from "@relkit/app/schema";

const createOrder = defineFunction({
  input: z.object({
    orderId: z.string().min(1),
    sku: z.string().min(1),
    quantity: z.number().int().positive(),
  }),
  output: z.object({
    orderId: z.string(),
    sku: z.string(),
    totalCents: z.number().int().nonnegative(),
  }),
  publishes: ["orders.created"],
  handler: async (input, context) => {
    const order = { orderId: input.orderId, sku: input.sku, totalCents: input.quantity * 100 };
    await context.events["orders.created"].publish(order);
    return order;
  },
});

export default createOrder;
```

This function uses the [Orders example's](/guides/events/react-to-order-events) `priceOrder` helper. To publish the event:

1. Add `publishes: ["orders.created"]` to allow this function to publish the event.
2. Call `await context.events["orders.created"].publish(order)` to send the order data.

Use the exact event ID in both places. The data must match the event's `input` schema. Only events listed in `publishes` are available through `context.events`.

Publishing waits for the event to be accepted, not for its consumers to finish. Your function can return the order while confirmation runs separately.

## Publish from an event function

A `defineEventFunction` can publish events the same way: list each ID in `publishes`, then call `.publish()`.
