Skip to content

Instantly share code, notes, and snippets.

@erajuan
Last active May 18, 2024 21:05
Show Gist options
  • 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:
BASE_URL = "https://api.apis.net.pe"
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": "https://apis.net.pe/api-tipo-cambio.html"
}
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")
logging.warning(response.text)
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")
else:
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})
@erajuan
Copy link
Author

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 
print(api_consultas.get_person("46027897"))
# Api Consulta ruc sunat
print(api_consultas.get_company("10460278975"))
# Api consulta tipo de cambio del dia sunata
print(api_consultas.get_exchange_rate_today())
# Api sunat tipo de cambio en sunat para una fecha especifica
print(api_consultas.get_exchange_rate("2021-08-23"))
# 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 Apis.net.pe/app

@erajuan
Copy link
Author

erajuan commented May 17, 2024

Tipo de cambio sunat

servicio JSON que permite acceder al tipo de cambio de la sunat de una manera mas simple:

https://apis.net.pe/api-tipo-cambio.html

  • Diario o fecha especifica
  • TIpo de cambio de todo el mes

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