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",
    "email": boolean | {
      "subject": string,
      "body": string
    }
  }
}

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.emailboolean | objectOptional. Whether to send the document as an attachment via email. If true, the document will be sent to your email address. If an object is provided, the email will be sent with the subject and body specified in the object.

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: {
	        firstName: "John",
	        lastName: "Doe",
	        company: "ACME Inc."
	    },
	    output: {
	        format: "pdf"
	    }
	})
});

const blob = await response.blob();