Last active
August 30, 2025 13:42
-
-
Save erajuan/1d43b275290ee3962a8a2f07e80f48c3 to your computer and use it in GitHub Desktop.
Consulta de api ruc sunat, api dni reniec y tipo de cambio sunat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import List, Optional | |
| import logging | |
| import requests | |
| class DecolectaAPIClient: | |
| """ | |
| Cliente para consumir los servicios públicos de la API de Decolecta. | |
| """ | |
| def __init__(self, api_token: str = None) -> None: | |
| self.api_token = api_token | |
| self.base_url = "https://api.decolecta.com" | |
| def _get(self, endpoint: str, params: dict) -> Optional[dict]: | |
| """ | |
| Realiza una solicitud GET autenticada al endpoint indicado. | |
| """ | |
| url = f"{self.base_url}{endpoint}" | |
| headers = { | |
| "Authorization": f"Bearer {self.api_token}", | |
| "Referer": "python-decolecta", | |
| } | |
| response = requests.get(url, headers=headers, params=params) | |
| if response.status_code == 200: | |
| return response.json() | |
| elif response.status_code == 422: | |
| logging.warning(f"{response.url} - Parámetros inválidos: {params}") | |
| logging.warning(response.text) | |
| elif response.status_code == 403: | |
| logging.warning(f"{response.url} - Acceso denegado: IP bloqueada") | |
| elif response.status_code == 429: | |
| logging.warning(f"{response.url} - Demasiadas solicitudes: aplicar retardo") | |
| elif response.status_code == 401: | |
| logging.warning(f"{response.url} - Token inválido o sin permisos") | |
| else: | |
| logging.warning( | |
| f"{response.url} - Error del servidor {response.status_code}" | |
| ) | |
| return None | |
| def get_person(self, dni: str) -> Optional[dict]: | |
| """ | |
| Consulta datos personales por DNI (RENIEC). | |
| """ | |
| return self._get(endpoint="/v1/reniec/dni", params={"numero": dni}) | |
| def get_company(self, ruc: str) -> Optional[dict]: | |
| """ | |
| Consulta información de empresa por RUC (SUNAT). | |
| """ | |
| return self._get(endpoint="/v1/sunat/ruc", params={"numero": ruc}) | |
| def get_exchange_rate_by_date(self, date: str) -> Optional[dict]: | |
| """ | |
| Obtiene el tipo de cambio para una fecha específica (formato YYYY-MM-DD). | |
| """ | |
| return self._get(endpoint="/v1/tipo-cambio/sunat", params={"date": date}) | |
| def get_exchange_rate_sunat_today(self) -> Optional[dict]: | |
| """ | |
| Obtiene el tipo de cambio del día actual. | |
| """ | |
| return self._get(endpoint="/v1/tipo-cambio/sunat", params={}) | |
| def get_average_exchange_rate_sbs(self, currency: str) -> Optional[dict]: | |
| """ | |
| Obtiene el tipo de cambio promedio de la SBS. | |
| """ | |
| return self._get( | |
| endpoint="/v1/tipo-cambio/sbs/average", params={"currency": currency} | |
| ) | |
| def get_accounting_exchange_rate_sbs(self, currency: str) -> Optional[dict]: | |
| """ | |
| Obtiene el tipo de cambio contable de la SBS. | |
| """ | |
| return self._get( | |
| endpoint="/v1/tipo-cambio/sbs/accounting", params={"currency": currency} | |
| ) | |
| def get_exchange_rate_sunat_by_month( | |
| self, month: int, year: int | |
| ) -> Optional[List[dict]]: | |
| """ | |
| Obtiene el tipo de cambio para un mes y año específicos. | |
| """ | |
| return self._get( | |
| endpoint="/v1/tipo-cambio/sunat", params={"month": month, "year": year} | |
| ) | |
| def get_average_exchange_rate_sbs_by_month( | |
| self, currency: str, month: int, year: int | |
| ) -> Optional[List[dict]]: | |
| """ | |
| Obtiene el tipo de cambio de la SBS para un mes y año específicos. | |
| """ | |
| return self._get( | |
| endpoint="/v1/tipo-cambio/sbs/average", | |
| params={"currency": currency, "month": month, "year": year}, | |
| ) | |
| def get_accounting_exchange_rate_sbs_by_month( | |
| self, currency: str, month: int, year: int | |
| ) -> Optional[List[dict]]: | |
| """ | |
| Obtiene el tipo de cambio de la SBS para un mes y año específicos. | |
| """ | |
| return self._get( | |
| endpoint="/v1/tipo-cambio/sbs/accounting", | |
| params={"currency": currency, "month": month, "year": year}, | |
| ) |
para consultar DNI, como lo valido en el postman?
curl --location 'https://api.decolecta.com/v1/reniec/dni?numero=49071347' \ --header 'Authorization: Bearer sk_token_generado_desde_la_web'
De esta forma puedes hacerlo sin problemas.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
para consultar DNI, como lo valido en el postman?