JavaScript / TypeScript SDK
The official JS/TS SDK is published as docjet on npm. Zero runtime dependencies; requires Node.js 18+ (native fetch).
Install
Section titled “Install”npm install docjetRender a PDF
Section titled “Render a PDF”import { DocJetClient } from 'docjet';
const client = new DocJetClient({ apiKey: process.env.DOCJET_API_KEY! });
// Returns a signed download URLconst { url } = await client.render({ template_id: 'invoice-ro', data: { client: 'Acme SRL', total: 1500, currency: 'RON', },});
console.log('PDF ready:', url);Raw HTML instead of a template
Section titled “Raw HTML instead of a template”const { url } = await client.render({ html: '<h1>Hello World</h1><p>Generated by DocJet.</p>', data: { name: 'World' },});PNG images, templates, usage
Section titled “PNG images, templates, usage”// PNG (e.g. OG/social card) — dimensions come from the templateconst image = await client.renderImage({ template_id: 'og-blog', data: { title: 'My Article', author: 'Me' },});
// Public template catalogconst templates = await client.listTemplates();
// Current-period usageconst usage = await client.usage();Verify webhook signatures
Section titled “Verify webhook signatures”Every async DocJet callback is signed with an X-DocJet-Signature header. Verify it before trusting the payload — one call, timing-safe, with a 5-minute replay window:
import { verifyWebhookSignature } from 'docjet';
app.post('/webhooks/docjet', (req, res) => { const rawBody = req.body; // raw string BEFORE JSON.parse — critical! const signature = req.headers['x-docjet-signature'] as string; const secret = process.env.DOCJET_WEBHOOK_SECRET!;
if (!verifyWebhookSignature(rawBody, signature, secret)) { return res.status(401).json({ error: 'Invalid signature' }); }
const event = JSON.parse(rawBody); // now safe to trust res.json({ received: true });});Get your signing secret with GET /v1/keys/webhook-secret. Full scheme details: Webhooks & signatures.
Error handling
Section titled “Error handling”Non-2xx responses throw DocJetError with the API error code and HTTP status:
import { DocJetClient, DocJetError } from 'docjet';
try { const { url } = await client.render({ template_id: 'invoice-ro', data: {} });} catch (err) { if (err instanceof DocJetError) { console.error(`[${err.code}] HTTP ${err.statusCode}: ${err.message}`); // err.code: 'AUTH_INVALID' | 'QUOTA_EXCEEDED' | 'RATE_LIMITED' | ... } else { throw err; }}See the full taxonomy in Errors & limits.
Configuration
Section titled “Configuration”const client = new DocJetClient({ apiKey: 'binfra_...', // required baseUrl: 'https://api.docjet.dev', // optional, default shown});