Parameters
namestringThe file name. Used as-is on the account; the server reduces it to a safe one on its own disk.
contentstring|Buffer|BlobThe bytes. Can be set after construction — it is read at save(), not here.
mimeTypestringDefaults to application/octet-stream, which makes the category document.
It returns synchronously and there is no await: this is a constructor in all but name.
The object
const file = api.files.create({ name: "notes.md", content: "…", mimeType: "text/markdown" });
file.name;
file.content;
file.mimeType;
file.sourceId; // null until it is saved
file.location; // null until it is saved
file.metadata; // null until it is savedfile = api.files.create(name="notes.md", content="…", mime_type="text/markdown")
file.name
file.content
file.mime_type
file.source_id # None until it is saved
file.location # None until it is saved
file.metadata # None until it is savedSet the mimeType, even though it is optional
It decides two things that are awkward to change later: the category, and whether get() hands the content back as text or as bytes.
| You pass | Category | get() returns |
|---|---|---|
| text/markdown | document | content as a string |
| application/json | data | content as a string |
| image/png | image | content as a Buffer |
| (nothing) | document | content as a Buffer |
Without a type the default is a binary one, so a perfectly readable Markdown file is handed back as a Buffer. One line of work now, or a confusing read later.
What content accepts
// A string
api.files.create({ name: "notes.md", content: "# Notes", mimeType: "text/markdown" });
// Bytes you already have
api.files.create({ name: "chart.png", content: pngBuffer, mimeType: "image/png" });
// Something generated — write it out the moment you have it
const { outputs } = await assistant.generate({ inputs, outputs: [{ type: "image" }] });
api.files.create({ name: "cover.png", content: outputs[0].content, mimeType: outputs[0].mime });import base64
# A string
api.files.create(name="notes.md", content="# Notes", mime_type="text/markdown")
# Bytes you already have
api.files.create(name="chart.png", content=png_bytes, mime_type="image/png")
# Something generated — write it out the moment you have it
outputs = assistant.generate(inputs=inputs, outputs=[{"type": "image"}])["outputs"]
api.files.create(name="cover.png", content=base64.b64decode(outputs[0]["content"]), mime_type=outputs[0].get("mime"))Filling it in afterwards
The content is read when you save, so building the object first and filling it later is fine — and it is how update() works.
const file = api.files.create({ name: "log.txt", mimeType: "text/plain" });
file.content = lines.join("\n");
await file.save();
// And to replace it later
file.content = "…";
await file.update();file = api.files.create(name="log.txt", content="", mime_type="text/plain")
file.content = "\n".join(lines)
file.save()
# And to replace it later
file.content = "…"
file.update()The SDK throws before any request when content is undefined or null — an empty string, on the other hand, is a legitimate empty file.