Skip to main content

Quickstart

1. Install the package

npm install facturapi

2. Create your first invoice

// a) Import the package
const Facturapi = require('facturapi');
// b) Create an instance of the client, using the secret key
// of the issuing organization (https://dashboard.facturapi.io/integration/apikeys)
const facturapi = new Facturapi('sk_test_API_KEY');
// c) Create an invoice
const invoice = await facturapi.invoices.create({
customer: {
legal_name: 'Dunder Mifflin',
email: 'email@example.com',
tax_id: 'ABC101010111',
tax_system: '601',
address: {
zip: '85900'
}
},
items: [{
quantity: 1,
product: {
description: 'Ukelele',
product_key: '60131324',
price: 345.60
}
}],
use: 'G01',
payment_form: '28' // Credit card
});
info

By default, the product price is considered to have taxes included. Facturapi will break down the product taxes (16% VAT by default) and calculate the unit price. If you need the price attribute to be the unit price instead, you must send the tax_included parameter with the value false.

Response: Invoice object
{
"id": "58e93bd8e86eb318b019743d",
"created_at": "2017-01-01T14:00:08.000Z",
"livemode": false,
"status": "valid",
"cancellation_status": "none",
"verification_url": "https://verificacfdi.facturaelectronica.sat.gob.mx/default.aspx?id=45BEC0CA-5F1E-491E-9417-698EA48C382A&re=AAA010101AAA&rr=ABC101010111&tt=345.600000&fe=bWApPw==",
"customer": {
"id": "58e93bd8e86eb318b0197456",
"legal_name": "Dunder Mifflin S.A. de C.V.",
"tax_id": "ABC101010111",
"tax_system": "601",
"address": {
"zip": "85900",
"country": "MEX"
}
},
"total": 345.60,
"uuid": "39c85a3f-275b-4341-b259-e8971d9f8a94",
"folio_number": 914,
"series": "A",
"payment_form": "28",
"related": [],
"currency": "MXN",
"exchange": 1,
"items": [{
"quantity": 1,
"discount": 0,
"product": {
"description": "Ukelele",
"price": 345.60,
"tax_included": true,
"taxes": [
{
"type": "IVA",
"rate": 0.16
}
]
}
}],
"stamp": {
"signature": "ZGgQ126+lbo6XxVmeM0Kys1rAllqRaDmaK4yW20B3H5AaVShnItBwKATpxqJuGK1qPmLA2r16B8dAb4UFjR27Xc/+SsNPSwRBYRVKI0AB62jx2Z4uxooiVQBY9Bb6czlgzJb+ftgNvnGwSXzI6QZKpuWRe9LmJvACzqTB3ZdC9QoqaVICDNZ9oaT99txu9ahbJu3ftPhlykXi1SxVTBZ7uUTqsBkc6iEjbSTYpE85bsrhbMw4tDODR7o/PS917whChOFUU0sQenm5sJQMenPcKPyS9JoGQPO/a/4wzxJ2RyWCkw72LNFBbJTsPXcXdOZmEJ06Ixc2Iy24Biz8GEbJg==",
"date": "2021-03-30T00:57:48",
"sat_cert_number": "20001000000300022323",
"sat_signature": "AzYwRdHfDp0BCBaTpT87gtAAE3Q="
}
}

3. Send the invoice by email

await facturapi.invoices.sendByEmail(invoice.id);

4. Download the XML and PDF files

const fs = require('fs');

// Download the PDF and XML files, bundled together in a ZIP file
const zipStream = await facturapi.invoices.downloadZip(invoice.id);
// Save the downloaded file to disk
const file = fs.createWriteStream('./factura.zip');
zipStream.pipe(file);
// Or send it as a response to your customer (ExpressJS syntax)
zipStream.pipe(res);

Next steps

This was a brief overview of using Facturapi.

We invite you to check our examples section, as well as the full API reference, where you can explore all the endpoints available in Facturapi.