> ## Documentation Index
> Fetch the complete documentation index at: https://docs.markpdf.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript

> Fetch desde navegador, Node.js o agentes.

# JavaScript

## Convertir archivo

```ts theme={null}
export async function convertFile(file: File) {
  const res = await fetch(
    `https://api.markpdf.tech/convert/raw?filename=${encodeURIComponent(file.name)}&mode=fast`,
    {
      method: "POST",
      headers: {
        "x-api-key": process.env.FLASH_MD_API_KEY!,
        "content-type": file.type || "application/octet-stream"
      },
      body: file
    }
  );

  if (!res.ok) throw new Error(await res.text());
  return res.text();
}
```

## Convertir URL firmada

```ts theme={null}
export async function convertFromUrl(url: string, filename: string) {
  const res = await fetch("https://api.markpdf.tech/convert/from-url", {
    method: "POST",
    headers: {
      "x-api-key": process.env.FLASH_MD_API_KEY!,
      "content-type": "application/json"
    },
    body: JSON.stringify({ url, filename, mode: "fast" })
  });

  if (!res.ok) throw new Error(await res.text());
  return res.text();
}
```

No pongas tu API key secreta en frontend publico. Usa un backend propio si el usuario final esta en navegador.

## Manejo de 202 (auto-async)

Cuando todos los backends están saturados, la API devuelve `202` con un `job_id`. Wrapper que maneja ambos casos:

```ts theme={null}
const API_URL = "https://api.markpdf.tech";
const API_KEY = process.env.FLASH_MD_API_KEY!;

async function pollJob(jobId: string): Promise<string> {
  while (true) {
    await new Promise((r) => setTimeout(r, 5000));
    const res = await fetch(`${API_URL}/jobs/${jobId}`, {
      headers: { "x-api-key": API_KEY },
    });
    const data = await res.json();
    if (data.status === "completed") return data.body;
    if (data.status === "failed") throw new Error(data.error);
  }
}

export async function convert(file: File): Promise<string> {
  const res = await fetch(
    `${API_URL}/convert/raw?filename=${encodeURIComponent(file.name)}&mode=fast`,
    {
      method: "POST",
      headers: {
        "x-api-key": API_KEY,
        "content-type": file.type || "application/octet-stream",
      },
      body: file,
    }
  );

  if (res.status === 200) return res.text();
  if (res.status === 202) {
    const job = await res.json();
    return pollJob(job.job_id);
  }

  throw new Error(`HTTP ${res.status}: ${await res.text()}`);
}
```
