TypeScript
Official TypeScript/Node.js SDK for TemplateFox.
Installation
npm install @templatefox/sdkOr with yarn:
yarn add @templatefox/sdkGenerate a PDF
import { Configuration, PDFApi, CreatePdfRequest } from '@templatefox/sdk';
// Initialize the client
const config = new Configuration({
apiKey: 'your-api-key',
});
const api = new PDFApi(config);
// Generate a PDF
async function generatePdf() {
const response = await api.createPdf({
templateId: 'YOUR_TEMPLATE_ID',
data: {
name: 'John Doe',
invoiceNumber: 'INV-001',
totalAmount: 150.00,
},
});
console.log('PDF URL:', response.url);
console.log('Credits remaining:', response.creditsRemaining);
}
generatePdf();Save PDF to File
import * as fs from 'fs';
const response = await api.createPdf({
templateId: 'YOUR_TEMPLATE_ID',
data: { name: 'Jane Doe', invoiceNumber: 'INV-042' },
});
// Download and save locally
const pdfResponse = await fetch(response.url);
const buffer = Buffer.from(await pdfResponse.arrayBuffer());
fs.writeFileSync('invoice.pdf', buffer);Error Handling
import { ResponseError } from '@templatefox/sdk';
try {
const response = await api.createPdf({ templateId: 'INVALID_ID', data: {} });
} catch (error) {
if (error instanceof ResponseError) {
console.error(`Status: ${error.response.status}`);
const body = await error.response.json();
console.error(`Message: ${body.detail}`);
}
}Available Methods
The SDK covers all TemplateFox API endpoints through four service classes:
| Class | Method | Description |
|---|---|---|
PDFApi | createPdf | Generate a PDF from a template |
TemplatesApi | listTemplates | List all templates in your team |
TemplatesApi | getTemplateFields | Get data fields for a template |
AccountApi | getAccount | Get account info and credit balance |
AccountApi | listTransactions | List credit transaction history |
IntegrationsApi | saveS3Config | Configure S3 storage integration |
IntegrationsApi | getS3Config | Get current S3 configuration |
IntegrationsApi | deleteS3Config | Remove S3 configuration |
IntegrationsApi | testS3Connection | Test S3 bucket connectivity |
Configuration
const config = new Configuration({
apiKey: process.env.TEMPLATEFOX_API_KEY,
});Quick Testing
Want to test the API before integrating the SDK? Use our Postman collection to try all endpoints interactively.
Full SDK Reference
For the complete API coverage including templates, account, S3 integration, and error handling, see the GitHub repository README .
Last updated on