> ## Documentation Index
> Fetch the complete documentation index at: https://developers.luminpdf.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate Document from Template

> Creates a downloadable document from an existing Lumin template.

<Accordion title="Required scopes (OAuth 2.0)" icon="key">
  This endpoint requires the following scope:

  `templates`
</Accordion>


## OpenAPI

````yaml POST /templates/{template_id}/generate-document
openapi: 3.1.0
info:
  version: 1.0.0
  title: Lumin API Reference
  description: >
    The Lumin API Reference provides a comprehensive set of tools to integrate
    document workflows — including editing, eSignatures, and automation — into
    your applications.


    Useful links:

    - [Document Repository](https://github.com/luminpdf/luminsign-docs)

    - [API
    Definition](https://github.com/luminpdf/luminsign-docs/blob/main/openapi.yaml)

    - [Authentication Guide](/tabs/guides/authentication/overview)
  termsOfService: https://www.luminpdf.com/terms-of-use/
  contact:
    name: API Support
    email: integration@luminpdf.com
    url: https://help.luminpdf.com
servers:
  - url: https://api.luminpdf.com/v1
    description: Production server
security: []
tags:
  - name: Signature Requests
    description: Everything about Signature Requests
  - name: Users
    description: Everything about Users
  - name: Templates
    description: Everything about Templates
  - name: Documents
    description: Everything about Documents
  - name: Workspaces
    description: Everything about Workspaces
  - name: Agreements
    description: Everything about Agreements
paths:
  /templates/{template_id}/generate-document:
    post:
      tags:
        - Templates
      summary: Generate Document from Template
      description: >
        Creates a downloadable PDF document from an existing Lumin template.


        If a template contains **required tags or fields**, you must include
        these tags and fields in the request payload along with their
        corresponding values to generate the PDF document.
      parameters:
        - in: path
          name: template_id
          schema:
            type: string
          required: true
          description: ID of the template to generate document from.
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                tags:
                  type: object
                  description: >
                    Key–value pairs for **Merge Tags** defined in the Sign
                    template. Keys must match tag names. Values replace the
                    corresponding tags and are rendered as plain text in the
                    generated document.
                fields:
                  type: object
                  description: >
                    Key–value pairs for **Form Fields** defined in the template.
                    Keys must match field names. Values prefill the
                    corresponding fields in the generated document.
                variables:
                  type: object
                  description: >
                    Key–value pairs for **Variables** defined in the
                    AgreementGen template. Keys must match variable names.
                    Values prefill the corresponding variables and are rendered
                    as plain text in the generated document.
                collections:
                  type: object
                  description: >
                    Map of **collection name → array of record objects** used to
                    expand table-scoped row-loop markers in the template.


                    - Each key must match a collection name returned by [Get
                    Template
                    Details](/tabs/api-reference/api/templates/get-template-details)
                    under `collections[].name`.

                    - Each value is an ordered array of flat record objects;
                    each record is a key–value map where keys match the
                    collection's variable names and values are strings.

                    - Records are rendered in array order.

                    - Maximum 100 items per collection, and up to 50 collections
                    per request.


                    Applies to AgreementGen templates only. Ignored silently
                    when the template `type` is not `lumin`.
                  additionalProperties:
                    type: array
                    items:
                      type: object
                      additionalProperties:
                        type: string
                document_name:
                  type: string
                  description: >-
                    Optional name for the generated document. If omitted, the
                    document name defaults to the template's name.
            example:
              tags:
                Client.Name: Acme Corp
                Effective.Date: '2025-08-01'
              fields:
                CustomerName: John Doe
                AgreeToTerms: true
              variables:
                Company.Name: ACME Corp
                Document.Name: NDA Document
              collections:
                Opportunity.LineItems:
                  - Product.Name: Annual enterprise license
                    Product.Quantity: '1'
                    Product.UnitPrice: $1,000.00
                    Product.Total: $1,000.00
                  - Product.Name: Premium support package
                    Product.Quantity: '1'
                    Product.UnitPrice: $500.00
                    Product.Total: $500.00
              document_name: My Contract
      responses:
        '200':
          description: Returns the generated document.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DocumentGenerateResponse'
              example:
                document_name: NDA_AcmeCorp
                signed_url: >-
                  https://files.luminpdf.com/download/nda-acmecorp-abc123.pdf?expires=2025-08-24T10:45:00Z
                expires_at: 1766726700
            application/pdf:
              schema:
                type: string
                format: binary
        4XX:
          description: Unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - ApiKey: []
        - BearerAuth:
            - templates
components:
  schemas:
    DocumentGenerateResponse:
      type: object
      required:
        - document_name
        - signed_url
        - expires_at
      properties:
        document_name:
          type: string
          description: Name of the generated document (from input or default).
        signed_url:
          type: string
          format: uri
          description: >-
            Signed HTTPS URL to download the generated document. Expires in 30
            minutes.
        expires_at:
          type: integer
          format: unix-epoch
          description: >-
            Unix epoch timestamp (in seconds) indicating when signed_url will no
            longer work.
    Error:
      type: object
      required:
        - error_code
        - error_message
      properties:
        error_code:
          type: string
          description: The system error code.
        error_message:
          type: string
          description: The human-readable error message.
  securitySchemes:
    ApiKey:
      type: apiKey
      in: header
      name: X-API-Key
      description: >
        Provide your API key in the `X-API-Key` header, e.g., `X-API-Key:
        YOUR_API_KEY`.
    BearerAuth:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: https://auth.luminpdf.com/oauth2/auth
          tokenUrl: https://auth.luminpdf.com/oauth2/token
          scopes:
            openid: >-
              Retrieve basic identity details (username, email, profile
              picture).
            offline_access: Request a refresh token for long-lived access. Private apps only.
            profile.read: View basic user profile information.
            workspaces: View and manage Workspaces and Spaces.
            workspaces.read: View information about the authenticated user's Workspace.
            templates: View and manage templates in a Workspace.
            pdf:files: Create, edit, and delete PDF files in a Workspace.
            pdf:files.read: Retrieve PDF documents stored in a Workspace.
            sign:requests: Create, update, or view signature requests.
            sign:requests.read: Retrieve signature requests.
            agreements: Create, update, or delete AgreementGen documents.
      description: >
        OAuth 2.0 authorization code flow. Provide your access token in the
        `Authorization` header, e.g., `Authorization: Bearer <token>`. See the
        [OAuth 2.0 guide](/tabs/guides/authentication/oauth2) for details.

````