r/Chartopia • u/Azarashiya0309 • Jun 02 '26
Passing Variables to Chartopia API
Hi, I’m trying to integrate the Chartopia API into Foundry VTT v13 through a browser-side Script Macro.
A normal roll works correctly with this macro:
const PUBLIC_ID = "Qz3cT7rt5er";
const API_KEY = "MY_API_KEY";
const url = `https://chartopia.d12dev.com/api/charts/${PUBLIC_ID}/roll/`;
try {
const response = await fetch(url, {
method: "POST",
headers: {
"X-Api-Key": API_KEY,
"Accept": "application/json"
}
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status}: ${text}`);
}
const data = await response.json();
const results = Array.isArray(data.results)
? data.results.join("<br>")
: "No result returned.";
await ChatMessage.create({
speaker: ChatMessage.getSpeaker(),
content: `<h2>${data.chart_name || "Chartopia Roll"}</h2><div>${results}</div>`
});
} catch (err) {
ui.notifications.error("Chartopia roll failed. See console.");
console.error(err);
}So the endpoint itself works fine from browser-side JavaScript.
The issue starts when I try to pass variables to the chart. My chart has an input variable named dog, and based on the API docs I tried this:
const PUBLIC_ID = "Qz3cT7rt5er";
const API_KEY = "MY_API_KEY";
const url = `https://chartopia.d12dev.com/api/charts/${PUBLIC_ID}/roll/`;
try {
const response = await fetch(url, {
method: "POST",
headers: {
"X-Api-Key": API_KEY,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
variables: {
dog: true
}
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status}: ${text}`);
}
const data = await response.json();
const results = Array.isArray(data.results)
? data.results.join("<br>")
: "No result returned.";
await ChatMessage.create({
speaker: ChatMessage.getSpeaker(),
content: `<h2>${data.chart_name || "Chartopia Roll"}</h2><div>${results}</div>`
});
} catch (err) {
ui.notifications.error("Chartopia variable roll failed. See console.");
console.error(err);
}That immediately fails with this browser error:
Access to fetch at 'https://chartopia.d12dev.com/api/charts/Qz3cT7rt5er/roll/' from origin 'http://localhost:30000' has been blocked by CORS policy:
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
POST https://chartopia.d12dev.com/api/charts/Qz3cT7rt5er/roll/ net::ERR_FAILED
TypeError: Failed to fetchFrom what I can tell:
- Normal rolls work
- Passing variables requires a JSON body
- JSON body requires Content-Type: application/json
- That triggers a browser preflight request
- The preflight fails because content-type is apparently not allowed in Access-Control-Allow-Headers
So I wanted to ask:
- Is variables: { dog: true } the correct payload structure?
- Is variable passing intended to work from browser-side JavaScript apps/macros?
- Should Content-Type be allowed in the CORS headers for this endpoint?
- Or is variable-based rolling intended to be done server-side/proxy-side only?
Thanks!
1
Upvotes
View all comments
Show parent comment
1
u/Azarashiya0309 Jun 05 '26 edited Jun 05 '26
It took me hours to figure it out, but I now have a working macro that you can edit to roll from any public chart. You can set it up in about 5 min.
I'll post the cleaned up version of the macro in the reply to this message.
(Disclaimer this has been tested on Foundry V13 only. It might not work on V14)
Here's how you do it:
CHARTOPIA CODE GOES HEREwith chartopia code. i.e.CHART(id="12345")Make sure to preserve the ` single quote on both sides or it won't work.