JavasScript Fetch to request data from API in Google Sheets

I’m trying to make a Quiz game. My thought was to have the Question and Answer data located in Google Sheets, and create an API via App Scripts. I used ChatGPT to help get me started on the JavaScript that would be needed.

In the game itself, I was trying to fetch one piece of data from the API and store in a variable and then change the Text Object to what was stored in the variable.

Here is the JavaScript that I was using, any ideas what I’m doing wrong and if I need to approach this differently any thoughts?

JavaScript
fetch(‘https://script.googleusercontent.com/macros/echo?user_content_key=YIFOC6heKnSNzrv6jIfrcvDoK5IhYLIh1ntvkSWQzVvO8CgPHmQ0RKUc0lMPhWDMi_McN3P1MkRz_RRwZX7KNkaDVUWleZSNm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnJFSXzWDldUCeM4cVLr3iys4yw7RgHzq9iZhJ9nI0VXKYEipytZAqSF28VVp1c5UJIFd2UhHPtpAzyrGrAToFTGekcn64eBJ7A&lib=MGVFr2X43f2FQh0JkR2jvdWaCc0TDZAiB’)
.then(response => response.json())
.then(data => {
if (data && data.length > 0) {
// Access the “Question Text” property. If your property name has spaces, use bracket notation like this:
const firstQuestionText = data[0][“Question Text”]; // Adjust based on your actual data structure

  // Now set this text into the "QuestionText" scene variable in GDevelop
  runtimeScene.getVariables().get("QuestionText").setString(firstQuestionText);
}

})
.catch(error => console.error(‘Error fetching data:’, error));

Additional Details:
“Question Text” = the Header of the data I’m trying to fetch from the google sheets API

“QuestionText” = the variable in Gdevelop

What does it do currently? Does the error message come up? Have you checked the console output in the developer tools for any messages?

If it helps, I call an external API and define a function to act a listener to get the response. Below are snips of how I do it - note that I use global variables, not scene variables and I’ve collapsed most of the switch cases so I can get it all in the snip.

Also, I’m not fetching but instead posting a message because the game is hosted in an external platform which listens for messages, but hopefully this will give you an idea that may lead you to solving your issue.

The listener declared in a JavaScript event:


And the JavaScript action that invokes the API call. It’s in an extension, hence the getArgument refererence, but you get the idea: