Back to API Reference

Word Templates API

Manage Word document templates for mail merge and document generation within the Office Add-in.

---

Base URL

https://app.datapublisher.io/api/word-templates

---

Authentication

All endpoints require JWT authentication via Authorization: Bearer header.

---

Overview

The Word Templates API allows you to:

Supported Operations:

---

Template Operations

Upload Word Template

Upload a .docx file as a template.

Endpoint: POST /upload

Headers:

Authorization: Bearer 

Content-Type: multipart/form-data

Form Data:

Request Example:

const formData = new FormData();

formData.append('template', templateFile);

formData.append('name', 'Sales Report Template');

formData.append('description', 'Quarterly sales report');

formData.append('category', 'reports');

formData.append('tags', 'sales,quarterly,report');

Response (200 OK):

{

"success": true,

"template": {

"id": "template-uuid",

"name": "Sales Report Template",

"description": "Quarterly sales report",

"fileName": "sales-report-template.docx",

"category": "reports",

"tags": ["sales", "quarterly", "report"],

"fileSize": 45678,

"uploadedAt": "2026-02-12T10:30:00Z"

},

"message": "Template uploaded successfully"

}

Error Responses:

400 Bad Request - Invalid file:
{

"error": "Only .docx files are allowed"

}

413 Payload Too Large - File too large:
{

"error": "File size exceeds 10MB limit"

}

---

List Templates

Get all Word templates for the user.

Endpoint: GET /

Headers:

Authorization: Bearer 

Query Parameters:

Response (200 OK):

{

"success": true,

"templates": [

{

"id": "template-uuid-1",

"name": "Sales Report Template",

"description": "Quarterly sales report",

"fileName": "sales-report-template.docx",

"category": "reports",

"tags": ["sales", "quarterly", "report"],

"fileSize": 45678,

"uploadedAt": "2026-02-12T10:30:00Z",

"lastUsedAt": "2026-02-12T11:00:00Z"

},

{

"id": "template-uuid-2",

"name": "Invoice Template",

"description": "Standard invoice format",

"fileName": "invoice-template.docx",

"category": "invoices",

"tags": ["invoice", "billing"],

"fileSize": 32000,

"uploadedAt": "2026-01-15T09:00:00Z",

"lastUsedAt": "2026-02-10T14:30:00Z"

}

],

"totalCount": 2

}

---

Get Template Details

Get detailed information about a specific template.

Endpoint: GET /:templateId

Headers:

Authorization: Bearer 

Path Parameters:

Response (200 OK):

{

"success": true,

"template": {

"id": "template-uuid",

"name": "Sales Report Template",

"description": "Quarterly sales report",

"fileName": "sales-report-template.docx",

"category": "reports",

"tags": ["sales", "quarterly", "report"],

"fileSize": 45678,

"filePath": "/templates/user-uuid/sales-report-template.docx",

"uploadedAt": "2026-02-12T10:30:00Z",

"lastUsedAt": "2026-02-12T11:00:00Z",

"usageCount": 15

}

}

---

Download Template

Download template .docx file.

Endpoint: GET /:templateId/download

Headers:

Authorization: Bearer 

Response (200 OK):

Usage:

const response = await fetch(https://app.datapublisher.io/api/word-templates/${templateId}/download, {

headers: { 'Authorization': Bearer ${jwtToken} }

});

const blob = await response.blob();

const url = window.URL.createObjectURL(blob);

const a = document.createElement('a');

a.href = url;

a.download = 'template.docx';

a.click();

---

Get Template OOXML

Extract OOXML content for Office.js insertion.

Endpoint: GET /:templateId/ooxml

Headers:

Authorization: Bearer 

Response (200 OK):

{

"success": true,

"templateId": "template-uuid",

"ooxml": "...",

"sections": [

{

"type": "header",

"ooxml": "..."

},

{

"type": "body",

"ooxml": "..."

},

{

"type": "footer",

"ooxml": "..."

}

]

}

Usage with Office.js:

// Insert template OOXML into current document

const response = await fetch(https://app.datapublisher.io/api/word-templates/${templateId}/ooxml, {

headers: { 'Authorization': Bearer ${jwtToken} }

});

const { ooxml } = await response.json();

await Word.run(async (context) => {

const body = context.document.body;

body.insertOoxml(ooxml, Word.InsertLocation.replace);

await context.sync();

});

---

Delete Template

Remove a template from the library.

Endpoint: DELETE /:templateId

Headers:

Authorization: Bearer 

Response (200 OK):

{

"success": true,

"message": "Template deleted successfully"

}

---

Save Current Document

Save Document as Template

Save the current Word document as a reusable template.

Endpoint: POST /from-document

Headers:

Authorization: Bearer 

Content-Type: application/json

Request Body:

{

"name": "My Custom Template",

"description": "Created from current document",

"category": "custom",

"tags": ["custom", "letter"],

"documentOoxml": "..."

}

Response (200 OK):

{

"success": true,

"template": {

"id": "template-uuid",

"name": "My Custom Template",

"fileName": "my-custom-template.docx",

"fileSize": 38000,

"createdAt": "2026-02-12T11:00:00Z"

},

"message": "Template saved successfully"

}

Office.js Implementation:

// Get current document OOXML

await Word.run(async (context) => {

const body = context.document.body;

const ooxml = body.getOoxml();

await context.sync();

// Save as template

const response = await fetch('https://app.datapublisher.io/api/word-templates/from-document', {

method: 'POST',

headers: {

'Authorization': Bearer ${jwtToken},

'Content-Type': 'application/json'

},

body: JSON.stringify({

name: 'My Template',

description: 'From current doc',

documentOoxml: ooxml.value

})

});

});

---

Template Categories

Pre-defined categories:

---

File Specifications

---

Rate Limiting

---

Common Error Codes

| Status Code | Description |

|-------------|-------------|

| 400 | Invalid file format or missing fields |

| 401 | Unauthorized - invalid JWT |

| 404 | Template not found |

| 413 | File size exceeds 10MB limit |

| 500 | Template processing failed |

---

Usage Examples

JavaScript (Office Add-in)

// Upload template

async function uploadTemplate(file, name) {

const formData = new FormData();

formData.append('template', file);

formData.append('name', name);

formData.append('category', 'reports');

const response = await fetch('https://app.datapublisher.io/api/word-templates/upload', {

method: 'POST',

headers: { 'Authorization': Bearer ${jwtToken} },

body: formData

});

return await response.json();

}

// List templates

async function listTemplates() {

const response = await fetch('https://app.datapublisher.io/api/word-templates', {

headers: { 'Authorization': Bearer ${jwtToken} }

});

const { templates } = await response.json();

return templates;

}

// Load template into document

async function loadTemplate(templateId) {

// Get OOXML

const response = await fetch(https://app.datapublisher.io/api/word-templates/${templateId}/ooxml, {

headers: { 'Authorization': Bearer ${jwtToken} }

});

const { ooxml } = await response.json();

// Insert into Word

await Word.run(async (context) => {

const body = context.document.body;

body.insertOoxml(ooxml, Word.InsertLocation.replace);

await context.sync();

});

}

// Save current document as template

async function saveAsTemplate(name, description) {

const ooxml = await Word.run(async (context) => {

const body = context.document.body;

const ooxmlObj = body.getOoxml();

await context.sync();

return ooxmlObj.value;

});

const response = await fetch('https://app.datapublisher.io/api/word-templates/from-document', {

method: 'POST',

headers: {

'Authorization': Bearer ${jwtToken},

'Content-Type': 'application/json'

},

body: JSON.stringify({

name: name,

description: description,

documentOoxml: ooxml

})

});

return await response.json();

}

---

Best Practices

  • Template Design: Use placeholders (e.g., {{CustomerName}}) for merge fields
  • File Size: Keep templates under 5MB for faster loading
  • Categories: Organize templates into logical categories
  • Tags: Use descriptive tags for easy searching
  • OOXML: Cache OOXML for frequently used templates
  • Testing: Test templates before sharing
  • Backup: Keep local copies of important templates
  • ---

    Related Documentation