---
title: Toaster
description: Session-backed toast notifications with HTMX injection for FastHTML apps
module: fasthtml.toaster
---
Queue toast notifications in the user session with [`add_toast`](toaster.md#add_toast) and inject them into responses automatically via [`setup_toasts`](toaster.md#setup_toasts).

```python
app, rt = fast_app()
setup_toasts(app)

@rt
def save(sess):
    add_toast(sess, 'Saved', 'success')
    return P('Done')
```

### setup\_toasts

```python
def setup_toasts(
    app, # FastHTML app instance
    duration:int=5000, # Default auto-remove delay in ms
):
```

*Store `duration` on `app.state.toast_duration`, append toast CSS/JS to `app.hdrs`, and register [`toast_after`](toaster.md#toast_after) on `app.after`.*

### add\_toast

```python
def add_toast(
    sess, # Session mapping
    message:str, # Toast text
    typ:str='info', # One of `'info'`, `'success'`, `'warning'`, `'error'`
    dismiss:bool=False, # Include a dismiss `'x'` button?
):
```

*Append `(message, typ, dismiss)` to `sess['toasts']`.*

### Toast

```python
def Toast(
    message:str, # Toast text
    typ:str='info', # Style: `'info'`, `'success'`, `'warning'`, or `'error'`
    dismiss:bool=False, # Include a dismiss `'x'` button?
    duration:int=5000, # Auto-remove delay in ms
):
```

*Build a `.fh-toast.fh-toast-{typ}` `Div` that removes itself after `duration` ms.*

## Helpers

### toast\_after

```python
def toast_after(
    resp, req, sess
):
```

*FastHTML `after` hook draining queued session toasts into `req.injects` for empty, tuple, `FT`, or [`FtResponse`](core.md#ftresponse) results (uses [`Partial`](components.md#partial) on HTMX 4 fragments).*

### render\_toasts

```python
def render_toasts(
    sess
):
```

*Drain `sess['toasts']` into an out-of-band `Div` targeting `beforeend:#fh-toast-container`.*
