Skip to content

Instantly share code, notes, and snippets.

Last active December 1, 2023 16:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erajuan/1d43b275290ee3962a8a2f07e80f48c3 to your computer and use it in GitHub Desktop.
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
from typing import List, Optional
import logging
import requests
class ApisNetPe:
def __init__(self, token: str = None) -> None:
self.token = token
def _get(self, path: str, params: dict):
url = f"{self.BASE_URL}{path}"
headers = {
"Authorization": self.token,
"Referer": ""
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} - invalida parameter")
elif response.status_code == 403:
logging.warning(f"{response.url} - IP blocked")
elif response.status_code == 429:
logging.warning(f"{response.url} - Many requests add delay")
elif response.status_code == 401:
logging.warning(f"{response.url} - Invalid token or limited")
logging.warning(f"{response.url} - Server Error status_code={response.status_code}")
return None
def get_person(self, dni: str) -> Optional[dict]:
return self._get("/v2/reniec/dni", {"numero": dni})
def get_company(self, ruc: str) -> Optional[dict]:
return self._get("/v2/sunat/ruc", {"numero": ruc})
def get_exchange_rate(self, date: str) -> dict:
return self._get("/v2/sunat/tipo-cambio", {"fecha": date})
def get_exchange_rate_today(self) -> dict:
return self._get("/v2/sunat/tipo-cambio", {})
def get_exchange_rate_for_month(self, month: int, year: int) -> List[dict]:
return self._get("/v2/sunat/tipo-cambio", {"month": month, "year": year})
Copy link

erajuan commented Aug 24, 2021

from apis_net_pe import ApisNetPe
# Usar token personal
APIS_TOKEN = "apis-token-1.aTSI1U7KEuT-6bbbCguH-4Y8TI6KS73N"

api_consultas = ApisNetPe(APIS_TOKEN)
# Api Consulta reniec dni 
# Api Consulta ruc sunat
# Api consulta tipo de cambio del dia sunata
# Api sunat tipo de cambio en sunat para una fecha especifica
# Consulta de api sunat - tipo de cambio de un mes en especifico
print(api_consultas.get_exchange_rate_for_month(month=8, year=2021))

Generar tu token en

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment