Request handlers
If your route just accepts { source, ... } and returns a PDF, you don’t need
to write the handler yourself. handleCompileRequest is a standard
Fetch API
Request → Response function that works in every framework unmodified.
handleCompileRequest
Section titled “handleCompileRequest”import { handleCompileRequest } from '@nandan-varma/platex';
export const runtime = 'nodejs';export const POST = handleCompileRequest;It parses the JSON body, calls compile(), and returns a Response:
| Outcome | Status | Body |
|---|---|---|
| Success | 200 |
Raw PDF bytes, Content-Type: application/pdf |
| Compile failed | 422 |
JSON { errors, warnings } |
| Bad input | 400 |
JSON error |
| Remote service unreachable | 502 |
JSON error |
Request body
Section titled “Request body”{ source: string; // required — the main.tex content engine?: 'pdflatex' | 'xelatex' | 'lualatex'; passes?: 'auto' | 1 | 2 | 3; bibliography?: 'bibtex' | 'biber' | 'none'; files?: Record<string, string>; // filename → base64-encoded content timeout?: number;}Only source is required.
JSON responses instead of raw PDF
Section titled “JSON responses instead of raw PDF”Pass responseFormat: 'json' to always return 200 with a JSON body — useful
when the client (a browser, a Client Component) wants the PDF as base64
alongside diagnostics:
import { createRequestHandler, createPlatexClient } from '@nandan-varma/platex';
export const POST = createRequestHandler(createPlatexClient(), { responseFormat: 'json',});// response shape when responseFormat: 'json'{ pdf: string | null, errors: LatexError[], warnings: LatexWarning[] }responseFormat |
Success | Failure |
|---|---|---|
'pdf' (default) |
200, raw PDF bytes |
422, JSON { errors, warnings } |
'json' |
200, { pdf: base64, errors, warnings } |
200, { pdf: null, errors, warnings } |
Applying your client’s defaults
Section titled “Applying your client’s defaults”handleCompileRequest uses the default env-var-driven compile(). To apply a
client’s custom timeout / retry / apiKey without repeating them, bind the
handler to a client with createRequestHandler:
import { createPlatexClient, createRequestHandler } from '@nandan-varma/platex';
const platex = createPlatexClient({ timeout: 25_000, retry: 2 });export const POST = createRequestHandler(platex);Which import?
Section titled “Which import?”Use @nandan-varma/platex — includes the local-compile fallback.
import { handleCompileRequest } from '@nandan-varma/platex';export const runtime = 'nodejs';export const POST = handleCompileRequest;Use @nandan-varma/platex/client — remote-only, zero Node built-ins, safe to
bundle for the edge. Requires a serviceUrl.
export const runtime = 'edge';import { handleCompileRequest } from '@nandan-varma/platex/client';export const POST = handleCompileRequest;