# Events

URL: https://zerodev.ca/docs/prism/developer-documentation/events

> Run code when products, purchases, settings or modules change.



An event class listens for one event on `this.prism.events`. Put them in `src/events/`.

```js title="modules/example/src/events/checkoutFulfilled.js"
import { Events } from "#prism/modules";

export class CheckoutFulfilledEvent extends Events {
    constructor(prism) {
        super(prism, { name: "checkoutFulfilled", event: "checkout.fulfilled" });
    }

    async run({ customerEmail, items, licenses }) {
        this.prism.console.log(this.prism.console.levels.info, `${customerEmail} bought ${items.length} items.`);
    }
}
```

| Key     | Default  | What it does                         |
| ------- | -------- | ------------------------------------ |
| `name`  | Required | The listener name.                   |
| `event` | Required | The event to listen for.             |
| `once`  | `false`  | Stop listening after the first time. |

A listener that throws is logged and does not stop the others.

## Events

| Event                | Payload                                                                         | When                                                    |
| -------------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `product.created`    | `{ product, input }`                                                            | A product is created.                                   |
| `product.updated`    | `{ product, input }`                                                            | A product is edited.                                    |
| `product.removed`    | `{ product }`                                                                   | A product is deleted.                                   |
| `checkout.fulfilled` | `{ customerEmail, customerId, customerName, customerDiscord, items, licenses }` | A purchase is paid and its licenses are issued.         |
| `purchase.completed` | The same                                                                        | Fired right after `checkout.fulfilled`.                 |
| `config.saved`       | `{ module, group, values }`                                                     | A module's settings are saved.                          |
| `audit.recorded`     | The entry                                                                       | Something is added to the audit log.                    |
| `module.registered`  | The module                                                                      | A module is registered.                                 |
| `module.loaded`      | The module                                                                      | A module finishes loading.                              |
| `<module>.ready`     | The module                                                                      | The same, named after the module, such as `core.ready`. |
| `module.failed`      | `{ folder, module, error }`                                                     | A module fails to load.                                 |
| `module.toggled`     | `{ id, enabled }`                                                               | A module is turned on or off.                           |
| `module.deloaded`    | The module                                                                      | A module is unloaded.                                   |
| `<module>.shutdown`  | The module                                                                      | The same, named after the module.                       |
| `modules.ready`      | The loaded modules                                                              | Every module has loaded.                                |
| `prism.ready`        |                                                                                 | Prism has finished starting.                            |

In `checkout.fulfilled`, `items` holds `{ productId, quantity, addons }` for each product bought, and `licenses` holds only newly issued licenses. Addons added to an existing license are in `items` but not `licenses`.

## Purchase Webhook

To send purchases to another service without a module, set `webhookUrl` and `webhookSecret` in the `CHECKOUT` setting through `POST /api/settings`. Prism posts `{ event: "checkout.fulfilled", ...payload, timestamp }` to the URL after each purchase, with an `x-prism-signature` header holding the HMAC-SHA256 of the body using the secret, in hex.

## Firing Your Own

From a handler:

```js
this.emit("example.synced", { count });
await this.emitAsync("example.synced", { count });
```

`emitAsync` waits for every listener to finish. Prefix event names with your module ID.
