Skip to content

PHP Examples

All examples use PHP’s built-in cURL extension. No external dependencies required.

<?php
define('API_BASE', 'https://api.suit.pe');
define('API_KEY', 'sk_live_YOUR_API_KEY');
function suitRequest(string $method, string $endpoint, ?array $body = null): array
{
$ch = curl_init(API_BASE . $endpoint);
$headers = [
'Authorization: Bearer ' . API_KEY,
'Content-Type: application/json',
];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => $method,
]);
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status' => $httpCode,
'data' => json_decode($response, true),
];
}
<?php
function emitInvoice(): array
{
return suitRequest('POST', '/api/invoices', [
'series' => 'F001',
'correlative' => 1,
'issueDate' => '2026-02-20',
'dueDate' => '2026-03-20',
'currencyCode' => 'PEN',
'customer' => [
'identityType' => '6',
'identityNumber' => '20100047218',
'name' => 'Empresa Cliente SAC',
'address' => 'Av. Javier Prado 1234, San Isidro',
],
'items' => [
[
'code' => 'SRV-001',
'description' => 'Servicio de consultoria',
'quantity' => 1,
'unitCode' => 'ZZ',
'unitPrice' => 1000.00,
'igvType' => '10',
],
],
]);
}
$result = emitInvoice();
echo "Invoice: " . json_encode($result['data']) . "\n";
// {"id": "cm3abc123", "status": "ACCEPTED", "documentId": "20123456789-01-F001-00000001"}
<?php
function emitReceipt(): array
{
return suitRequest('POST', '/api/receipts', [
'series' => 'B001',
'correlative' => 1,
'issueDate' => '2026-02-20',
'currencyCode' => 'PEN',
'customer' => [
'identityType' => '1',
'identityNumber' => '12345678',
'name' => 'Juan Perez',
],
'items' => [
[
'code' => 'SRV-002',
'description' => 'Corte de cabello premium',
'quantity' => 1,
'unitCode' => 'ZZ',
'unitPrice' => 50.00,
'igvType' => '10',
],
],
]);
}
<?php
function emitCreditNote(): array
{
return suitRequest('POST', '/api/credit-notes', [
'series' => 'F001',
'correlative' => 1,
'issueDate' => '2026-02-21',
'currencyCode' => 'PEN',
'referenceDocumentId' => 'F001-00000001',
'referenceDocumentType' => '01',
'responseCode' => '01',
'responseDescription' => 'Anulacion de la operacion',
'customer' => [
'identityType' => '6',
'identityNumber' => '20100047218',
'name' => 'Empresa Cliente SAC',
],
'items' => [
[
'code' => 'SRV-001',
'description' => 'Servicio de consultoria (anulacion)',
'quantity' => 1,
'unitCode' => 'ZZ',
'unitPrice' => 1000.00,
'igvType' => '10',
],
],
]);
}
<?php
function emitDebitNote(): array
{
return suitRequest('POST', '/api/debit-notes', [
'series' => 'F001',
'correlative' => 1,
'issueDate' => '2026-02-21',
'currencyCode' => 'PEN',
'referenceDocumentId' => 'F001-00000001',
'referenceDocumentType' => '01',
'responseCode' => '01',
'responseDescription' => 'Intereses por mora',
'customer' => [
'identityType' => '6',
'identityNumber' => '20100047218',
'name' => 'Empresa Cliente SAC',
],
'items' => [
[
'code' => 'INT-001',
'description' => 'Intereses moratorios - Febrero 2026',
'quantity' => 1,
'unitCode' => 'ZZ',
'unitPrice' => 150.00,
'igvType' => '10',
],
],
]);
}

Send a Comunicacion de Baja (Voided Document)

Section titled “Send a Comunicacion de Baja (Voided Document)”
<?php
function sendVoidedDocument(): array
{
// Step 1: Send the voided document
$result = suitRequest('POST', '/api/voided-documents', [
'issueDate' => '2026-02-20',
'referenceDate' => '2026-02-19',
'correlative' => 1,
'items' => [
[
'series' => 'F001',
'correlative' => 1,
'documentType' => '01',
'reason' => 'Error en datos del cliente',
],
],
]);
echo "Ticket: " . $result['data']['ticket'] . "\n";
// Step 2: Poll for the result
$finalResult = checkTicket($result['data']['id']);
echo "Final status: " . $finalResult['data']['status'] . "\n";
return $finalResult;
}
function checkTicket(string $documentId, int $maxAttempts = 12): array
{
for ($i = 0; $i < $maxAttempts; $i++) {
$result = suitRequest('POST', "/api/documents/{$documentId}/check-ticket");
if ($result['data']['status'] !== 'SENT') {
return $result;
}
// Wait 5 seconds before next attempt
sleep(5);
}
throw new \RuntimeException('Ticket check timed out');
}
<?php
function listDocuments(array $options = []): array
{
$params = http_build_query(array_filter([
'page' => $options['page'] ?? 1,
'limit' => $options['limit'] ?? 20,
'type' => $options['type'] ?? null,
'status' => $options['status'] ?? null,
'from' => $options['from'] ?? null,
'to' => $options['to'] ?? null,
]));
return suitRequest('GET', "/api/documents?{$params}");
}
// Usage
$invoices = listDocuments(['type' => '01', 'page' => 1, 'limit' => 10]);
<?php
function downloadPdf(string $documentId, string $outputPath): void
{
$ch = curl_init(API_BASE . "/api/documents/{$documentId}/pdf");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . API_KEY,
],
]);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents($outputPath, $response);
echo "PDF saved to {$outputPath}\n";
}
// Usage
downloadPdf('cm3abc123', './invoice-F001-1.pdf');
<?php
function retryDocument(string $documentId): array
{
return suitRequest('POST', "/api/documents/{$documentId}/retry");
}
<?php
function emitInvoiceAfterPayment(array $payment, array $customer): array
{
$today = date('Y-m-d');
$dueDate = date('Y-m-d', strtotime('+30 days'));
$items = array_map(function ($item) {
return [
'code' => $item['code'],
'description' => $item['name'],
'quantity' => $item['quantity'],
'unitCode' => 'ZZ',
'unitPrice' => $item['price'],
'igvType' => '10',
];
}, $payment['items']);
$result = suitRequest('POST', '/api/invoices', [
'series' => 'F001',
'correlative' => $payment['invoice_number'],
'issueDate' => $today,
'dueDate' => $dueDate,
'currencyCode' => 'PEN',
'customer' => [
'identityType' => $customer['document_type'],
'identityNumber' => $customer['document_number'],
'name' => $customer['name'],
'address' => $customer['address'] ?? '',
],
'items' => $items,
]);
$data = $result['data'];
switch ($data['status']) {
case 'ACCEPTED':
echo "Invoice accepted: {$data['documentId']}\n";
break;
case 'REJECTED':
echo "Invoice rejected: {$data['cdrDescription']}\n";
break;
case 'SUNAT_ERROR':
echo "SUNAT error, will retry later: {$data['id']}\n";
break;
}
return $result;
}