Condition for opening the app through the status of an external API

Question / Problem

Condition for opening the app through the status of an external API.

Current Situation

I'm having an issue with the Kintone app. I want to call an external API before entering and displaying data in the app. This API will take the login ID as a parameter. If the response returns false, no operations will be allowed in the app, and the list data of the app will not be viewable. If the response returns true, normal operations with the Kintone app will be allowed. I'm using JavaScript and CSS Customization to add a JS file to check the kintone.events.on("app.record.index.show", function (event) event, but it's not working. I would greatly appreciate any support from everyone.

Code / Attempts

kintone.events.on("app.record.index.show", function (event) {
  const user = kintone.getLoginUser();
  const query = "JavaScript";
  const apiUrl = `https://www.googleapis.com/books/v1/volumes?q=${query}`;
  const payload = {
    id: user.code,
  };
  fetch(apiUrl, {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
      // Authorization: "Bearer your_token_here",
    },
    // body: JSON.stringify(payload),
  })
    .then((response) => {
      if (response) {
        //When call api successful then the Kintone app will be allowed
        alert("The API call was successful");
        return true;
      }
    })
    .catch((error) => {
      // If call api failed, not show app kintone
      alert("Error: " + error.message);
      return false;
    });
  return event;
});
1 Like

Hello @Viet_Hoang
Welcome to the community!
I think these are the issues with the script that need to be addressed:

  1. Ensure that the fetch call is asynchronous, and the event is returned only after the API call completes.
  2. Use the return value from the fetch promise to determine whether the app's content should be displayed.
  3. Implement logic in the API call to prevent the Kintone app from proceeding if the API call fails.
1 Like

To my knowledge, when entering the app, the earliest event that is triggered is app.record.index.show. However, according to the documentation, this event is called after the record list page has been displayed. So how can I intervene before that to prevent the app from displaying or not showing any data at all if an external API call fails (i.e., the user is not allowed to interact with the app)? If you have a solution, could you please provide an example? Thank you very much.

Hello @Viet_Hoang
Since you posted a new thread with the same question, I responded there.