Parameters
Returns
const file = await api.files.get(sourceId);
file.name; // "report.md"
file.mimeType; // "text/markdown"
file.content; // a string, here — a Buffer for binary
file.sourceId; // set, so update() works straight away
file.location; // the kind of storage it is in
file.metadata; // { size, mimeType, originalName }file = api.files.get(source_id)
file.name # "report.md"
file.mime_type # "text/markdown"
file.content # a str, here — bytes for binary
file.source_id # set, so update() works straight away
file.location # the kind of storage it is in
file.metadata # {"size", "mimeType", "originalName"}String or bytes, decided by the type
A file whose type is text-ish comes back as a string; everything else as a Buffer.
| mimeType | content |
|---|---|
| text/* · json · xml · yaml · csv · markdown · plain | a string |
| image/* · audio/* · video/* · pdf · anything else | a Buffer |
const file = await api.files.get(sourceId);
if (typeof file.content === "string") {
console.log(file.content.slice(0, 200));
} else {
await fs.writeFile(`./${file.name}`, file.content);
}from pathlib import Path
file = api.files.get(source_id)
if isinstance(file.content, str):
print(file.content[:200])
else:
Path(file.name).write_bytes(file.content)A text file saved without a mimeType comes back as a Buffer. Set the type at create() and this never comes up.
It reads the whole file
There is no range and no streaming: the content arrives in one response, as text or as base64 that the SDK decodes for you. For something large, that is memory on both sides — size it before you fetch.
const files = await api.files.list();
const target = files.find((f) => f.id === sourceId);
if (target.size > 20 * 1024 * 1024) {
throw new Error(`${target.name} is ${Math.round(target.size / 1e6)} MB — too big to read in one go`);
}
const file = await api.files.get(sourceId);files = api.files.list()
target = next(f for f in files if f["id"] == source_id)
if target["size"] > 20 * 1024 * 1024:
raise ValueError(f"{target['name']} is {round(target['size'] / 1e6)} MB — too big to read in one go")
file = api.files.get(source_id)Errors
| Code | Status | When |
|---|---|---|
| — (thrown locally) | — | No sourceId passed. |
| file_not_found | 404 | No file of this account with that id. |
| file_missing_on_disk | 404 | The record exists and its bytes do not. |
The record and the bytes have come apart. Retrying answers the same thing; what helps is deleting the record and saving the file again.
A file belonging to another account answers the same 404: every lookup is scoped to the caller.