> ## 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 from browser, Node.js or agents.

# JavaScript

## Convert file

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

## Convert URL signed

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

Do not put your API secret key on the public frontend. Use your own backend if the end user is in a browser.

## Handling of 202 (auto-async)

When all backends are saturated, API returns `202` with a `job_id`. Wrapper that handles both cases:

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