> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Read files

> Read a stored file's metadata and bytes inside a query or mutation, and link a public file by its URL.

Read a file through the `File` point returned by `ctx.store` or held in a `t.file` field.

* `ctx.get(file)` answers the metadata, `{ id, sha256, size,
  contentType }`, or `null` once the file is retired.
* `ctx.get(file.bytes)` answers the bytes, `{ contentType, size,
  bytes(), text() }`, or `null` once the file is retired. It refuses
  `Erased` when the file's unit was erased.

Both need a frame that clears the file's label, and both work in queries
and mutations.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { query, t } from "@bijectionhq/bijection";
import { expenses, finance, receipts, reviewer, submitter } from "./schema";

/** The expense's receipt, for its owner or its reviewer. */
export const receipt = query(
  { under: finance, allow: [submitter, reviewer], args: { expense: t.key(expenses) } },
  async (ctx, { expense }) => {
    const fact = await ctx.get(expenses(expense));
    const me = ctx.frame.as.id;
    if (fact === null || fact.receipt === null || (fact.owner !== me && fact.reviewer !== me)) return null;
    const stored = await ctx.get(receipts(fact.receipt));
    if (stored === null) return null;
    const opened = await ctx.get(stored.file.bytes);
    if (opened === null) return null;
    return { contentType: opened.contentType, bytes: opened.bytes() };
  },
);
```

Return only what the caller's entry and audience permit: the query decides
who sees the receipt, and the label decides who may run it at all. The
[customer file storage example](https://github.com/bijectionhq/bijection/tree/main/examples/customer-file-storage)
shows the whole flow: labels, roles, the upload route and the reads.

## Serve a public file at a URL

`ctx.get(file.url)` answers the URL the installation's file route
serves the file at, `<origin>/api/storage/<file>`, as a `string`. It
answers `null` when no such file is stored, or once it is retired. Since a
browser fetches the file at that URL outside any frame, only a public file
kept in the clear has one. That is a file stored at `label.public` with no
expiry. Any other file refuses `Invalid` (reason `FileNotServed`):
a labelled file, or one sealed for an expiry. Read those files' bytes with
`ctx.get(file.bytes)` inside a function whose label clears them.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { query, role, t } from "@bijectionhq/bijection";
import { logos } from "./schema";

/** The public logo's URL, for an <img> tag. */
export const logoUrl = query({ allow: [role.anyone], args: { logo: t.key(logos) } }, async (ctx, { logo }) => {
  const fact = await ctx.get(logos(logo));
  return fact === null ? null : await ctx.get(fact.file.url);
});
```

Like the other reads, it works in queries and mutations. In the
[test kernel](https://docs.bijection.com/development/testing/test-library.md) the origin is
`https://installation.test` unless `kernel(app, { origin })` names
another one.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.