Send siteStructure to an external API?

siteStructure seems to be just front-end code - can you send an object like the page names to a 3rd party API? How?

Hi,

Below is an example on how to send the site structure as a json over to a third party API using a POST request.

import wixSite from 'wix-site';
import {fetch} from 'wix-fetch';
  
const thirdPartyApi = "https://someaddress.com/";
let siteStructure = wixSite.getSiteStructure();
let fetchOptions = { method: 'post',body: JSON.stringify(siteStructure) };

fetch(thirdPartyApi, fetchOptions)
  .then( (httpResponse) => {
    if (httpResponse.ok) {
      console.log("ok")
    } else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then(response => console.log(response));
  .catch(err => console.log(err));
1 Like

OK, so that is great but I am imagining you would put that client-side (or you have to put that client-side), right?
The APIs in wix-site can only be used in front-end code.
That is where I am confused. I would not want to send the site structure on page load, I would want to request it from a 3rd party.

Can you show an example of how to do this server-side?

Or with client-side code that calls a server side function??

Hi Adam,

The api for wix-site can only be accessed by front end code, therefore you will need to implement a front end function to grab it and send it to the back end for further processing.

For example you could create an admin only page ( password protected) with a button to grab the site structure and store it in a collection for easy access further processing by your backend code.
Note that at the moment there is no option to create schedule function runs therefore you will need to click the button after each update to site structure.

You mentioned you want to allow external api to request the structure - for that you will need to implement wix http-functions as explained here