Typcraft
High-performance document generation engine powered by Rust & Typst

Overview
Typcraft is an API-first document generation platform. Users design templates in a visual drag-and-drop editor, then generate thousands of personalized documents — invoices, certificates, reports, marketing materials — via a REST API. The engine is powered by Rust and Typst, a modern typesetting system that produces high-fidelity output at native speed.
Existing document tools are either too complex for non-developers or too slow for high-volume use cases. Typcraft bridges no-code design with native-speed generation. One credit equals one page, scaling from hobby projects generating a handful of PDFs to enterprise batch processing thousands of documents per minute.
Built for developers integrating document generation into their products, and businesses automating paperwork at scale. The platform includes subscription billing, API key management, a template marketplace, and an admin dashboard — everything needed to run a production SaaS.
Technical Highlights
Polyglot Monorepo: The codebase spans Rust and TypeScript, managed by Moonrepo. A Cargo workspace handles the API server, database entities, migrations, and a WASM crate. A pnpm workspace manages the Next.js 16 frontend and a shared TypeScript schema package. This shared schema ensures type safety between the visual editor and the Rust transpiler — the same document types are used on both sides of the stack.
Document Transpiler: A custom engine converts the visual editor's block-based JSON into valid Typst markup. It handles 15+ block types — text, tables, charts, barcodes, QR codes, diagrams — along with nested containers, data binding via template variables, conditional rendering, and iteration loops for batch personalization. The transpiler is the core of the system, responsible for producing pixel-perfect output from a visual design.
Hot Redis / Cold Postgres Billing: Credit operations run at sub-millisecond latency through Redis, using Lua scripts for atomic multi-key transactions. PostgreSQL serves as the durable ledger for audit and compliance. A background outbox processor reconciles the two stores every 5 seconds. Credits are reserved before job submission and committed after completion — ensuring atomicity even when generation jobs fail mid-process.
WASM Preview Engine: A custom Rust module compiled to WebAssembly runs text measurement algorithms directly in the browser. This provides instant preview rendering without server roundtrips, and the measurements match the exact output the Typst engine produces on the backend — so what users see in the editor is what they get in the generated document.
Key Features
- Visual Editor: Drag-and-drop template designer with 15+ block types including text, image, table, chart, shape, icon, QR code, barcode, and diagrams
- REST API: Programmatic document generation with API key authentication, scoped permissions, and rate limiting
- Multi-Format Output: Generate PDF, PNG, and SVG from a single template definition
- Template Marketplace: Versioned templates with changelogs and a public marketplace for sharing and discovery
- Data Binding: Template variables with iteration support for batch personalization across thousands of documents
- Credit-Based Billing: Subscription plans and pay-as-you-go credit packs powered by Lemon Squeezy
- AI Generation: Document creation powered by Claude and GPT models for rapid template prototyping
- Custom Fonts: Upload and manage custom font families with S3 storage and parallel loading during generation
- Multi-Tenant Organizations: Team workspaces with role-based access control (owner, admin, member)
- Admin Dashboard: User management, billing controls, font uploads, and template curation for platform operators
Challenges & Solutions
JSON → Typst Transpiler: The visual editor produces block-based JSON, but the Typst engine expects markup. Converting between the two required handling 15+ block types — each with unique rendering rules — plus nested containers, data binding expressions, and conditional logic. Built a recursive transpiler with block-specific handlers and a template variable resolver that walks the document tree and emits valid Typst markup.
Credit Atomicity: Generation jobs can fail mid-process (network errors, invalid templates, resource limits), but credits must never be lost or double-charged. Implemented a reserve → process → commit/refund pattern. Credits are atomically reserved in Redis before job submission. On success, they're committed to the Postgres ledger. On failure, the reservation is released. Lua scripts ensure the multi-key Redis operations cannot be interrupted.
Parallel Font Loading: Documents can reference multiple custom fonts stored in S3. Loading them sequentially added seconds to generation time for font-heavy templates. Switched to parallel async font fetching with Tokio, loading all required font variants concurrently before compilation begins — cutting font load time from seconds to milliseconds.
Two-Tier Reverse Proxy: Production routing needs to split traffic between Next.js (pages, NextAuth sessions) and the Rust API (document generation with 120-second timeouts). Designed a two-tier Nginx architecture — an outer router handles SSL termination via Cloudflare origin certificates, while an inner project-level proxy handles path-based routing with different timeout and buffer configurations per upstream.
Polyglot Build Pipeline: Rust and TypeScript have fundamentally different build toolchains. The CI pipeline uses GitHub Actions with cargo-chef for Rust dependency caching and pnpm for TypeScript, building both into optimized multi-stage Docker images. Images are pushed to GitHub Container Registry and auto-deployed to the VPS via SSH — a single push to main triggers the full build-test-deploy cycle.
WASM Text Measurement: The browser preview must match server-side Typst output exactly — if text wraps differently in the editor than in the generated PDF, users lose trust. Built a minimal WASM module that runs the same Typst text measurement algorithms in the browser, ensuring pixel-accurate preview without server roundtrips.
Tech Stack
Rust
Backend API and document engine
Axum
Async HTTP framework
Typst
Document compilation (PDF/PNG/SVG)
Next.js 16
Frontend framework, SSR, app router
React 19
UI with server components
TypeScript
Type safety across the full stack
PostgreSQL
Primary database (SeaORM)
Redis
Job queue, credit cache, session store
WebAssembly
Browser-side text measurement
Docker
Containerized deployment
Nginx
Two-tier reverse proxy
Lemon Squeezy
Subscription and credit billing
Apalis
Background job processing
MinIO / S3
Object storage for documents and fonts
GitHub Actions
CI/CD pipeline
Moonrepo
Polyglot monorepo management
What I Learned
- Building a production SaaS from scratch — billing, authentication, background jobs, admin tooling, and infrastructure that serves real users
- Designing a document transpiler that maps visual editor concepts to typesetting primitives — understanding layout engines at a deep level
- Hybrid billing architectures where Redis handles hot-path performance and Postgres ensures durability and auditability
- Managing a polyglot monorepo where Rust and TypeScript codebases share types and build in a unified CI/CD pipeline
- Production deployment on VPS with Docker, Nginx, Cloudflare, and automated deploys — understanding every layer of the stack from DNS to container orchestration