Skip to content

Quick start

This page gets you from install to a rendered PDF. It assumes you’ve deployed a service and set PLATEX_SERVICE_URL (see Installation) — or that you’re compiling locally, in which case you can leave serviceUrl unset entirely.

Section titled “The recommended pattern: one client, reused everywhere”

Create a client once with your defaults baked in, then import it anywhere. This keeps timeout, retry, apiKey, and friends in a single place.

  1. Create the client.

    lib/platex.ts
    import { createPlatexClient } from '@nandan-varma/platex';
    export const platex = createPlatexClient({
    timeout: 25_000,
    retry: 2, // retry transient network/5xx failures against the service
    // engine, passes, bibliography, limits, apiKey, headers... all optional
    });

    serviceUrl is read from PLATEX_SERVICE_URL automatically — you don’t need to pass it.

  2. Use it. createPlatexClient returns plain functions, so destructuring works — no this binding required.

    import { platex } from '@/lib/platex';
    const result = await platex.compile(source);
    if (result.pdf) {
    // result.pdf is a Buffer — return it, save it, stream it
    }
app/api/compile/route.ts
import { NextResponse } from 'next/server';
import { platex } from '@/lib/platex';
export const runtime = 'nodejs';
export const maxDuration = 30;
export async function POST(req: Request) {
const { source } = await req.json();
const result = await platex.compile(source);
if (!result.pdf) {
return NextResponse.json({ errors: result.errors }, { status: 422 });
}
return new NextResponse(result.pdf, {
headers: { 'Content-Type': 'application/pdf' },
});
}

Prefer the shell? The same pipeline ships as a CLI — no TeX installation required, thanks to the bundled Tectonic fallback:

Terminal window
npx @nandan-varma/platex main.tex # → main.pdf
platex main.tex --watch # recompile on every save
platex main.tex -s https://platex.example.com # compile via a remote service
platex main.tex --json | jq '.errors' # structured output