|
1 | | -# FiscalAPI SDK |
| 1 | +# FiscalAPI SDK para Python |
| 2 | + |
| 3 | +[](https://badge.fury.io/py/fiscalapi) |
| 4 | +[](https://github.com/FiscalAPI/fiscalapi-python/blob/main/LICENSE) |
| 5 | + |
| 6 | +**SDK oficial de FiscalAPI para Python**, la API de facturación CFDI y otros servicios fiscales en México. Simplifica la integración con los servicios de facturación electrónica, eliminando las complejidades del SAT y facilitando la generación de facturas, notas de crédito, complementos de pago, nómina, carta porte, y más. ¡Factura sin dolor! |
| 7 | + |
| 8 | +## 🚀 Características |
| 9 | + |
| 10 | +- Soporte completo para **CFDI 4.0** |
| 11 | +- Compatible con Python 3.7+ |
| 12 | +- Dos modos de operación: **Por valores** o **Por referencias** |
| 13 | +- Manejo simplificado de errores |
| 14 | +- Búsqueda en catálogos del SAT |
| 15 | +- Documentación completa y ejemplos prácticos |
| 16 | + |
| 17 | +## 📦 Instalación |
| 18 | + |
| 19 | +**pip**: |
| 20 | +```bash |
| 21 | +pip install fiscalapi |
| 22 | +``` |
| 23 | + |
| 24 | +**poetry**: |
| 25 | +```bash |
| 26 | +poetry add fiscalapi |
| 27 | +``` |
| 28 | + |
| 29 | +## ⚙️ Configuración |
| 30 | + |
| 31 | +### Configuración Básica |
| 32 | + |
| 33 | +1. **Crea un objeto de configuración** con [tus credenciales](https://docs.fiscalapi.com/credentials-info): |
| 34 | +```python |
| 35 | +from fiscalapi.models.common_models import FiscalApiSettings |
| 36 | + |
| 37 | +settings = FiscalApiSettings( |
| 38 | + api_url="https://test.fiscalapi.com", # https://live.fiscalapi.com (producción) |
| 39 | + api_key="<API_KEY>", |
| 40 | + tenant="<TENANT_KEY>" |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +2. **Crea la instancia del cliente**: |
| 45 | +```python |
| 46 | +from fiscalapi.services.fiscalapi_client import FiscalApiClient |
| 47 | + |
| 48 | +client = FiscalApiClient(settings=settings) |
| 49 | +``` |
| 50 | + |
| 51 | +## 🔄 Modos de Operación |
| 52 | + |
| 53 | +FiscalAPI admite dos [modos de operación](https://docs.fiscalapi.com/modes-of-operation): |
| 54 | + |
| 55 | +- **Por Referencias**: Envía solo IDs de objetos previamente creados en el dashboard de FiscalAPI. |
| 56 | + Ideal para integraciones ligeras. |
| 57 | + |
| 58 | +- **Por Valores**: Envía todos los campos requeridos en cada petición, con mayor control sobre los datos. |
| 59 | + No se requiere configuración previa en el dashboard. |
| 60 | + |
| 61 | +## 📝 Ejemplos de Uso |
| 62 | + |
| 63 | +### 1. Crear una Persona (Emisor o Receptor) |
| 64 | + |
| 65 | +```python |
| 66 | +from fiscalapi.models.fiscalapi_models import Person |
| 67 | + |
| 68 | +person = Person( |
| 69 | + legal_name="Empresa Python SA de CV", |
| 70 | + email="mail7@gmail.com", |
| 71 | + password="TestPassword1234!" |
| 72 | +) |
| 73 | + |
| 74 | +api_response = client.people.create(person) |
| 75 | +``` |
| 76 | + |
| 77 | +### 2. Subir Certificados CSD |
| 78 | + |
| 79 | +```python |
| 80 | +from fiscalapi.models.fiscalapi_models import TaxFile |
| 81 | + |
| 82 | +# Subir certificado (CER) |
| 83 | +certificado_csd = TaxFile( |
| 84 | + person_id="3f3478b4-60fd-459e-8bfc-f8239fc96257", |
| 85 | + tin="FUNK671228PH6", |
| 86 | + base64_file="MIIFgDCCA2igAwIBAgIUMzAwMDEwMDAwMDA1MDAwMDM0NDYwDQYJKo...", |
| 87 | + file_type=0, # 0 para certificado, 1 para llave privada |
| 88 | + password="12345678a" |
| 89 | +) |
| 90 | + |
| 91 | +# Subir llave privada (KEY) |
| 92 | +clave_privada_csd = TaxFile( |
| 93 | + person_id="3f3478b4-60fd-459e-8bfc-f8239fc96257", |
| 94 | + tin="FUNK671228PH6", |
| 95 | + base64_file="MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIAg...", |
| 96 | + file_type=1, |
| 97 | + password="12345678a" |
| 98 | +) |
| 99 | + |
| 100 | +api_response_cer = client.tax_files.create(certificado_csd) |
| 101 | +api_response_key = client.tax_files.create(clave_privada_csd) |
| 102 | +``` |
| 103 | + |
| 104 | +### 3. Crear un Producto o Servicio |
| 105 | + |
| 106 | +```python |
| 107 | +from fiscalapi.models.fiscalapi_models import Product |
| 108 | + |
| 109 | +product = Product( |
| 110 | + description="Producto python sin impuestos", |
| 111 | + unit_price=Decimal("100.00") |
| 112 | +) |
| 113 | + |
| 114 | +api_response = client.products.create(product) |
| 115 | +``` |
| 116 | + |
| 117 | +### 4. Actualizar Impuestos de un Producto |
| 118 | + |
| 119 | +```python |
| 120 | +from fiscalapi.models.fiscalapi_models import Product, ProductTax |
| 121 | + |
| 122 | +product = Product( |
| 123 | + id="f4bf4df3-5a91-4a30-b137-52cb517d13c4", |
| 124 | + description="Producto python sin impuestos", |
| 125 | + unit_price=Decimal("100.00"), |
| 126 | + product_taxes=[ |
| 127 | + ProductTax( |
| 128 | + rate=Decimal("0.160000"), |
| 129 | + taxId="002", |
| 130 | + taxFlagId="T", |
| 131 | + taxTypeId="Tasa" |
| 132 | + ), |
| 133 | + ProductTax( |
| 134 | + rate=Decimal("0.106667"), |
| 135 | + taxId="002", |
| 136 | + taxFlagId="R", |
| 137 | + taxTypeId="Tasa" |
| 138 | + ), |
| 139 | + ProductTax( |
| 140 | + rate=Decimal("0.100000"), |
| 141 | + taxId="001", |
| 142 | + taxFlagId="R", |
| 143 | + taxTypeId="Tasa" |
| 144 | + ) |
| 145 | + ] |
| 146 | +) |
| 147 | + |
| 148 | +api_response = client.products.update(product) |
| 149 | +``` |
| 150 | + |
| 151 | +### 5. Crear una Factura de Ingreso (Por Referencias) |
| 152 | + |
| 153 | +```python |
| 154 | +from datetime import datetime |
| 155 | +from decimal import Decimal |
| 156 | +from fiscalapi.models.fiscalapi_models import Invoice, InvoiceIssuer, InvoiceItem, InvoiceRecipient |
| 157 | + |
| 158 | +invoice = Invoice( |
| 159 | + version_code="4.0", |
| 160 | + series="F", |
| 161 | + date=datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), |
| 162 | + payment_form_code="01", |
| 163 | + payment_conditions="Contado", |
| 164 | + currency_code="MXN", |
| 165 | + type_code="I", |
| 166 | + expedition_zip_code="42501", |
| 167 | + payment_method_code="PUE", |
| 168 | + exchange_rate=1, |
| 169 | + export_code="01", |
| 170 | + issuer=InvoiceIssuer( |
| 171 | + id="3f3478b4-60fd-459e-8bfc-f8239fc96257" |
| 172 | + ), |
| 173 | + recipient=InvoiceRecipient( |
| 174 | + id="96b46762-d246-4a67-a562-510a25dbafa9" |
| 175 | + ), |
| 176 | + items=[ |
| 177 | + InvoiceItem( |
| 178 | + id="114a4be5-fb65-40b2-a762-ff0c55c6ebfa", |
| 179 | + quantity=Decimal("1.5"), |
| 180 | + discount=Decimal("255.85") |
| 181 | + ) |
| 182 | + ] |
| 183 | +) |
| 184 | + |
| 185 | +api_response = client.invoices.create(invoice) |
| 186 | +``` |
| 187 | + |
| 188 | +### 6. Crear la Misma Factura de Ingreso (Por Valores) |
| 189 | + |
| 190 | +```python |
| 191 | +from fiscalapi.models.fiscalapi_models import Invoice, InvoiceIssuer, InvoiceItem, InvoiceRecipient, ItemTax, TaxCredential |
| 192 | + |
| 193 | +invoice = Invoice( |
| 194 | + version_code="4.0", |
| 195 | + series="F", |
| 196 | + date=datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), |
| 197 | + payment_form_code="01", |
| 198 | + currency_code="MXN", |
| 199 | + type_code="I", |
| 200 | + expedition_zip_code="42501", |
| 201 | + payment_method_code="PUE", |
| 202 | + exchange_rate=1, |
| 203 | + export_code="01", |
| 204 | + issuer=InvoiceIssuer( |
| 205 | + tin="FUNK671228PH6", |
| 206 | + legal_name="KARLA FUENTE NOLASCO", |
| 207 | + tax_regime_code="621", |
| 208 | + tax_credentials=[ |
| 209 | + TaxCredential( |
| 210 | + base64_file="MIIFgDCCA2igAwIBAgIUMzAwMDEwMDAwMDA1MDAwMDM0NDYwDQYJKo...", |
| 211 | + file_type=0, |
| 212 | + password="12345678a" |
| 213 | + ), |
| 214 | + TaxCredential( |
| 215 | + base64_file="MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIAg...", |
| 216 | + file_type=1, |
| 217 | + password="12345678a" |
| 218 | + ) |
| 219 | + ] |
| 220 | + ), |
| 221 | + recipient=InvoiceRecipient( |
| 222 | + tin="EKU9003173C9", |
| 223 | + legal_name="ESCUELA KEMPER URGATE", |
| 224 | + zip_code="42501", |
| 225 | + tax_regime_code="601", |
| 226 | + cfdi_use_code="G01", |
| 227 | + email="mail@domain.com" |
| 228 | + ), |
| 229 | + items=[ |
| 230 | + InvoiceItem( |
| 231 | + item_code="84111506", |
| 232 | + quantity=Decimal("9.5"), |
| 233 | + unit_of_measurement_code="E48", |
| 234 | + description="Invoicing software as a service", |
| 235 | + unit_price=Decimal("3587.75"), |
| 236 | + tax_object_code="02", |
| 237 | + item_sku="7506022301697", |
| 238 | + discount=Decimal("255.85"), |
| 239 | + item_taxes=[ |
| 240 | + ItemTax( |
| 241 | + tax_code="002", |
| 242 | + tax_type_code="Tasa", |
| 243 | + tax_rate=Decimal("0.160000"), |
| 244 | + tax_flag_code="T" |
| 245 | + ) |
| 246 | + ] |
| 247 | + ) |
| 248 | + ] |
| 249 | +) |
| 250 | + |
| 251 | +api_response = client.invoices.create(invoice) |
| 252 | +``` |
| 253 | + |
| 254 | +### 7. Búsqueda en Catálogos del SAT |
| 255 | + |
| 256 | +```python |
| 257 | +# Buscar registros que contengan 'inter' en el catalogo 'SatUnitMeasurements' (página 1, tamaño página 10) |
| 258 | +api_response = client.catalogs.search_catalog("SatUnitMeasurements", "inter", 1, 10) |
| 259 | + |
| 260 | +if api_response.succeeded: |
| 261 | + for item in api_response.data.items: |
| 262 | + print(f"Unidad: {item.description}") |
| 263 | +else: |
| 264 | + print(api_response.message) |
| 265 | +``` |
| 266 | + |
| 267 | +## 📋 Operaciones Principales |
| 268 | + |
| 269 | +- **Facturas (CFDI)** |
| 270 | + Crear facturas de ingreso, notas de crédito, complementos de pago, cancelaciones, generación de PDF/XML. |
| 271 | +- **Personas (Clientes/Emisores)** |
| 272 | + Alta y administración de personas, gestión de certificados (CSD). |
| 273 | +- **Productos y Servicios** |
| 274 | + Administración de catálogos de productos, búsqueda en catálogos SAT. |
| 275 | + |
| 276 | +## 🤝 Contribuir |
| 277 | + |
| 278 | +1. Haz un fork del repositorio. |
| 279 | +2. Crea una rama para tu feature: `git checkout -b feature/AmazingFeature` |
| 280 | +3. Realiza commits de tus cambios: `git commit -m 'Add some AmazingFeature'` |
| 281 | +4. Sube tu rama: `git push origin feature/AmazingFeature` |
| 282 | +5. Abre un Pull Request en GitHub. |
| 283 | + |
| 284 | +## 🐛 Reportar Problemas |
| 285 | + |
| 286 | +1. Asegúrate de usar la última versión del SDK. |
| 287 | +2. Verifica si el problema ya fue reportado. |
| 288 | +3. Proporciona un ejemplo mínimo reproducible. |
| 289 | +4. Incluye los mensajes de error completos. |
| 290 | + |
| 291 | +## 📄 Licencia |
| 292 | + |
| 293 | +Este proyecto está licenciado bajo la Licencia **MPL**. Consulta el archivo [LICENSE](LICENSE.txt) para más detalles. |
| 294 | + |
| 295 | +## 🔗 Enlaces Útiles |
| 296 | + |
| 297 | +- [Documentación Oficial](https://docs.fiscalapi.com) |
| 298 | +- [Portal de FiscalAPI](https://fiscalapi.com) |
| 299 | +- [Ejemplos en Python](https://github.com/FiscalAPI/fiscalapi-samples-python) |
| 300 | + |
| 301 | +--- |
| 302 | + |
| 303 | +Desarrollado con ❤️ por [Fiscalapi](https://www.fiscalapi.com) |
0 commit comments