Alegra API Client
This project provides an API client for the Alegra platform using the pydantic and requests libraries. The modular structure allows for flexible and extensible resource management.
Installation
- Clone the repository.
- Install the required dependencies:
pip install -r requirements.txt
Configuration
Create a configuration object to handle API key and environment settings.
from config import ApiConfig
config = ApiConfig(api_key='your_api_key', environment='sandbox')
Usage
Initialize the API client with the configuration:
from client import ApiClient
client = ApiClient(config)
Company Operations
Create a Company
from models.company import Company, NotificationByEmail
company_data = Company(
useAlegraCertificate=True,
notificationByEmail=NotificationByEmail(enabled=True)
)
new_company = client.companies.create(company_data)
print(new_company.id, new_company.name)
Get a Company
company = client.companies.get("company_id")
print(company.id, company.name)
Update a Company
updated_data = Company(name="Updated Company Name")
updated_company = client.companies.update("company_id", updated_data)
print(updated_company.name)
List Companies
companies = client.companies.list()
for company in companies:
print(company.id, company.name)
Payroll Operations
Create a Payroll
from models.payroll import Payroll
payroll_data = Payroll(
prefix="NE",
number=1,
governmentData={"key": "value"}
)
new_payroll = client.payrolls.create(payroll_data)
print(new_payroll.id, new_payroll.prefix)
Get a Payroll
payroll = client.payrolls.get("payroll_id")
print(payroll.id, payroll.prefix)
Update a Payroll
updated_data = Payroll(prefix="NE", number=2)
updated_payroll = client.payrolls.update("payroll_id", updated_data)
print(updated_payroll.number)
List Payrolls
payrolls = client.payrolls.list()
for payroll in payrolls:
print(payroll.id, payroll.prefix)
Replace a Payroll
replacement_data = Payroll(prefix="NE", number=2, governmentData={"key": "new_value"})
replaced_payroll = client.payrolls.perform_subaction("payroll_id", "replace", replacement_data)
print(replaced_payroll.id, replaced_payroll.number)
Cancel a Payroll
cancel_response = client.payrolls.perform_subaction("payroll_id", "cancel")
print(cancel_response)
DIAN Operations
List DIAN Resources
dian_resources = client.dian.list()
for resource in dian_resources:
print(resource.id, resource.name)
Test Set Operations
Create a Test Set
from models.test_set import TestSet
test_set_data = TestSet(
type="test",
governmentId="gov123",
idCompany="company_id"
)
new_test_set = client.test_sets.create(test_set_data)
print(new_test_set.id)
Get a Test Set
test_set = client.test_sets.get("test_set_id")
print(test_set.id, test_set.type)
Invoice Response Statuses
client.invoices.create(...), client.invoices.get(...) and client.invoices.list(...) return the variant of InvoiceResponse for the invoice's status. Each variant requires exactly the fields the API sends for that status; fields a status does not carry are None.
status |
Class | Present besides the fixed fields |
|---|---|---|
REGISTERED |
RegisteredInvoiceResponse |
nothing |
WAITING_RESPONSE |
WaitingInvoiceResponse |
cufe, qrCodeContent, xmlFileName, zipFileName |
SENT |
SentInvoiceResponse |
the four above, plus legalStatus and governmentResponse |
FAILED |
FailedInvoiceResponse |
errors when the API sends them, plus whatever was generated before the failure |
SENT only means the DIAN received the document; whether it was accepted is legalStatus. While the status is REGISTERED or WAITING_RESPONSE, call client.invoices.get(invoice.id) again until the DIAN answers.
The variant is chosen by AnyInvoiceResponse, a discriminated union on status. To parse a payload outside the client, validate it through a TypeAdapter:
from pydantic import TypeAdapter
from alegra.models.invoice import AnyInvoiceResponse
invoice = TypeAdapter(AnyInvoiceResponse).validate_python(payload)
An unknown status fails validation. Call get on a sync client: with async_mode=True only create is awaited, and get, update, list and subactions fail with AlegraResponseParseError.
Error Handling
Every error the client raises intentionally is an AlegraApiError:
AlegraConfigurationError: invalidApiConfig(badenvironment, blankapi_key)AlegraHttpError: an HTTP error response, or a network/transport failure. Carries.status_code(Nonefor network errors),.url, and.responseAlegraResponseParseError: a "successful" response didn't have the expected shape. The message names the status, id and failing fields without including response data;.responseholds the full decoded body.listraises it too
from alegra import AlegraApiError, AlegraHttpError
try:
new_company = client.companies.create(company_data)
except AlegraHttpError as e:
print(f"Request to {e.url} failed with status {e.status_code}")
except AlegraApiError as e:
print(f"An error occurred: {e}")
Working with Pydantic Models
This library uses Pydantic V2 for data validation. When working with models, use the Pydantic V2 API:
Serializing Models to JSON
from alegra.models.company import Company
company = Company(...)
# ✅ Correct - Use model_dump_json() (Pydantic V2)
json_string = company.model_dump_json()
# ❌ Deprecated - Avoid using json() (will be removed in Pydantic V3)
json_string = company.json() # Generates deprecation warning
Serializing Models to Dictionary
# ✅ Correct - Use model_dump() (Pydantic V2)
data_dict = company.model_dump()
# ❌ Deprecated - Avoid using dict() (will be removed in Pydantic V3)
data_dict = company.dict() # Generates deprecation warning
For more information, see the Pydantic V2 Migration Guide.
License
This project is licensed under the MIT License.
Metadata
Release files for alegra-e-provider 0.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| alegra_e_provider-0.0.9.tar.gz | 40.9 kB | With provenance | Details for alegra_e_provider-0.0.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| alegra_e_provider-0.0.9-py3-none-any.whl | Python 3 | none | any | Details for alegra_e_provider-0.0.9-py3-none-any.whl |
Total release size: 57.6 kB
Release files / alegra_e_provider-0.0.9.tar.gz
| Download URL | alegra_e_provider-0.0.9.tar.gz |
|---|---|
| Size | 40.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0120be353dd3829c45d138ed27a936b53e72d79b1a78abd9e130e830ae8ddf8e
|
|
BLAKE2b-256 checksum How to use checksums |
f0b19062dc259ccb4df4d0570854be2fc9c22f0ba75a4493efab2b7c27b50049
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / alegra_e_provider-0.0.9-py3-none-any.whl
| Download URL | alegra_e_provider-0.0.9-py3-none-any.whl |
|---|---|
| Size | 16.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b1c6058226f55c4b2f6c2e30fee57f8de5cfb8bf4b0cd490573eb35131c0aa09
|
|
BLAKE2b-256 checksum How to use checksums |
c6cf8e5093f7513a37927e6180d50fb7f80ddc4c13ca8fbbb70eaf960299f2db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for alegra-e-provider 0.0.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for alegra_e_provider-0.0.9.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.