# Attachments

> Files linked to records in a tenant: a record per file, a private bucket whose policies only accept an upload a pending record expects, and reads only for scanned files. confirm_attachment() emits attachment.uploaded for the app's scan job. options.publicBuckets serves chosen buckets by public URL, for media such as product images.

- Category: Collaboration
- Module: `attachments`
- Modes: `managed`, `custom`
- Docs: https://bettersupabase.com/docs/blocks/attachments
- Page: https://bettersupabase.com/blocks/attachments

## Install

```bash
npx better-supabase add attachments
```

The command also installs `updated-at`, `memberships`, `permissions`.

## Configure

Add the block to `better-supabase.config.ts`:

```ts
import { defineConfig } from "better-supabase/config";

export default defineConfig({
  blocks: {
    // Required by attachments; add installs them too.
    "updated-at": {},
    memberships: {},
    permissions: {},
    // Modes: "managed", "custom". The default is "managed".
    attachments: { mode: "managed" },
  },
});
```

## Client

The React hooks are the registry item `@better-supabase/use-attachments` (`npx shadcn add @better-supabase/use-attachments`). They return data and actions and render no UI. Source: https://github.com/ScaleDockHQ/better-supabase/blob/main/packages/registry/items/hooks/use-attachments.ts

## Events

- `attachment.replaced`
- `attachment.restored`
- `attachment.scanned`
- `attachment.uploaded`

## Related blocks

Requires: [memberships](https://bettersupabase.com/blocks/memberships.md), [permissions](https://bettersupabase.com/blocks/permissions.md)

Works with: [profiles](https://bettersupabase.com/blocks/profiles.md), [subjects](https://bettersupabase.com/blocks/subjects.md), [imports](https://bettersupabase.com/blocks/imports.md)

## Recipes

- [Message composer](https://bettersupabase.com/recipes/message-composer.md): A message box that sends on Enter, signals typing, and uploads dropped, pasted or picked files onto the message.
- [Attachments uploader](https://bettersupabase.com/recipes/attachments-uploader.md): A drop zone that uploads files to a record and lists them with a download link.
- [Subject timeline](https://bettersupabase.com/recipes/subject-timeline.md): One record's comments, files and versions merged newest first, from `bsb.record(...).timeline`, skipping the bindings the caller can't read.
