-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
29 lines (25 loc) · 957 Bytes
/
Copy pathnode.js
File metadata and controls
29 lines (25 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// CFE API - Node.js example (Node 18+, uses built-in fetch).
const API = "https://cfe-api.fly.dev";
const API_KEY = process.env.CFE_API_KEY;
async function consulta(rpu, nombre) {
const r = await fetch(`${API}/api/v1/consulta`, {
method: "POST",
headers: { "X-API-Key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ rpu, nombre }),
});
if (!r.ok) throw new Error(`${r.status} ${(await r.json()).error}`);
return r.json();
}
async function balance() {
const r = await fetch(`${API}/api/v1/balance`, {
headers: { "X-API-Key": API_KEY },
});
return r.json();
}
(async () => {
const result = await consulta("123456789012", "JUAN PEREZ");
const { data } = result;
console.log(`Tarifa: ${data.tarifa} Anual: ${data.annual_kwh} kWh`);
console.log(`Hilos: ${data.hilos ?? "no disponible"}`);
console.log(`Cobrado: ${(result.charged_cents / 100).toFixed(2)} MXN Cache: ${result.cached}`);
})();