# write()

Canonical URL: https://developers.iagenify.com/sdk/ref/local-files/write/
Language: en

`files.write(file, content) → Promise<Item>`

Writes a file and returns what was written. Text or binary, parents created, and an existing file is replaced.

Status: free · replaces · text and binary

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `file` | string | required | The path to write, relative to the workspace. |
| `content` | string | Buffer | Uint8Array | ArrayBuffer | required | What to write. Anything else is refused. |

## Text {#text}

```javascript
const item = await pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.");
// { name: "brief.md", path: "Launch plan/brief.md", type: "file", size: 29,
//   mimeType: "text/markdown", modifiedAt: "…" }
```

A string is written as UTF-8. The `mimeType` that comes back is read from the extension, not from the content — so the name you choose is what decides how the file is later treated.

## Binary {#binary}

```javascript
// A generated image, straight from a model session
const [picture] = await session.generate({
  inputs: [{ type: "text", content: "A fox in the snow" }],
  outputs: [{ type: "image" }],
}).then((r) => r.outputs);

await pc.files.write("Assets/fox.png", Buffer.from(picture.content, "base64"));

// A download
const response = await fetch("https://example.com/report.pdf");
await pc.files.write("Reports/report.pdf", await response.arrayBuffer());
```

## It replaces, and keeps nothing {#replaces}

> **There is no previous version here**
>
> write() is the plain operation: the old content is gone. Keeping a copy is what the agent's own write tools add on top, under .versions/ — if you want that behaviour in your code, read the file first and write the copy yourself.

```javascript
// Keep a copy before replacing
const before = await pc.files.read("notes.md");
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
await pc.files.write(`.versions/notes.md/${stamp}.md`, before.content);
await pc.files.write("notes.md", next);
```

## Folders are created as needed {#parents}

Writing `Clients/Acme/2026/invoice.md` creates the three folders on the way. One call, whatever the depth.

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| missing_content | 400 | `content` is `undefined` or `null`. An empty string is valid. |
| invalid_content | 400 | Not a string, a Buffer, a Uint8Array or an ArrayBuffer. |
| invalid_path | 400 | Absolute, outside the workspace, or the workspace root. |
| local_files_require_node | 400 | The runtime has no filesystem. |
| storage_not_on_this_machine | 409 | Another computer's storage. |

An object is **not** serialised for you: pass `JSON.stringify(value)`, so the formatting stays yours.

- [read()](/sdk/ref/local-files/read): Read it back — text as a string, binary as a Buffer.
- [LocalFiles](/sdk/ref/local-files): The workspace, the path rules, and the shape every method returns.

```python
item = pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.")
# {"path": "Launch plan/brief.md", "size": 29}
```

```python
import base64, urllib.request

# A generated image, straight from a model session
picture = session.generate(
    inputs=[{"type": "text", "content": "A fox in the snow"}],
    outputs=[{"type": "image"}],
)["outputs"][0]

pc.files.write("Assets/fox.png", base64.b64decode(picture["content"]))

# A download
with urllib.request.urlopen("https://example.com/report.pdf") as response:
    pc.files.write("Reports/report.pdf", response.read())
```

```python
from datetime import datetime, timezone

# Keep a copy before replacing
before = pc.files.read("notes.md")
stamp = datetime.now(timezone.utc).isoformat().replace(":", "-").replace(".", "-")
pc.files.write(f".versions/notes.md/{stamp}.md", before["content"])
pc.files.write("notes.md", next_content)
```
