简体   繁体   中英

How do I export an entire space in storyblok in js?

From reading up on the management API, I think I should be able to fetch data from storyblok from inside my js. The first thing I'm trying is to export my entire space so that I can have an external backup. From reading the documentation, I think the following should work, but it gives me a 401. What is the correct syntax for this?

  // spaceId is discovered in Settings / Space
  fetch(
     `https://mapi.storyblok.com/v2/spaces/${spaceId}/export.json`,
     {
       headers: {
         Authorization: managementToken, // this was created in My Account / Account Settings / Personal access Token
       },
     }
  )
  .then(async (res) => {
    const json = await res.json()
    console.log(json)
  })
  .catch((err) => console.log(err));

I was also looking to export a single story, which I think the correct URL should be:

`https://mapi.storyblok.com/v2/spaces/${spaceId}/stories/${storyId}/export.json`

I can't figure out how to determine the storyId, though. I tried the UID but that didn't work and the example showed an 8 digit number. Where do I find this number?

Note: I'm in the US, and for the regular fetches I had to use the domain https://api-us.storyblok.com so I tried adding -us and that didn't work.

Note: I will eventually be trying to add and modify stories in this same js file. Also, be able to "restore" the entire space if necessary. I hope the solution to the above will be applicable to all the rest of the calls I'll be attempting.

Note: The app is written in Nuxt 3 and I'm using useStoryblok() successfully to retrieve data. I could fulfill the above requirement to back up the entire space by iterating through everything there, but that seems like more work than is necessary, and it doesn't solve my problem with the other calls I need to make.

The management API is still on v1 , it's only the content API that's v2 : https://www.storyblok.com/docs/api That probably should help with your 401. I actually also assumed I had to use v2 :)

As how to get the story ID: yes, it's the simple ID, not the UUID. You get it by listing some stories .../v2/spaces/SPACEID/stories?with_slug=abc .

Or look at the draft/published JSON in the Storyblok UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM