# get()

Canonical URL: https://developers.iagenify.com/sdk/ref/files/get/
Language: en

`api.files.get(sourceId) → Promise<IaGenifyFile>`

Reads a file back with its content. Text comes back as a string, anything else as bytes — and the object is ready to update.

Status: GET /api/files/:id · free · content included

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `sourceId` | string | required | From [save()](/sdk/ref/files/save) or [list()](/sdk/ref/files/list). Missing → the SDK throws. |

## Returns {#returns}

```javascript
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 }
```

## String or bytes, decided by the type {#text}

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 |

```javascript
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);
}
```

> **The decision is the type, not the bytes**
>
> 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 {#whole}

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.

```javascript
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);
```

## Errors {#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. |

> **file_missing_on_disk is worth noticing, not retrying**
>
> 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.

- [file.update()](/sdk/ref/files/update): The object comes back ready for it.
- [list()](/sdk/ref/files/list): Finding an id, and a size, before reading.

```python
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"}
```

```python
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)
```

```python
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)
```
