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

# Generate Letter of Medical Necessity

> Generates a Letter of Medical Necessity (LMN) from uploaded procedure notes using AI analysis. The letter can be returned as plain text or as a formatted PDF document based on the `output_format` parameter.

## Overview

This endpoint generates a Letter of Medical Necessity (LMN) from uploaded procedure notes using AI analysis. The letter can be returned in two formats:

* **Text format** (default): Returns the letter content as plain text markdown
* **PDF format**: Returns a professionally formatted PDF document with proper letter layout, including header, patient information, body content, signature, and footer

## Output Formats

### Text Format (`output_format: "text"` or omitted)

When requesting text format, the response includes a `letter` field containing the markdown-formatted letter content.

### PDF Format (`output_format: "pdf"`)

When requesting PDF format, the response includes a `fileContent` field containing a base64-encoded PDF document. The PDF includes:

* Professional letter header with center name, contact information, and date
* Patient information section
* Formatted letter body with proper text wrapping
* Signature section with expert name
* Footer with generation timestamp


## OpenAPI

````yaml POST /api/generate-letter
openapi: 3.1.0
info:
  title: Cair Health APIs
  description: APIs for the Cair Health platform
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://forecaster.cairhealth.com
security:
  - bearerAuth: []
paths:
  /api/generate-letter:
    post:
      description: >-
        Generates a Letter of Medical Necessity (LMN) from uploaded procedure
        notes using AI analysis. The letter can be returned as plain text or as
        a formatted PDF document based on the `output_format` parameter.
      requestBody:
        description: >-
          Request body containing base64 encoded file content and additional
          metadata
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateLetterRequest'
            example:
              file_content: <Base64 encoded file content here>
              patient_identifier: PAT-12345
              patient_name: John Doe
              patient_age: '45'
              facility_name: Medical Center
              visit_cost: '500.00'
              provider_email: contact@medicalcenter.com
              provider_phone: +1-555-123-4567
              provider_signature: Dr. Jane Smith
              provider_name: Dr. Jane Smith, MD
              output_format: pdf
        required: true
      responses:
        '200':
          description: Letter of Medical Necessity generated successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LetterResponse'
              examples:
                text_response:
                  summary: Text format response, returned as plain text markdown
                  value:
                    letter: <Letter Content Here>
                    timestamp: '2025-10-16T14:30:45Z'
                    invoice_cost: '500.00'
                    patient_identifier: PAT-12345
                pdf_response:
                  summary: PDF format response, returned as base64 encoded string
                  value:
                    fileContent: <Base64 encoded PDF content>
                    timestamp: '2025-10-16T14:30:45Z'
                    invoice_cost: '500.00'
                    patient_identifier: PAT-12345
        '400':
          description: Invalid request or file processing error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: No file content provided
        '401':
          description: Unauthorized access or invalid organization ID
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: Organization not found
        '500':
          description: Server error or letter generation failure
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: Failed to generate letter from encounter
      security:
        - bearerAuth: []
components:
  schemas:
    GenerateLetterRequest:
      type: object
      description: >-
        Request body for generating a Letter of Medical Necessity from encounter
        notes
      properties:
        file_content:
          type: string
          description: Base64 encoded content of the procedure notes file (PDF)
        patient_identifier:
          type: string
          description: Unique identifier for the patient
        patient_name:
          type: string
          description: Patient's full name
        patient_age:
          type: string
          description: Patient's age
        facility_name:
          type: string
          description: Name of the medical facility or center
        visit_cost:
          type: string
          description: Cost of the visit or procedure
        provider_email:
          type: string
          description: Provider's contact email address
          format: email
        provider_phone:
          type: string
          description: Provider's contact phone number
          example: +1-555-123-4567
        provider_signature:
          type: string
          description: Signature line text for the letter
        provider_name:
          type: string
          description: Name and credentials of the provider signing the letter
          example: Dr. Jane Smith, MD
        output_format:
          type: string
          enum:
            - text
            - pdf
          description: Output format for the letter. Defaults to 'text' if not specified.
          default: text
      required:
        - file_content
        - patient_identifier
        - patient_name
        - patient_age
        - facility_name
        - visit_cost
        - provider_email
        - provider_phone
        - provider_signature
        - provider_name
    LetterResponse:
      type: object
      description: >-
        Response containing the generated Letter of Medical Necessity. The
        response format depends on the `output_format` parameter in the request.
      properties:
        letter:
          type: string
          description: >-
            The generated Letter of Medical Necessity content in text format.
            Present when `output_format` is 'text' or not specified.
        fileContent:
          type: string
          description: >-
            Base64 encoded PDF content of the generated letter. Present when
            `output_format` is 'pdf'.
        timestamp:
          type: string
          description: Timestamp when the letter was generated
          format: date-time
        invoice_cost:
          type: string
          description: The visit cost associated with the letter generation
        patient_identifier:
          type: string
          description: The patient identifier associated with the letter
      required: []
    Error:
      type: object
      properties:
        message:
          type: string
          description: Error message
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

````