Repository navigation
Expand file tree
/
Copy pathquickstart.mjs
More file actions
50 lines (45 loc) · 1.74 KB
/
Copy pathquickstart.mjs
File metadata and controls
50 lines (45 loc) · 1.74 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// DivineAPI example: today's daily horoscope for Aries: each life area plus the daily percentages.
// Docs: https://developers.divineapi.com/horoscope-and-tarot-api/daily-horoscope-prediction
// Run (Node 18+): export DIVINEAPI_API_KEY=... DIVINEAPI_AUTH_TOKEN=... then node quickstart.mjs
// Node 20.6+ can also read the .env file: node --env-file=.env quickstart.mjs
const URL = "https://astroapi-5.divineapi.com/api/v5/daily-horoscope";
const API_KEY = process.env.DIVINEAPI_API_KEY;
const AUTH_TOKEN = process.env.DIVINEAPI_AUTH_TOKEN;
if (!API_KEY || !AUTH_TOKEN) {
console.error("Set DIVINEAPI_API_KEY and DIVINEAPI_AUTH_TOKEN (see .env.example)");
process.exit(1);
}
const now = new Date();
const pad = (n) => String(n).padStart(2, "0");
const fields = {
api_key: API_KEY,
sign: "aries",
h_day: "today",
day: pad(now.getDate()),
month: pad(now.getMonth() + 1),
year: String(now.getFullYear()),
tzone: "5.5",
};
const form = new FormData(); // multipart/form-data, which every DivineAPI endpoint expects
for (const [k, v] of Object.entries(fields)) form.append(k, v);
const res = await fetch(URL, {
method: "POST",
headers: { Authorization: `Bearer ${AUTH_TOKEN}` },
body: form,
});
const body = await res.json();
// legacy hosts return HTTP 200 even on errors: check "success" in the body
if (body.success !== 1) {
console.error("API error:", JSON.stringify(body.msg ?? body));
process.exitCode = 1;
} else {
printResult(body);
}
function printResult(body) {
const d = body.data;
console.log(d.sign, d.date);
for (const area of ["personal", "health", "profession", "emotions", "travel"]) {
console.log(`${area}: ${d.prediction[area].slice(0, 90)}...`);
}
console.log("percentages:", d.special.horoscope_percentage);
}