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

# Generate document from template (async)

> Initiates an asynchronous process to generate a document from a template slug and relevant data (documentBody for document, variables for PDF). Returns an ID for tracking.



## OpenAPI

````yaml https://app.stainless.com/api/spec/documented/samplehc/openapi.documented.yml post /api/v2/documents/templates/generate-document
openapi: 3.0.3
info:
  title: Sample Healthcare API
  description: API for Sample Healthcare
  version: 1.0.0
servers: []
security: []
paths:
  /api/v2/documents/templates/generate-document:
    post:
      tags:
        - Document
      summary: Generate document from template (async)
      description: >-
        Initiates an asynchronous process to generate a document from a template
        slug and relevant data (documentBody for document, variables for PDF).
        Returns an ID for tracking.
      requestBody:
        content:
          application/json:
            schema:
              anyOf:
                - type: object
                  properties:
                    type:
                      type: string
                      enum:
                        - document
                    slug:
                      type: string
                      description: The slug of the template to use.
                    documentBody:
                      description: The body of the document.
                    fileName:
                      type: string
                      description: The name of the file to save.
                  required:
                    - type
                    - slug
                  additionalProperties: false
                - type: object
                  properties:
                    type:
                      type: string
                      enum:
                        - pdf
                    slug:
                      type: string
                      description: The slug of the template to use.
                    variables:
                      type: object
                      additionalProperties:
                        anyOf:
                          - type: string
                          - type: number
                          - type: boolean
                          - type: array
                            items:
                              type: object
                              additionalProperties:
                                anyOf:
                                  - type: string
                                  - type: number
                      description: >-
                        The variables to use in the template. Arrays will be
                        converted to text representation for PDF form fields.
                    fileName:
                      type: string
                      description: The name of the file to save.
                  required:
                    - type
                    - slug
                    - variables
                  additionalProperties: false
              description: >-
                The request body, discriminated by the 'type' field ('document'
                or 'pdf').
        description: >-
          The request body, discriminated by the 'type' field ('document' or
          'pdf').
      responses:
        '202':
          description: Default Response
          content:
            application/json:
              schema:
                type: object
                properties:
                  asyncResultId:
                    type: string
                    description: The ID to track the asynchronous document generation task.
                required:
                  - asyncResultId
                additionalProperties: false
        '400':
          description: Default Response
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    description: The error message.
                  message:
                    type: string
                    description: The message.
                  statusCode:
                    type: number
                    description: The status code.
                  details:
                    type: object
                    additionalProperties: {}
                    description: The details of the error
                additionalProperties: true
        '404':
          description: The template was not found.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
        '500':
          description: An internal server error occurred.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
      x-codeSamples:
        - lang: Python
          source: |-
            import os
            from samplehc import SampleHealthcare

            client = SampleHealthcare(
                api_key=os.environ.get("SAMPLEHC_API_KEY"),  # This is the default and can be omitted
            )
            response = client.v2.documents.templates.generate_document_async(
                slug="slug",
                type="document",
            )
            print(response.async_result_id)

````