The unusual part is that it reads the object rather than its arguments. Set file.content, call update(), and that is the new content.
const file = await api.files.get(sourceId);
file.content = "# Q4\n\nRevised after the audit.";
await file.update();file = api.files.get(source_id)
file.content = "# Q4\n\nRevised after the audit."
file.update()What it needs
| Condition | Otherwise |
|---|---|
| file.sourceId is set | the SDK throws: there is nothing to update |
| file.content is not null | the SDK throws: an empty file is not a content |
Both are thrown locally, before any request. A file comes with its sourceId from get(), or acquires it on save() — so the natural sequence works and a freshly create()d object does not.
const file = api.files.create({ name: "notes.md", content: "…" });
await file.update(); // throws — never saved, so no sourceId
await file.save(); // now it has one
file.content = "…";
await file.update(); // finefile = api.files.create(name="notes.md", content="…")
file.update() # raises ValueError — never saved, so no source_id
file.save() # now it has one
file.content = "…"
file.update() # fineReturns
{
ok: true,
message: "…",
sourceId: "…",
metadata: { size: 2048, mimeType: "text/markdown" }
}{
"ok": True,
"message": "…",
"sourceId": "…",
"metadata": {"size": 2048, "mimeType": "text/markdown"},
}It replaces, it does not append
The new content is the whole content. To add to a file, read it, concatenate, and update.
const file = await api.files.get(sourceId);
file.content = `${file.content}\n${newLine}`;
await file.update();file = api.files.get(source_id)
file.content = f"{file.content}\n{new_line}"
file.update()The old content is gone. Read it first if it mattered — reading is free. A file whose history matters wants a new file per version, not an update.
What it does not change
| Changed | Unchanged |
|---|---|
| The content, and its size | The name |
| The storage's remaining space | The mimeType, and so the category |
| The sourceId |
So a text file cannot become an image by updating it. Save a new one for that.
Searchable text is not re-indexed
Indexing happens on save. After an update, a search can return the version you replaced. Where that matters, delete and save again rather than update.
Errors
| Code | Status | When |
|---|---|---|
| — (thrown locally) | — | No sourceId, or no content. |
| file_not_found | 404 | No file of this account with that id. |
| missing_content | 400 | Nothing usable reached the server. |