PHP code example of facturapi / facturapi-php

1. Go to this page and download the library: Download facturapi/facturapi-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

facturapi / facturapi-php example snippets






acturapi\Facturapi;

$apiKey = getenv('FACTURAPI_KEY') ?: 'YOUR_API_KEY';
$facturapi = new Facturapi($apiKey);

$customer = $facturapi->Customers->create([
  'email' => '[email protected]',
  'legal_name' => 'Walter White',
  'tax_id' => 'WIWA761018',
  'address' => [
    'zip' => '06800',
    'street' => 'Av. de los Rosales',
    'exterior' => '123',
    'neighborhood' => 'Tepito',
  ],
]);

new Facturapi(string $apiKey, ?array $config = null)

use Facturapi\Facturapi;

$facturapi = new Facturapi($apiKey, [
  'apiVersion' => 'v2',
  'timeout' => 420,
]);

use Facturapi\Facturapi;
use GuzzleHttp\Client;

$httpClient = new Client([
  'timeout' => 420,
]);

$facturapi = new Facturapi($apiKey, [
  'httpClient' => $httpClient,
]);

$product = $facturapi->Products->create([
  'product_key' => '4319150114',
  'description' => 'Apple iPhone 8',
  'price' => 345.60,
]);

$invoice = $facturapi->Invoices->create([
  'customer' => 'YOUR_CUSTOMER_ID',
  'items' => [[
    'quantity' => 1,
    'product' => 'YOUR_PRODUCT_ID',
  ]],
  'payment_form' => \Facturapi\PaymentForm::EFECTIVO,
  'folio_number' => '581',
  'series' => 'F',
]);

$zipBytes = $facturapi->Invoices->downloadZip('INVOICE_ID');
$pdfBytes = $facturapi->Invoices->downloadPdf('INVOICE_ID');
$xmlBytes = $facturapi->Invoices->downloadXml('INVOICE_ID');

file_put_contents('invoice.pdf', $pdfBytes);

$facturapi->Invoices->sendByEmail('INVOICE_ID');

$results = $facturapi->ComercioExteriorCatalogs->searchTariffFractions([
  'q' => '0101',
  'page' => 0,
  'limit' => 10,
]);

$result = $facturapi->Organizations->updateDefaultSeries(
  'ORGANIZATION_ID',
  [
    'type' => 'I',
    'series' => 'A',
  ]
);

use Facturapi\Exceptions\FacturapiException;

try {
  $facturapi->Invoices->create($payload);
} catch (FacturapiException $e) {
  $status = $e->getStatusCode();
  $error = $e->getErrorData(); // Full API error shape when body is valid JSON.
  $firstDetail = $error['details'][0] ?? null; // e.g. ['path' => 'items.0.quantity', 'message' => '...', 'code' => '...']
}
bash
composer