# convert()

Canonical URL: https://developers.iagenify.com/sdk/ref/tools/convert/
Language: en

`api.tools.convert({ from, to, url | file }) → Promise<Converted>`

Converts a document between 244 pairs of formats — 33 sources, 31 targets. Upload the bytes, or give an address and let the platform fetch them.

Status: POST /api/convert · 1 credit · 50 MB at most

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `from` | string | required | The source format. Common spellings are accepted: `md`, `jpeg`, `yml`, `.TXT`. |
| `to` | string | required | The target format. |
| `url` | string |  | Fetch the source from there. Checked by the address guard, capped at 50 MB. |
| `file` | Buffer | Blob | Uint8Array |  | The bytes, uploaded. Use this **or** `url`. |

Neither `from` nor `to` may be left out — the format is not guessed from the bytes, because a wrong guess produces a plausible file that is not the one you asked for.

## The four families {#families}

| Family | Sources | Targets |
| --- | --- | --- |
| Image | `jpg png webp gif tiff svg avif` | the encodable ones, plus `pdf` and `base64` |
| Data | `json yaml csv tsv xml jsonl` | each other, plus `markdown html xlsx` |
| Document | `pdf docx pptx xlsx markdown html text` | text, HTML, `pdf`, `docx`, images from a PDF |
| Media | `mp3 wav ogg m4a aac flac opus` · `mp4 webm mkv mov avi` | each other; video to audio; to and from an animated `gif` |

## Examples {#examples}

```javascript
// A PDF on the web, as markdown
const doc = await api.tools.convert({ from: "pdf", to: "markdown", url: "https://example.com/report.pdf" });
console.log(doc.content);

// A spreadsheet in hand, as rows
const rows = await api.tools.convert({ from: "xlsx", to: "json", file: buffer });

// An image, re-encoded
const webp = await api.tools.convert({ from: "png", to: "webp", file: png });
await pc.files.write("Assets/logo.webp", Buffer.from(webp.base64, "base64"));
```

## Returns {#returns}

```javascript
{
  ok: true,
  family: "document",
  conversion: "pdf-to-markdown",
  from: "pdf",
  to: "markdown",
  mimeType: "text/markdown",
  content: "# Quarterly report\n\n…",     // text targets
  base64: "…",                              // binary targets
  metadata: { originalSize: 184_221, convertedSize: 9_118, durationMs: 1_204 },
  creditsUsed: 1,
  timing: { durationMs: 1_240 }
}
```

> **content or base64, never both**
>
> A text target fills content; a binary one fills base64. Test for the field rather than assuming one — the same call with a different to gives the other.

## An impossible target says what is possible {#refusal}

A pair that is not supported comes back as `unsupported_conversion`, and **the message names every target reachable from that source**. One refusal instead of a series of attempts.

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

try {
  await api.tools.convert({ from: "pdf", to: "mp3", url });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "unsupported_conversion") {
    console.log(err.message);
    // La conversion de 'pdf' vers 'mp3' n'est pas prise en charge.
    // Depuis 'pdf' : text, markdown, html, json, png, jpg, webp.
  }
}
```

> **The list is in the message, not on the error object**
>
> The HTTP response also carries it as a supportedTargets array, which is what a direct call gets. Through the package you read the message — so parse it, or keep the matrix on this page beside your own call.

## Spellings are normalised {#spellings}

`md` and `mdown` are `markdown`; `jpeg` and `jfif` are `jpg`; `yml` is `yaml`; `txt` is `text`; a leading dot and any case are ignored. The fields that come back carry the normalised names, which is what makes `conversion` safe to use as a key.

## Bounds {#bounds}

| Thing | Bound |
| --- | --- |
| Source size | 50 MB, uploaded or fetched |
| Fetch timeout | 20 seconds |
| Price | 1 credit per conversion, whatever the size |

There is no timeout on the call itself: a video transcode takes minutes and it is charged, so abandoning the request would pay for a result you never see.

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| *thrown locally* | — | `from` or `to` is absent, or neither `file` nor `url`: the package refuses before any request. |
| unsupported_conversion | 400 | The pair is not in the matrix. Carries `supportedTargets`. |
| url_refusee | 400 | The address is refused by the guard. |
| invalid_url_source | 400 | The source could not be fetched. |
| conversion_failed | 502 | The converter refused the bytes. The credit is given back. |
| NotEnoughCredits | 402 | The balance will not cover one credit. |

## As a tool {#as-a-tool}

Declared to a model it takes one parameter, `url`: the source format is read from the address and the target is chosen for it — text out of a PDF, DOCX or PPTX, rows out of a spreadsheet. The full matrix is for your own calls.

- [The agent's convert](/sdk/agent/tools/convert): The workspace version, which works on a file by path.
- [api.tools](/sdk/ref/tools): The fifteen, and the two ways to use them.

```python
import base64

# A PDF on the web, as markdown
doc = api.tools.convert({"from_format": "pdf", "to": "markdown", "url": "https://example.com/report.pdf"})
print(doc["content"])

# A spreadsheet in hand, as rows
rows = api.tools.convert({"from_format": "xlsx", "to": "json", "file": xlsx_bytes})

# An image, re-encoded
webp = api.tools.convert({"from_format": "png", "to": "webp", "file": png_bytes})
pc.files.write("Assets/logo.webp", base64.b64decode(webp["base64"]))
```

```python
{
    "ok": True,
    "family": "document",
    "conversion": "pdf-to-markdown",
    "from": "pdf",
    "to": "markdown",
    "mimeType": "text/markdown",
    "content": "# Quarterly report\n\n…",     # text targets
    "base64": "…",                              # binary targets
    "metadata": {"originalSize": 184_221, "convertedSize": 9_118, "durationMs": 1_204},
    "creditsUsed": 1,
    "timing": {"durationMs": 1_240},
}
```

```python
from iagenify import IaGenifyError

try:
    api.tools.convert({"from_format": "pdf", "to": "mp3", "url": url})
except IaGenifyError as error:
    if error.code != "unsupported_conversion":
        raise
    print(error)
    # La conversion de 'pdf' vers 'mp3' n'est pas prise en charge.
    # Depuis 'pdf' : text, markdown, html, json, png, jpg, webp.
```
