-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode-example.js
More file actions
56 lines (48 loc) · 1.57 KB
/
Copy pathnode-example.js
File metadata and controls
56 lines (48 loc) · 1.57 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
51
52
53
54
55
56
// ProvChart Developer API — Node.js example
// Docs: https://chart.devtem.org/docs#dev-api-overview
//
// Usage:
// PROVCHART_API_KEY=pc_live_xxxx node node-example.js
const API_URL = "https://provchart-api.devtem.org/api/v1/generate";
async function generateChart() {
const res = await fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": process.env.PROVCHART_API_KEY,
},
body: JSON.stringify({
type: "line",
series: [
{ name: "Revenue", color: "#8b7bff", points: [20, 35, 45, 30, 50] },
{ name: "Cost", color: "#4fd8c4", points: [15, 28, 30, 22, 40] },
],
axisX: ["Jan", "Feb", "Mar", "Apr", "May"],
}),
});
const data = await res.json();
if (!data.success) {
// See error codes: INVALID_API_KEY, SUBSCRIPTION_REQUIRED, MONTHLY_LIMIT_REACHED
console.error(`ProvChart error [${data.code}]: ${data.error}`);
process.exit(1);
}
// data.html + data.css — static output, no chart-library runtime needed
console.log(data.html);
console.log(data.css);
return data;
}
generateChart();
// Express route example — proxy the key server-side, never ship it to the client
//
// app.post("/api/chart", async (req, res) => {
// const upstream = await fetch(API_URL, {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// "X-API-Key": process.env.PROVCHART_API_KEY,
// },
// body: JSON.stringify(req.body),
// });
// const data = await upstream.json();
// res.status(upstream.status).json(data);
// });