> For the complete documentation index, see [llms.txt](https://docs.osmosworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.osmosworks.com/business-modules/notifications.md).

# Notifications

This module covers how Osmos sends emails and other asynchronous notifications.

## Outbox pattern

Osmos uses the outbox pattern to ensure notifications are sent reliably without blocking the main transaction. When the application needs to send a notification, it writes an `OutboxEvent` to the database and commits. A background scheduler processes pending events asynchronously.

```mermaid
sequenceDiagram
    autonumber
    participant App as Service Layer
    participant DB as PostgreSQL
    participant Worker as OutboxProcessingJob (Scheduled 2s)
    participant Resend as Resend Email API

    Note over App,DB: ACID Transaction
    App->>DB: INSERT / UPDATE Business Entity (e.g. Member invited)
    App->>DB: INSERT OutboxEvent (status: PENDING, availableAt: NOW)
    App->>DB: COMMIT Transaction

    Note over Worker,Resend: Asynchronous Reliable Processing
    Worker->>DB: SELECT PENDING events WHERE availableAt <= NOW (Batch: 20)
    Worker->>DB: UPDATE status = PROCESSING (Optimistic Lock)
    Worker->>Resend: Send email using template (e.g. re_staff_invite)
    
    alt Email delivered successfully
        Worker->>DB: UPDATE status = COMPLETED
    else Network failure / API Error
        Worker->>DB: UPDATE status = PENDING, attempts++, availableAt = NOW + exponentialBackoff
    else Max retries exceeded (3 attempts)
        Worker->>DB: UPDATE status = FAILED
    end
```

## Outbox event lifecycle

* `PENDING` — waiting to be processed.
* `PROCESSING` — currently being handled.
* `COMPLETED` — successfully processed.
* `FAILED` — failed after the maximum number of retries.

## Processing

The `OutboxProcessingJob` runs at a configured interval (default 2 seconds) and processes batches of pending events (default 20). It:

1. Selects pending events whose `availableAt` time has passed.
2. Marks them as `PROCESSING` with optimistic locking.
3. Dispatches them to the appropriate handler.
4. Marks them `COMPLETED` or schedules a retry with exponential backoff.

A separate recovery task resets events that have been stuck in `PROCESSING` for longer than the timeout. A cleanup task removes completed events older than the configured retention.

## Retry policy

* Maximum attempts: 3
* Initial delay: 10 seconds
* Maximum delay: 1 hour

## Email provider

Email notifications are sent through Resend. The backend stores Resend API configuration and template IDs in `application.yml`.

## Notification templates

Configured templates include:

* `re_org_verify` — organization onboarding verification.
* `re_staff_invite` — staff invitation.
* `re_password_reset` — password reset.
* `re_customer_setup` — customer account setup.
* `re_payment_succeeded` / `re_payment_failed` — payment results.
* `re_customer_paid` — customer payment confirmation.
* `re_sub_activated` / `re_sub_receipt` / `re_sub_failed` / `re_sub_grace` / `re_sub_downgraded` / `re_sub_changed` / `re_sub_canceled` — subscription lifecycle.
* `re_tier_warning` / `re_tier_blocked` — tier limit notifications.

## Manual retry

Platform or tenant administrators can retry a failed outbox event:

`POST /api/v1/admin/outbox/{eventId}/retry`

## Configuration

| Setting                              | Default               | Description                       |
| ------------------------------------ | --------------------- | --------------------------------- |
| `OUTBOX_PROCESSING_ENABLED`          | `true`                | Enable outbox processing.         |
| `OUTBOX_PROCESSING_INTERVAL_MS`      | 2000                  | Interval between processing runs. |
| `OUTBOX_PROCESSING_BATCH_SIZE`       | 20                    | Events processed per run.         |
| `OUTBOX_RETRY_MAX_ATTEMPTS`          | 3                     | Maximum retry attempts.           |
| `OUTBOX_RETRY_INITIAL_DELAY_SECONDS` | 10                    | Initial retry delay.              |
| `OUTBOX_RETRY_MAX_DELAY_SECONDS`     | 3600                  | Maximum retry delay.              |
| `RESEND_API_KEY`                     | test key              | Resend API key.                   |
| `RESEND_FROM_EMAIL`                  | `support@example.com` | From address.                     |

## Needs clarification

* The exact mapping of template IDs to Resend template names in the live account is configuration-dependent.
* SMS, push, and in-app notification channels are not implemented in the current codebase.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.osmosworks.com/business-modules/notifications.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
