FormaTeX

\begin{article}

Getting Started with FormaTeX

Learn how to compile LaTeX documents to PDF in seconds using the FormaTeX API — no TeX Live installation required.

·4 min read·
Getting Started with FormaTeX

FormaTeX gives you a REST API that accepts LaTeX source and returns a compiled PDF. No 4 GB TeX Live install, no version mismatches, no maintenance — just POST and receive.

This guide walks through creating an API key, compiling your first document, and handling errors correctly.

Prerequisites

You need:

  • A FormaTeX account (sign up free) — or try the Playground first without creating an account
  • An API key from the dashboard
  • curl or any HTTP client

Creating Your API Key

After signing up, navigate to Dashboard → API Keys and click New Key. Give it a name — my-first-key works — and copy the raw key shown immediately. It is displayed only once.

Your First Compilation

Send your LaTeX source as a JSON body with the latex field:

bash
curl -X POST https://api.formatex.io/api/v1/compile \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "latex": "\\documentclass{article}\\begin{document}Hello, FormaTeX!\\end{document}",
    "engine": "pdflatex"
  }' \
  --output hello.pdf

The response is a raw PDF binary written directly to hello.pdf. That is the entire integration for simple documents.

Two response headers are always included on success:

HeaderDescription
X-Job-IDUnique ID for this compilation job
X-Compile-Duration-MsServer-side compile time in milliseconds

Choosing an Engine

FormaTeX supports four LaTeX engines:

EngineBest for
pdflatexStandard documents, fastest
xelatexUnicode, custom fonts
lualatexLua scripting, advanced typography
latexmkMulti-pass documents with bibliography

The Free plan includes pdflatex. Pro, Max, and Enterprise plans unlock all four engines.

Handling Errors

When compilation fails, the API returns a 422 status with a JSON body containing the full TeX log:

json
{
  "success": false,
  "error": "compilation failed",
  "log": "! Undefined control sequence.\nl.5 \\badcommand\n...",
  "duration": 843,
  "diagnostics": []
}

The log field is the raw TeX compiler output — it tells you exactly where the error is. Common errors and fixes:

  • Undefined control sequence — typo in a command or missing \usepackage
  • File not found — multi-file documents need all sources included in the request
  • Overfull \hbox — warning only; the PDF still compiles and is returned
  • Unicode characters with pdflatex — switch to xelatex or use Smart Compile with engine: "auto"

Using TypeScript

Call the API directly with fetch:

typescript
const response = await fetch("https://api.formatex.io/api/v1/compile", {
  method: "POST",
  headers: {
    "X-API-Key": process.env.FORMATEX_API_KEY!,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    latex: latexSource,
    engine: "pdflatex",
  }),
});

if (!response.ok) {
  const error = await response.json();
  throw new Error(error.log ?? error.error);
}

const pdfBuffer = await response.arrayBuffer();

Tip: Store your API key in an environment variable, never in source code. The dashboard lets you set per-key expiration dates and revoke keys instantly.

Requesting a JSON Response

By default the API streams the raw PDF binary. If you prefer a JSON envelope (e.g., to embed the PDF as base64 in an API response), pass Accept: application/json:

bash
curl -X POST https://api.formatex.io/api/v1/compile \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"latex": "\\documentclass{article}\\begin{document}Hello\\end{document}", "engine": "pdflatex"}'

The JSON response looks like:

json
{
  "success": true,
  "pdf": "<base64-encoded PDF>",
  "jobId": "01J...",
  "engine": "pdflatex",
  "duration": 512,
  "sizeBytes": 14832,
  "diagnostics": []
}

Next Steps

  • Try the Playground — compile LaTeX instantly without creating an account
  • Browse the API reference for the full endpoint schema
  • Learn about Smart Compile — AI-powered error fixing for documents that fail to compile
  • Use async compilation for long-running documents with webhook callbacks
  • Explore engine comparisons to pick the right engine for your document
bash
# That's it. Ship PDFs.
curl -X POST https://api.formatex.io/api/v1/compile \
  -H "X-API-Key: $FORMATEX_KEY" \
  -d '{"latex":"...","engine":"pdflatex"}' \
  --output result.pdf

\end{article}

Back to blog

\related{posts}

One quick thing

We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.

Cookie policy