# ctx.fs()

> Reference for ctx.fs(), the project's own directory of files: root, resolve(), the Node fs/promises methods, and the errors it throws.

`ctx.fs()` returns a handle to the project's own directory of ordinary files, with the familiar `fs/promises` methods. Call it with no arguments. The directory persists across executions and deployments, and every function in the project sees the same one. It is turned on per organization, and your project has to call `ctx.fs()` in its own source for Helix to provide it.

## Signature

```typescript
fs(): ProjectFileSystem
```

Calling `ctx.fs()`, reading `root`, and calling `resolve()` never touch the disk. The first file operation does. Setup, local development, and the constraints live in [the project files guide](/documentation/guides/project-files/).

## ProjectFileSystem

Every path argument is relative to `root`. Arguments and return types match Node's `fs/promises`, and Node's errors reach you unchanged, so `err.code === 'ENOENT'` works as usual.

| Member | Returns | Notes |
|---|---|---|
| `root` | `string` | Absolute path of the project's directory. It differs between `helix dev` and a deployed run, so don't hard-code it. |
| `resolve(...segments)` | `string` | Joins the segments under `root` and returns the absolute path, for libraries that take a path. It checks the path as text and doesn't look at symlinks or the disk. |
| `readFile(path, options?)` | `Promise<Buffer>` or `Promise<string>` | A string when you pass an `encoding`. |
| `writeFile(path, data, options?)` | `Promise<void>` | Replaces the file. |
| `appendFile(path, data, options?)` | `Promise<void>` | Creates the file if it's missing. |
| `readdir(path, options?)` | `Promise<string[]>` or `Promise<Dirent[]>` | `Dirent` entries with `withFileTypes: true`. Accepts `recursive`. |
| `stat(path, options?)` | `Promise<Stats>` | `BigIntStats` with `bigint: true`. |
| `lstat(path, options?)` | `Promise<Stats>` | Describes a symlink instead of following it. |
| `mkdir(path, options?)` | `Promise<void>`, or `Promise<string \| undefined>` with `recursive: true` | With `recursive: true`, resolves to the first directory created, or `undefined` when it already existed. |
| `rm(path, options?)` | `Promise<void>` | Accepts `recursive` and `force`. The project's directory itself can't be removed. |
| `rename(from, to)` | `Promise<void>` | Both paths must stay inside the directory. The directory itself can't be renamed. |
| `copyFile(from, to, mode?)` | `Promise<void>` | Both paths must stay inside the directory. |
| `access(path, mode?)` | `Promise<void>` | Rejects when the path is missing. |
| `truncate(path, length?)` | `Promise<void>` | Resizes a file. |
| `createReadStream(path, options?)` | `ReadStream` | The `fd` and `fs` options are rejected. |
| `createWriteStream(path, options?)` | `WriteStream` | The `fd` and `fs` options are rejected. |

## Paths stay inside the directory

A path that would leave the project's directory is rejected before anything runs:

- A `..` segment, even one that would stay inside, such as `a/../b`.
- An absolute path.
- A NUL byte.
- A symlink that points outside the directory, in any part of the path.

`resolve()` rejects the first three. Every file operation also follows symlinks and rejects one that leads out, so a link created by a library can't be used to reach other files.

## Errors

Read `err.code` on the thrown error.

| Code | HTTP status when uncaught | When |
|---|---|---|
| `FS_PATH_INVALID` | 400 | A path leaves the project's directory. The message says why. |
| `FS_NOT_MOUNTED` | 500 | The deployed function has no project file system. The message says to redeploy. See [when something fails](/documentation/guides/project-files/#when-something-fails). |
| `FS_NOT_AVAILABLE` | 501 | The environment has no project directory to give. |

The caller of an uncaught `FS_NOT_MOUNTED` sees only a generic server error. The cause is in the execution's logs. Operating system errors such as `ENOENT` and `EEXIST` pass through with their `code`.

## Example

```typescript
// functions/api/v1/notes.post.ts
import { z } from 'zod';
import { defineFunction } from '@trayai/helix-sdk';

export const input = z.object({
  name: z.string().regex(/^[\w-]+$/),
  text: z.string(),
});

export default defineFunction<typeof input>(async (ctx) => {
  const fs = ctx.fs();

  await fs.mkdir('notes', { recursive: true });
  await fs.writeFile(`notes/${ctx.input.name}.txt`, ctx.input.text);

  return { saved: ctx.input.name, notes: await fs.readdir('notes') };
});
```

The pattern on `name` keeps a caller from sending `../x`, which `ctx.fs()` would reject with `FS_PATH_INVALID` anyway.

---

Canonical: https://helix.tray.ai/documentation/reference/context/fs/
Any link on this page is available as markdown by appending .md to its URL.
Full corpus: https://helix.tray.ai/documentation/llms-full.txt