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

# Create Credit Note

> Create a DRAFT credit note from an authorized invoice.

Validates that the original document is AUTHORIZED and its issue_date is
within 12 months. Supports idempotency via idempotency_key. Returns 201
with the new credit note id, state, and original_document_id.



## OpenAPI

````yaml /openapi.json post /documents/{document_id}/credit-note
openapi: 3.1.0
info:
  title: Factio SRI API
  description: >-
    Plataforma headless, multi-tenant, API-first para emitir comprobantes
    electrónicos válidos ante el SRI de Ecuador.


    ## Autenticación


    Soporta tres métodos:

    - **JWT Bearer por Google OAuth**: `/auth/google/callback`

    - **JWT Bearer por email/password**: `/auth/login`

    - **API Key**: header `X-API-Key` con clave generada en `/auth/api-keys`


    ## Multi-tenant


    El aislamiento por tenant combina PostgreSQL RLS y validaciones explícitas
    en backend. El `tenant_id` se resuelve desde el contexto de autenticación.


    ## Flujo de Emisión


    1. `POST /documents` — reserva secuencial y encola el documento (responde
    202)

    2. `GET /documents/{id}` — consulta el estado actual por polling

    3. Cuando `status = AUTHORIZED`, el campo `authorization_number` contiene la
    clave de autorización del SRI


    Documentación completa en https://docs.factioapp.com
  version: 0.1.0
servers:
  - url: http://localhost:8000
    description: Local
  - url: https://api.factioapp.com
    description: Producción
  - url: https://api.staging.factioapp.com
    description: Sandbox (datos de prueba, sin efecto fiscal real)
security: []
paths:
  /documents/{document_id}/credit-note:
    post:
      tags:
        - documents
      summary: Create Credit Note
      description: |-
        Create a DRAFT credit note from an authorized invoice.

        Validates that the original document is AUTHORIZED and its issue_date is
        within 12 months. Supports idempotency via idempotency_key. Returns 201
        with the new credit note id, state, and original_document_id.
      operationId: create_credit_note_documents__document_id__credit_note_post
      parameters:
        - name: document_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
            title: Document Id
        - name: authorization
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Authorization
        - name: x-api-key
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: X-Api-Key
        - name: x-tenant-id
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: X-Tenant-Id
        - name: factio_access_token
          in: cookie
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Factio Access Token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateCreditNoteRequest'
      responses:
        '201':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateCreditNoteResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    CreateCreditNoteRequest:
      properties:
        reason:
          type: string
          maxLength: 500
          minLength: 1
          title: Reason
        lines:
          items:
            $ref: '#/components/schemas/DocumentLineCreate'
          type: array
          title: Lines
        taxes:
          items:
            $ref: '#/components/schemas/DocumentTaxCreate'
          type: array
          title: Taxes
        subtotal:
          anyOf:
            - type: number
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Subtotal
        total_discount:
          anyOf:
            - type: number
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Total Discount
          default: '0.00'
        total_tax:
          anyOf:
            - type: number
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Total Tax
        total:
          anyOf:
            - type: number
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Total
        issue_date:
          type: string
          format: date
          title: Issue Date
        idempotency_key:
          anyOf:
            - type: string
            - type: 'null'
          title: Idempotency Key
      type: object
      required:
        - reason
        - lines
        - taxes
        - subtotal
        - total_tax
        - total
        - issue_date
      title: CreateCreditNoteRequest
    CreateCreditNoteResponse:
      properties:
        credit_note_id:
          type: string
          format: uuid
          title: Credit Note Id
        original_document_id:
          type: string
          format: uuid
          title: Original Document Id
        status:
          type: string
          title: Status
        access_key:
          anyOf:
            - type: string
            - type: 'null'
          title: Access Key
      type: object
      required:
        - credit_note_id
        - original_document_id
        - status
        - access_key
      title: CreateCreditNoteResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    DocumentLineCreate:
      properties:
        line_number:
          type: integer
          minimum: 1
          title: Line Number
          description: Line number (1-based)
        description:
          type: string
          maxLength: 1000
          minLength: 1
          title: Description
          description: Product or service description
        main_code:
          anyOf:
            - type: string
              maxLength: 25
              minLength: 1
            - type: 'null'
          title: Main Code
          description: Catalog SRI codigoPrincipal; omitted for ad-hoc lines
        aux_code:
          anyOf:
            - type: string
              maxLength: 25
            - type: 'null'
          title: Aux Code
          description: Catalog SRI codigoAuxiliar
        quantity:
          anyOf:
            - type: number
              exclusiveMinimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,6}0*$
          title: Quantity
          description: Quantity
        unit_price:
          anyOf:
            - type: number
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,6}0*$
          title: Unit Price
          description: Unit price
        discount:
          anyOf:
            - type: number
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,2}0*$
          title: Discount
          description: Discount amount for this line
          default: '0.00'
        subtotal:
          anyOf:
            - type: number
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,2}0*$
          title: Subtotal
          description: Line subtotal (quantity * unit_price - discount)
        tax_code:
          type: string
          title: Tax Code
          description: Tax code (e.g., '2' for IVA)
          default: '2'
        tax_rate_code:
          type: string
          title: Tax Rate Code
          description: Tax rate code (e.g., '0', '2', '4')
      type: object
      required:
        - line_number
        - description
        - quantity
        - unit_price
        - subtotal
        - tax_rate_code
      title: DocumentLineCreate
      description: Schema for creating a document line item.
    DocumentTaxCreate:
      properties:
        tax_code:
          type: string
          maxLength: 10
          minLength: 1
          title: Tax Code
          description: Tax code (e.g., '2' for IVA)
        tax_rate_code:
          type: string
          maxLength: 10
          minLength: 1
          title: Tax Rate Code
          description: Tax rate code (e.g., '2' for 12%, '0' for 0%)
        taxable_base:
          anyOf:
            - type: number
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,2}0*$
          title: Taxable Base
          description: Taxable base amount
        rate:
          anyOf:
            - type: number
              maximum: 1
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,4}0*$
          title: Rate
          description: Tax rate as decimal (e.g., 0.12 for 12%)
        tax_amount:
          anyOf:
            - type: number
              minimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d{0,2}0*$
          title: Tax Amount
          description: Tax amount (taxable_base * rate)
      type: object
      required:
        - tax_code
        - tax_rate_code
        - taxable_base
        - rate
        - tax_amount
      title: DocumentTaxCreate
      description: Schema for creating a document tax entry.
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````