# file.save()

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

`file.save({ storageId, path, localOnly }) → Promise<Result>`

Writes the file — to the account, to the local disk, or to both. The one call here that costs a credit.

Status: POST /api/files/save · 1 credit · refunded if the write fails

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `storageId` | string |  | Which storage to write to. Without it, the account's default. |
| `path` | string |  | Node only. Also write the bytes to this local path, creating the folders. |
| `localOnly` | boolean |  | With `path`: write locally and **stop**. Nothing is uploaded, nothing is charged. |

## The three ways to use it {#three}

```javascript
// 1. To the account — the usual one
await file.save();

// 2. To the account AND to this machine, in one call
await file.save({ path: "./out/report.md" });

// 3. To this machine only — no request, no credit
await file.save({ path: "./out/report.md", localOnly: true });
// { ok: true, status: "saved_local", path: "./out/report.md" }
```

> **A failed local write does not fail the call**
>
> If the path cannot be written, the SDK warns on the console and carries on to the upload. Check the file yourself when the local copy is the one that matters — a read-only folder will not raise.

## Returns {#returns}

```javascript
{
  ok: true,
  status: "saved",
  sourceId: "…",            // how you find it again
  name: "report.md",
  category: "document",
  mimeType: "text/markdown",
  location: "object",       // the kind of storage it landed in
  metadata: {
    size: 2048,
    storageId: "…",
    remainingBytes: null,   // what is left, when the storage tracks it
    durationMs: 260
  },
  creditsUsed: 1
}
```

The object learns from it: `file.sourceId`, `file.location` and `file.metadata` are set, which is what makes [update()](/sdk/ref/files/update) possible straight afterwards.

## Choosing where it goes {#target}

```javascript
// The account's default
await file.save();

// A storage you named
const all = await api.storage.list();
const archive = all.find((s) => s.label === "Archive");
await file.save({ storageId: archive.id });
```

| Storage kind | Save to it |
| --- | --- |
| object · database | yes — bytes are kept on the account |
| filesystem (a computer) | refused: storage_is_local |
| phone · browser | refused: storage_is_local |

The refusal comes **before** the credit is spent. It is not a failure to retry: those devices keep their own files, and the way to write them is from the device.

## The credit, and when it comes back {#cost}

- One credit is taken before the write.
- If the write then fails, it is **refunded in full** and you get `502 storage_failed`.
- A refusal that happens first — no storage, a local one, no content — costs nothing at all.

## A document keeps being worked on after you get your answer {#indexing}

When the category is `document`, the text is indexed for semantic search once the response has been sent. So a file is immediately readable and findable-by-meaning a moment later.

> **Do not search for something you just saved, in the same breath**
>
> Indexing is deliberately not awaited — it keeps the save fast. A search run immediately afterwards may not see it yet.

## Errors {#errors}

| Code | Status | When | Credit |
| --- | --- | --- | --- |
| — (thrown locally) | — | content is undefined or null. | none taken |
| missing_file_content | 400 | Neither a file nor text content reached the server. | none taken |
| storage_not_found | 404 | The account has no storage at all. | none taken |
| storage_is_local | 400 | The target is a computer or a phone. | none taken |
| not_enough_credits | 402 | The balance will not cover it. | none taken |
| storage_failed | 502 | The write itself failed. | refunded |

```javascript
import { IaGenifyError } from "iagenify";

try {
  await file.save({ storageId });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "storage_is_local") {
    // That storage is a device. Write it there instead.
    const device = await api.storage.get({ id: storageId });
    await device.files.write("report.md", file.content);
  } else throw err;
}
```

- [file.update()](/sdk/ref/files/update): Replacing the content afterwards.
- [LocalFiles](/sdk/ref/local-files): Writing to a machine's own files instead.
- [Credits](/sdk/concepts/credits): How a spend is taken and given back.

```python
# 1. To the account — the usual one
file.save()

# 2. To the account AND to this machine, in one call
file.save(path="./out/report.md")

# 3. To this machine only — no request, no credit
file.save(path="./out/report.md", local_only=True)
# {"ok": True, "status": "saved_local", "path": "./out/report.md"}
```

```python
{
    "ok": True,
    "status": "saved",
    "sourceId": "…",            # how you find it again
    "name": "report.md",
    "category": "document",
    "mimeType": "text/markdown",
    "location": "object",       # the kind of storage it landed in
    "metadata": {
        "size": 2048,
        "storageId": "…",
        "remainingBytes": None,   # what is left, when the storage tracks it
        "durationMs": 260,
    },
    "creditsUsed": 1,
}
```

```python
# The account's default
file.save()

# A storage you named
archive = next(s for s in api.storage.list() if s.label == "Archive")
file.save(storage_id=archive.id)
```

```python
from iagenify import IaGenifyError

try:
    file.save(storage_id=storage_id)
except IaGenifyError as error:
    if error.code != "storage_is_local":
        raise
    # That storage is a device. Write it there instead.
    device = api.storage.get(id=storage_id)
    device.files.write("report.md", file.content)
```
