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

# Create Invoice from Ticket

> Create invoice from receipt image

## Overview

Create an invoice (factura) from a receipt image. The system will process the image, extract the relevant information, and generate a CFDI-compliant invoice.

<Note>
  **This operation requires 1 credit.** Make sure you have sufficient credits before calling this endpoint.
</Note>

## Request Body

<ParamField body="receipt_image" type="string" required>
  Imagen del ticket codificada en Base64. Formatos soportados: JPEG, PNG.
</ParamField>

<ParamField body="cfdi_usage" type="string" required>
  Código de uso CFDI. Valores comunes:

  * `G01` - Adquisición de mercancías
  * `G02` - Devoluciones, descuentos o bonificaciones
  * `G03` - Gastos en general
  * `P01` - Por definir
</ParamField>

<ParamField body="expense_description" type="string">
  Descripción opcional del gasto. Ejemplo: `Comida de negocios`
</ParamField>

## Response

<ResponseField name="id" type="string">
  Unique invoice identifier
</ResponseField>

<ResponseField name="status" type="string">
  Invoice status. Values: `pending`, `processing`, `completed`, `failed`
</ResponseField>

<ResponseField name="amount" type="number">
  Invoice total amount (extracted from receipt)
</ResponseField>

<ResponseField name="currency" type="string">
  Currency code (e.g., `MXN`)
</ResponseField>

<ResponseField name="vendor" type="string">
  Vendor name (extracted from receipt)
</ResponseField>

<ResponseField name="created_at" type="string">
  ISO 8601 timestamp of creation
</ResponseField>

<ResponseField name="pdf_url" type="string">
  URL to download the invoice PDF (available when status is `completed`)
</ResponseField>

<ResponseField name="xml_url" type="string">
  URL to download the invoice XML/CFDI (available when status is `completed`)
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.usatimbre.com/api/timbre/facturar-ticket" \
    -H "Authorization: Bearer tmb_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "receipt_image": "data:image/jpeg;base64,/9j/4AAQSkZJRg...",
      "cfdi_usage": "G03",
      "expense_description": "Comida de negocios"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.usatimbre.com/api/timbre/facturar-ticket', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer tmb_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      receipt_image: 'data:image/jpeg;base64,/9j/4AAQSkZJRg...',
      cfdi_usage: 'G03',
      expense_description: 'Comida de negocios'
    })
  });
  const invoice = await response.json();
  ```

  ```python Python theme={null}
  import requests
  import base64

  # Read and encode image
  with open('receipt.jpg', 'rb') as f:
      image_base64 = base64.b64encode(f.read()).decode('utf-8')

  response = requests.post(
      'https://api.usatimbre.com/api/timbre/facturar-ticket',
      headers={
          'Authorization': 'Bearer tmb_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'receipt_image': f'data:image/jpeg;base64,{image_base64}',
          'cfdi_usage': 'G03',
          'expense_description': 'Comida de negocios'
      }
  )
  invoice = response.json()
  ```
</RequestExample>

<ResponseExample>
  ```json 201 theme={null}
  {
    "id": "inv_abc123",
    "status": "processing",
    "amount": null,
    "currency": "MXN",
    "vendor": null,
    "created_at": "2024-01-21T10:30:00Z",
    "pdf_url": null,
    "xml_url": null
  }
  ```

  ```json 400 theme={null}
  {
    "error": "Bad Request",
    "message": "Invalid image format"
  }
  ```

  ```json 402 theme={null}
  {
    "error": "Payment Required",
    "message": "Insufficient credits"
  }
  ```
</ResponseExample>

## Processing Status

After creating an invoice, the status will be `processing`. You can poll the [Get Invoice Details](/api-reference/timbre/get-invoice) endpoint to check when processing is complete.

| Status       | Description                    |
| ------------ | ------------------------------ |
| `pending`    | Invoice request received       |
| `processing` | Receipt is being processed     |
| `completed`  | Invoice generated successfully |
| `failed`     | Invoice generation failed      |
