Skip to main content
POST
/
api
/
v1
/
invoices
/
{id}
/
send-sunat
Send to SUNAT
curl --request POST \
  --url https://api.example.com/api/v1/invoices/{id}/send-sunat
{
  "success": true,
  "data": {},
  "message": "<string>",
  "error_code": "<string>"
}
Sends an invoice or boleta to SUNAT for validation. This endpoint generates the XML file, signs it digitally, and submits it to SUNAT’s web service.
This endpoint works for both invoices (facturas) and boletas. Use /api/v1/invoices/{id}/send-sunat for invoices and /api/v1/boletas/{id}/send-sunat for boletas.

Authentication

This endpoint requires authentication using a Bearer token (Sanctum).
Authorization: Bearer YOUR_API_TOKEN

Path Parameters

id
integer
required
The ID of the invoice or boleta to send to SUNAT.

Request Body

This endpoint does not require a request body. All information is taken from the existing invoice/boleta record.

Response

Success Response

success
boolean
Indicates if the document was successfully sent and accepted by SUNAT.
data
object
The updated document with SUNAT response information.
message
string
Success message describing the operation result.

Error Response

success
boolean
Always false for error responses.
data
object
The document object with error details.
message
string
Error message from SUNAT or system.
error_code
string
SUNAT error code or “UNKNOWN” for system errors.

Request Examples

curl -X POST https://api.example.com/api/v1/invoices/123/send-sunat \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json"

Response Examples

{
  "success": true,
  "data": {
    "id": 123,
    "company_id": 1,
    "branch_id": 1,
    "serie": "F001",
    "numero": 456,
    "numero_completo": "F001-456",
    "fecha_emision": "2024-03-15",
    "estado_sunat": "ACEPTADO",
    "sunat_response": "La Factura numero F001-456, ha sido aceptada",
    "sunat_code": "0",
    "xml_path": "storage/sunat/company_1/invoices/20240315/20987654321-01-F001-456.xml",
    "cdr_path": "storage/sunat/company_1/invoices/20240315/R-20987654321-01-F001-456.zip",
    "hash": "Xj3kL9mP2qR5vW8zA1bC4dE7fG0hI",
    "enviado_sunat": true,
    "fecha_envio_sunat": "2024-03-15T10:35:22.000000Z",
    "company": {
      "id": 1,
      "ruc": "20987654321",
      "razon_social": "MI EMPRESA SAC"
    },
    "branch": {
      "id": 1,
      "nombre": "Sede Principal"
    },
    "client": {
      "id": 45,
      "tipo_documento": "6",
      "numero_documento": "20123456789",
      "razon_social": "EMPRESA DEMO SAC"
    }
  },
  "message": "Factura enviada correctamente a SUNAT"
}

Process Flow

When you call this endpoint, the following process occurs:
  1. Validation: Checks if the document exists and hasn’t been accepted by SUNAT already
  2. XML Generation: Creates the UBL 2.1 XML file according to SUNAT specifications
  3. Digital Signature: Signs the XML with the company’s digital certificate
  4. Compression: Compresses the signed XML into a ZIP file
  5. SUNAT Submission: Sends the ZIP to SUNAT’s web service (OSE or SUNAT)
  6. CDR Reception: Receives and stores the CDR (Constancia de Recepción)
  7. Status Update: Updates the document status based on SUNAT’s response

SUNAT Status Codes

The document’s estado_sunat field will be updated based on the response:
  • ACEPTADO: SUNAT accepted the document (code 0 or 0001-0099)
  • RECHAZADO: SUNAT rejected the document (code 2000-4999)
  • PENDIENTE: Document hasn’t been sent or submission failed

Common SUNAT Error Codes

If SUNAT rejects the document, review the error_code and sunat_response to understand the issue:
  • 2335: RUC not active in the registry
  • 2324: Duplicate document number
  • 2800: Certificate expired or invalid
  • 2801: Certificate not found
  • 3032: Document date outside allowed range
  • 4033: Digital signature validation failed

Environment Configuration

The endpoint uses the company’s configuration to determine:
  • Production vs Testing: Sends to SUNAT production or beta environment
  • OSE Provider: Uses configured OSE (Operador de Servicios Electrónicos) or SUNAT directly
  • Certificates: Uses the company’s digital certificate for signing

Important Notes

  • Once a document is ACEPTADO by SUNAT, it cannot be sent again
  • Documents must be sent on the same day they are issued (SUNAT requirement)
  • The XML and CDR files are automatically stored in the configured storage path
  • For boletas sent via daily summary, use the daily summary endpoints instead

Build docs developers (and LLMs) love