JavaScript
Convert file
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
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();
}
Handling of 202 (auto-async)
When all backends are saturated, API returns202 with a job_id. Wrapper that handles both cases:
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()}`);
}
