ctx.fs() gives your project its own directory of ordinary files. Use it when state is better as a file than as a JSON value, or when a library needs a real file path. The directory persists across executions and deployments, every function in the project sees the same one, and you work in it with the fs/promises methods you already know. It is turned on per organization, and your project has to call ctx.fs() in its own source for Helix to provide it.
Turn it on
Two things have to be true before a deployed project can use ctx.fs():
-
Your organization has it turned on. If it doesn’t, a deploy of a project that calls
ctx.fs()fails with this message, andhelix deploy --waitprints it and exits with status 1:This project uses ctx.fs(), but the project file system is not enabled for your organization yet. Remove the ctx.fs() calls to deploy, or contact Tray to have it enabled.
-
The call is in your project’s source. Helix looks for
ctx.fs()in your project’s source files when you deploy, and turns the file system on for that project when it finds one. That covers your functions and the helper files they import. It doesn’t cover theapp/folder or code inside a dependency.
Removing the last ctx.fs() call and redeploying turns the file system off for that project again. Your files stay where they are, and they’re there again the next time you call it.
Read and write files
// functions/api/v1/reports.post.ts
import { z } from 'zod';
import { defineFunction } from '@trayai/helix-sdk';
export const input = z.object({ month: z.string().regex(/^\d{4}-\d{2}$/) });
export default defineFunction<typeof input>(async (ctx) => {
const fs = ctx.fs();
await fs.mkdir('reports', { recursive: true });
await fs.writeFile(`reports/${ctx.input.month}.json`, JSON.stringify({ rows: 42 }));
return { reports: await fs.readdir('reports') };
});
Read a file back, and treat a missing one as a normal case:
// functions/api/v1/reports/[month].get.ts
import { defineFunction } from '@trayai/helix-sdk';
export default defineFunction(async (ctx) => {
try {
const text = await ctx.fs().readFile(`reports/${ctx.input.month}.json`, 'utf8');
return JSON.parse(text);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
throw ctx.error(404, 'No report for that month');
}
throw err;
}
});
The ctx.fs() reference lists every method.
Libraries that need a path
Some libraries want a path on disk, not a buffer. fs.resolve() joins segments under the project’s directory and returns the absolute path:
const fs = ctx.fs();
await fs.mkdir('data', { recursive: true });
const path = fs.resolve('data', 'cache.db'); // pass this to the library
fs.root is the directory itself. Neither value is stable across environments, so build paths with resolve() each time and don’t store them.
Local development
During helix dev the files live in .helix/project-files/ inside your project. The folder is created on first use and kept across restarts and file saves. The scaffolded .gitignore already excludes it.
One directory for the whole project
Every function in the project, and every execution running at the same time, uses the same directory. Nothing coordinates writers, so give each execution its own file names when two can run at once.
Deleting a project deletes its files.
When something fails
| What you see | Why | What to do |
|---|---|---|
A deploy fails and says ctx.fs() isn’t enabled for your organization | Your organization doesn’t have it yet | Remove the ctx.fs() calls, or contact Tray to have it turned on. |
FS_NOT_MOUNTED (HTTP 500) | The deployed function has no project file system, usually because the only ctx.fs() call is inside a dependency | Call ctx.fs() in your own source, then redeploy. |
FS_PATH_INVALID (HTTP 400) | A path has a .. segment, is absolute, has a NUL byte, or reaches outside through a symlink | Build paths relative to the directory, and validate names that come from callers. |
FS_NOT_AVAILABLE (HTTP 501) | The environment has no project directory to give | Run the code under helix dev or deployed. |
ENOENT, EEXIST and other Node errors | The file or folder isn’t there, or already is | Handle them as you would with fs/promises. They arrive unchanged. |