# Use cases and traces

> Each file knows which use case uploaded it, each case says which buckets it uses, and every upload is a step in the case's trace.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://files.docs.sypnia.com/buckets/use-cases/

For agents: Case attribution in Files: SDK option case or header Sypnia-Case on uploads, replacements, deletes and signed URLs; recorded in sypnia_files.objects.case (the case that last wrote the file) and in the audit log; inside a request traced by @sypnia/telemetry the SDK reads the active case and sends traceparent, so case can be omitted. A case file declares the buckets it uses: files: { reads: [menu], writes: [receipts] }; the Cases check reports a case that writes a bucket it does not list, or lists one it never uses. The case is a label, not a permission: policies decide access. Spans: files.upload <bucket>, files.download <bucket>, files.sign <bucket>, files.delete <bucket>, files.transform <bucket> with sypnia.case, size, content type, never the file or its path's query. Cases shows each case's buckets and file counts.

A file in your app is always there because someone did something: a customer uploaded a photo, an order made its receipt. Files records **which use case** of your map did it, so you can answer «where did this file come from?» and «which files does this case create?».

## Saying the case

```ts
await files.bucket('receipts').upload('order-1042.pdf', pdf, { case: 'place-order' });
```

or the header `Sypnia-Case: place-order` on any request to the API: uploads, replacements, deletes and signed links. The id is the one in your map, from `cases/<area>/<id>.case.yaml` ([Cases](https://cases.docs.sypnia.com/)).

When your server is traced with Telemetry's SDK, the case of the current request is already known: `@sypnia/files` reads it and sends the trace along, so you can leave `case` out.

## What is recorded

| Where                       | What                                                            |
| --------------------------- | --------------------------------------------------------------- |
| `sypnia_files.objects.case` | the case that last wrote the file; `null` when none was given   |
| the audit log               | the case of every write and delete made with a secret or S3 key |

Because the case is a column, you can query it like any other: «every receipt `place-order` wrote this week», «files no case owns».

## The case says which buckets it uses

`cases/orders/place-order.case.yaml`

```yaml
id: place-order
title: Place an order
actor: customer
files:
  reads: [menu]
  writes: [receipts]
```

The Cases check compares that with what really happens: a case that writes a bucket it does not list, or lists one it never uses, is reported, the same way as a table the case did not declare.

> **Note: A label, not a permission**
>
> The case name comes from the caller, so it never opens a file by itself. Who may read and write is always decided by your [policies](https://files.docs.sypnia.com/access/policies). The case tells you what the file is for; the policy decides who reaches it.

## In the trace

Each call is a step (a span) inside the trace of its case, next to the database queries and the page that started it:

| Span                                                  | What it carries                                                        |
| ----------------------------------------------------- | ---------------------------------------------------------------------- |
| `files.upload <bucket>`                               | the size, the content type, whether it was resumable, the time it took |
| `files.download <bucket>`, `files.transform <bucket>` | the size served, whether it came from the CDN's cache                  |
| `files.sign <bucket>`, `files.delete <bucket>`        | the bucket and how long the link lasts                                 |

No span carries the file's contents. With [Sypnia Telemetry](https://telemetry.docs.sypnia.com/), a slow upload shows up in the case where a person was waiting, and in [Sypnia Cases](https://cases.docs.sypnia.com/) each case lists its buckets and how many files it wrote.
