Skip to content

Official TemplateFox TypeScript SDK - Generate PDFs from HTML templates via API

License

Notifications You must be signed in to change notification settings

TemplateFoxPDF/typescriptsdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TemplateFox TypeScript SDK

Official TypeScript/Node.js SDK for TemplateFox - Generate PDFs from HTML templates via API.

npm version License: MIT

Installation

npm install @templatefox/sdk

Or with yarn:

yarn add @templatefox/sdk

Quick Start

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();

Features

  • Template-based PDF generation - Create templates with dynamic variables, generate PDFs with your data
  • Multiple export options - Get a signed URL (default) or raw binary PDF
  • S3 integration - Upload generated PDFs directly to your own S3-compatible storage
  • TypeScript support - Full type definitions included

API Methods

PDF Generation

// Generate PDF and get URL
const response = await api.createPdf({
  templateId: 'TEMPLATE_ID',
  data: { name: 'John Doe' },
  exportType: 'url',        // 'url' or 'binary'
  expiration: 86400,        // URL expiration in seconds (default: 24h)
  filename: 'invoice-001',  // Custom filename
});

Templates

import { TemplatesApi } from '@templatefox/sdk';

const templatesApi = new TemplatesApi(config);

// List all templates
const templates = await templatesApi.listTemplates();

// Get template fields
const fields = await templatesApi.getTemplateFields({ templateId: 'TEMPLATE_ID' });

Account

import { AccountApi } from '@templatefox/sdk';

const accountApi = new AccountApi(config);

// Get account info
const account = await accountApi.getAccount();
console.log('Credits:', account.credits);

// List transactions
const transactions = await accountApi.listTransactions({ limit: 100, offset: 0 });

S3 Integration

import { IntegrationsApi } from '@templatefox/sdk';

const integrationsApi = new IntegrationsApi(config);

// Save S3 configuration
await integrationsApi.saveS3Config({
  s3ConfigRequest: {
    endpointUrl: 'https://s3.amazonaws.com',
    accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
    secretAccessKey: 'your-secret-key',
    bucketName: 'my-pdf-bucket',
    defaultPrefix: 'generated/pdfs/',
  }
});

// Test connection
const test = await integrationsApi.testS3Connection();
console.log('Connection:', test.success ? 'OK' : 'Failed');

Configuration Options

const config = new Configuration({
  basePath: 'https://api.pdftemplateapi.com', // Default API URL
  apiKey: 'your-api-key',
});

// Or use environment variable
const config = new Configuration({
  apiKey: process.env.TEMPLATEFOX_API_KEY,
});

Error Handling

try {
  const response = await api.createPdf({ /* ... */ });
} catch (error) {
  if (error.status === 402) {
    console.error('Insufficient credits');
  } else if (error.status === 403) {
    console.error('Access denied - check your API key');
  } else if (error.status === 404) {
    console.error('Template not found');
  } else {
    console.error('Error:', error.message);
  }
}

Documentation

Support

License

MIT License - see LICENSE for details.