Generate Document

The generate document endpoint allows you to create a document by filling a template with your data.

Request

POST /v1/templates/{template_id}/generate

Path Parameters

ParameterTypeDescription
template_idstringThe unique identifier of the template to use

Request Body

{
  "data": object,
  "output": {
    "format": "docx" | "pdf",
    "filename": string,
    "email": EmailOptions
  }
}

Request Fields

FieldTypeDescription
dataobjectKey-value pairs where keys match template placeholders
outputobjectOptional. Output options
output.formatstringOptional. The desired output format: "docx" (default) or "pdf"
output.filenamestringOptional. The desired output filename. If not provided, the filename will be generated based on the template name.
output.emailEmailOptionsOptional. See details below.

Email Options

To send the document via email, use the following object structure:

{
  "output": {
    "email": {
      "to": string[],
      "cc": string[],
      "bcc": string[],
      "subject": string,
      "body": string
    }
  }
}

Email Options Fields

FieldTypeDescription
tostring[]Primary recipients
ccstring[]Optional. CC recipients
bccstring[]Optional. BCC recipients
subjectstringOptional. Email subject line to replace the default one
bodystringOptional. Text or HTML email body content to replace the default one

Response

A successful request returns the generated document with the appropriate Content-Type header:

  • application/vnd.openxmlformats-officedocument.wordprocessingml.document for DOCX
  • application/pdf for PDF

Examples

const apiKey = "Your_API_Key";
const templateId = "template_123";

const response = await fetch(`https://templatedocs.io/api/v1/templates/${templateId}/generate`, {
    method: 'POST',
    headers: {
        "Authorization": `Bearer ${apiKey}`,
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
	    data: {
	        CustomerName: "John Doe",
	        Items: [
	            {
	                Name: "Gadget",
	                Price: 100
	            },
	            {
	                Name: "Gizmo",
	                Price: 200
	            }
	        ]
	    },
	    output: {
	        format: "pdf",
	        email: {
	            to: [
	                "recipient1@example.com",
	                "recipient2@example.com"
	            ],
	            subject: "Your document is ready"
	        }
	    }
	})
});

const blob = await response.blob();

Next Steps