Skip to content

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 RequestResponse function that works in every framework unmodified.

app/api/compile/route.ts (Next.js)
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
{
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.

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 }

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);

Use @nandan-varma/platex — includes the local-compile fallback.

import { handleCompileRequest } from '@nandan-varma/platex';
export const runtime = 'nodejs';
export const POST = handleCompileRequest;