docstron
v1.0.3
Published
Node.js SDK for Docstron PDF Generation API - Complete Features
Maintainers
Readme
Docstron SDK for Node.js
The official Node.js library for the Docstron PDF Generation API.
📋 What's Included
✅ Template Management
- Create templates with HTML content
- Get template details
- Update existing templates
- Delete templates
- List all templates
Installation
npm install docstronConfiguration
Add your API key to your environment:
DOCSTRON_API_KEY=your-api-key
DOCSTRON_BASE_URL=https://api.docstron.com/v1Quick Start
const Docstron = require("docstron");
// Initialize the client
const client = new Docstron();
// Create a template
const template = await client.templates.create({
application_id: "app-xxx",
name: "Invoice Template",
content: "<h1>Invoice #{{invoice_number}}</h1><p>Total: {{total}}</p>",
is_active: true,
});
console.log("Template created:", template.template_id);Authentication
Get your API key from your Docstron Dashboard.
const client = new Docstron();
// With custom options
const client = new Docstron("your-api-key-here", {
baseURL: "https://api.docstron.com/v1",
});API Reference
Templates
templates.create(params)
Create a new PDF template.
Parameters:
application_id(string, required) - Your application IDname(string, required) - Template namecontent(string, required) - HTML content with{{placeholders}}is_active(boolean, optional) - Active status (default:true)extra_css(string, optional) - Additional CSS rules for PDF styling
Returns: Promise
Example:
const template = await client.templates.create({
application_id: "app-7b4d78fb-820c-4ca9-84cc-46953f211234",
name: "Invoice Template",
content: `
<html>
<body>
<h1>Invoice</h1>
<p>Customer: {{customer_name}}</p>
<p>Total: {{total_amount}}</p>
</body>
</html>
`,
is_active: true,
extra_css: "@page { margin: 2cm; }",
});templates.get(templateId)
Retrieve a specific template by ID.
Parameters:
templateId(string, required) - The template ID
Returns: Promise
Example:
const template = await client.templates.get("template-xxx");
console.log(template.name);templates.update(templateId, params)
Update an existing template.
Parameters:
templateId(string, required) - The template IDparams(object, required) - Fields to update
Returns: Promise
Example:
const updated = await client.templates.update("template-xxx", {
name: "Updated Invoice Template",
is_active: false,
});templates.delete(templateId)
Delete a template.
Parameters:
templateId(string, required) - The template ID
Returns: Promise
Example:
await client.templates.delete("template-xxx");
console.log("Template deleted");templates.list(applicationId)
List all templates for an application.
Parameters:
applicationId(string, required) - The application ID
Returns: Promise
Example:
const templates = await client.templates.list("app-xxx");
console.log(`Found ${templates.length} templates`);Documents (v0.2.0+)
documents.generate(templateId, options)
Generate a PDF from a template.
Parameters:
templateId(string, required) - Template IDoptions.data(object, required) - Data for placeholdersoptions.response_type(string, optional) -'pdf','json_with_base64', or'document_id'(default)options.password(string, optional) - Password protect the PDF
Returns: Document object or PDF buffer
Example:
const doc = await client.documents.generate(templateId, {
data: {
customer_name: "John Doe",
invoice_number: "INV-001",
total: "$299.00",
},
response_type: "document_id",
});
console.log("Download URL:", doc.download_url);documents.quickGenerate(options)
Generate a PDF without creating a template first.
Parameters:
options.html(string, required) - HTML contentoptions.data(object, required) - Data for placeholdersoptions.response_type(string, optional) - Response formatoptions.extra_css(string, optional) - Additional CSSoptions.save_template(boolean, optional) - Save as reusable templateoptions.application_id(string, conditional) - Required if save_template is trueoptions.password(string, optional) - Password protect the PDF
Example:
const doc = await client.documents.quickGenerate({
html: "<h1>Hello {{name}}</h1>",
data: { name: "World" },
response_type: "document_id",
});documents.get(documentId)
Get document details.
Example:
const doc = await client.documents.get("document-xxx");
console.log(doc.template_id, doc.created_at);documents.list()
List all documents.
Example:
const docs = await client.documents.list();
console.log(`Total documents: ${docs.length}`);documents.download(documentId, filepath)
Download a PDF document.
Parameters:
documentId(string, required) - Document IDfilepath(string, optional) - Local path to save the PDF
Example:
// Save to file
await client.documents.download("document-xxx", "./invoice.pdf");
// Get as buffer
const buffer = await client.documents.download("document-xxx");documents.update(documentId, data)
Update document data.
documents.delete(documentId)
Delete a document.
Applications
applications.create(params)
Create a new application to organize your templates and documents.
Parameters:
params.name(string, required) - Application nameparams.description(string, optional) - Application descriptionparams.is_active(boolean, optional) - Active status (default:true)
Returns: Promise
Example:
const app = await client.applications.create({
name: "Invoice System",
description: "Manages all customer invoices",
is_active: true,
});
console.log("App ID:", app.app_id);applications.get(appId)
Get details of a specific application.
Example:
const app = await client.applications.get("app-xxx");
console.log(app.name, app.is_active);applications.list()
List all applications in your account.
Example:
const apps = await client.applications.list();
console.log(`Total applications: ${apps.length}`);
apps.forEach((app) => {
console.log(`- ${app.name} (${app.is_active ? "Active" : "Inactive"})`);
});applications.update(appId, params)
Update an existing application.
Parameters:
appId(string, required) - Application IDparams.name(string, optional) - New nameparams.description(string, optional) - New descriptionparams.is_active(boolean, optional) - New active status
Example:
const updated = await client.applications.update("app-xxx", {
name: "Updated Invoice System",
is_active: false,
});applications.delete(appId)
Delete an application.
Example:
await client.applications.delete("app-xxx");
console.log("Application deleted");Response Types
When generating documents, you can choose the response format:
document_id (default)
Returns document ID and download URL - best for most use cases.
{
document_id: "document-xxx",
template_id: "template-xxx",
download_url: "https://api.docstron.com/v1/documents/download/document-xxx",
size_bytes: 5370
}json_with_base64
Returns base64-encoded PDF content - useful for immediate processing.
{
document_id: "document-xxx",
pdf_content: "JVBERi0xLjcK...",
filename: "document.pdf",
size_bytes: 5370
}pdf
Returns raw PDF binary data - useful for direct download endpoints.
Template Placeholders
Use double curly braces to create dynamic placeholders:
<h1>Hello {{customer_name}}!</h1>
<p>Order #{{order_number}} - Total: {{total_amount}}</p>
<p>Date: {{order_date}}</p>When generating PDFs, you'll pass data to fill these placeholders:
{
customer_name: "John Doe",
order_number: "12345",
total_amount: "$299.00",
order_date: "2025-11-08"
}Error Handling
The SDK provides specific error types for better error handling:
const {
DocstronError,
ValidationError,
AuthenticationError,
NotFoundError
} = require('docstron');
try {
await client.templates.create({...});
} catch (error) {
if (error instanceof ValidationError) {
console.error('Validation failed:');
error.errors.forEach(err => {
console.error(`- ${err.field}: ${err.message}`);
});
} else if (error instanceof AuthenticationError) {
console.error('Invalid API key');
} else if (error instanceof NotFoundError) {
console.error('Template or application not found');
} else if (error instanceof DocstronError) {
console.error('API error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}Requirements
- Node.js >= 14.0.0
- A Docstron account and API key
Support
- 📧 Email: [email protected]
- 📚 Documentation: https://docs.docstron.com
- 🐛 Issues: https://github.com/playiiit/docstron-nodejs-sdk/issues
- 💬 Discussions: https://github.com/playiiit/docstron-nodejs-sdk/discussions
Contributors
Contributing
Contributions are welcome! This is an early-stage project, and we'd love your help making it better.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
