\section{Installation}
No package to install
FormaTeX is a plain HTTP API. Node 18+ ships with native fetch. For older Node versions, install node-fetch.
npm install node-fetch # or use built-in fetch (Node 18+)
# No SDK required — use the REST API directly\section{Quick start}
Compile your first PDF
Call the /v1/compile/sync endpoint with your LaTeX source. The response is a binary PDF you can write directly to disk or stream to the client.
const API_KEY = process.env.FORMATEX_API_KEY!;
const API_URL = "https://api.formatex.io/v1";
const latexSource = `
\\documentclass{article}
\\usepackage{amsmath}
\\begin{document}
Hello from Node.js! $E = mc^2$
\\end{document}
`;
const response = await fetch(`${API_URL}/compile/sync`, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
source: latexSource,
engine: "pdflatex",
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Compilation failed: ${error.error}`);
}
const pdfBuffer = Buffer.from(await response.arrayBuffer());
await fs.writeFile("output.pdf", pdfBuffer);\section{Environment variables}
Storing your API key
Never hardcode your API key. Store it in an environment variable and load it at runtime.
Shell / .env
dotenv package
Read in code
\section{Express integration}
Express route example
Expose a POST /generate-pdf endpoint that accepts LaTeX source from a request body and streams the compiled PDF directly back to the caller.
import express from "express";
import { writeFile } from "fs/promises";
const app = express();
app.use(express.json());
app.post("/generate-pdf", async (req, res) => {
const { latexSource, engine = "pdflatex" } = req.body;
const response = await fetch("https://api.formatex.io/v1/compile/sync", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FORMATEX_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ source: latexSource, engine }),
});
if (!response.ok) {
const err = await response.json();
return res.status(500).json({ error: err.error });
}
const pdfBuffer = Buffer.from(await response.arrayBuffer());
res.setHeader("Content-Type", "application/pdf");
res.setHeader("Content-Disposition", "attachment; filename=output.pdf");
res.send(pdfBuffer);
});
app.listen(3000);\section{Cloud storage}
Upload compiled PDFs to S3
Compile and upload to AWS S3 in a single function. The same pattern works for any S3-compatible storage — R2, GCS, DigitalOcean Spaces, and MinIO.
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: "us-east-1" });
async function compileAndUpload(latex: string, key: string): Promise<string> {
const response = await fetch("https://api.formatex.io/v1/compile/sync", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FORMATEX_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ source: latex, engine: "pdflatex" }),
});
if (!response.ok) throw new Error("Compilation failed");
const pdfBuffer = Buffer.from(await response.arrayBuffer());
await s3.send(new PutObjectCommand({
Bucket: "my-bucket",
Key: key,
Body: pdfBuffer,
ContentType: "application/pdf",
}));
return `https://my-bucket.s3.amazonaws.com/${key}`;
}\section{API reference}
Endpoint reference
| Field | Type | Required | Description |
|---|---|---|---|
| source | string | Yes | Full LaTeX document source |
| engine | string | No | pdflatex (default), xelatex, or lualatex |
\section{Other integrations}
FormaTeX from any language
FormaTeX is a plain HTTP API. Copy-paste examples for every major language.
