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

# SvelteKit

> Server endpoint in SvelteKit to convert documents.

# SvelteKit

## Server endpoint

```ts theme={null}
// src/routes/api/convert/+server.ts
import { env } from "$env/dynamic/private";
import { error } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";

const API_URL = env.FLASH_MD_API_URL!;
const API_KEY = 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 error(502, data.error);
  }
}

export const POST: RequestHandler = async ({ request }) => {
  const form = await request.formData();
  const file = form.get("file") as File | null;

  if (!file) throw error(400, "Missing file");

  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 new Response(await res.text(), {
      headers: { "content-type": "text/markdown; charset=utf-8" },
    });
  }

  if (res.status === 202) {
    const job = await res.json();
    const markdown = await pollJob(job.job_id);
    return new Response(markdown, {
      headers: { "content-type": "text/markdown; charset=utf-8" },
    });
  }

  throw error(res.status, await res.text());
};
```

Environment variables in `.env`:

```
FLASH_MD_API_URL=https://api.markpdf.tech
FLASH_MD_API_KEY=tu_key
```
