FAQ

How can I export all invoices for all spaces for accounting?

You can utilize our Management API to consume your information, including your invoices. Below you can find a Node.js script that uses our Storyblok JavaScript SDK to consume the endpoint of the management API. All you need to do is to replace YOUR_OAUTH_TOKEN with your own oauth token and you will receive a invoice-{timestamp}.csv in the folder.

Dependencies

npm i --save json2csv
npm i --save storyblok-js-client

Script

const StoryblokClient = require('storyblok-js-client')
const json2csv = require('json2csv').parse;
const fs = require('fs')

const Storyblok = new StoryblokClient({
  oauthToken: 'YOUR_OAUTH_TOKEN'
})

const start = async () => {
  // load space overview list
  let spacesList = await Storyblok.get(`spaces/`)
  
  // build requests for invoices for all spaces
  let invoiceRequests = []
  spacesList.data.spaces.forEach(space => {
    invoiceRequests.push(Storyblok.get(`spaces/${space.id}/invoices/`))
  }) 
  
  // execute requests and filter invoices to actual paid invoices (tatal != 0)
  let invoices = []
  let invoiceResponses = await Promise.all(invoiceRequests)
  invoiceResponses.forEach(response => {
    invoices = invoices.concat(response.data.invoices)
  })
  
  invoices = invoices.filter(invoice => {
    return invoice.total !== '0.0'
  })

  try {
    let fields = Object.keys(invoices[0])
    let csv = json2csv(invoices, { fields })

    fs.writeFileSync(`./invoices-${Date.now()}.csv`, csv);
  } catch (err) {
    console.error(err);
  }
}

start()

Execution

Save the above script as index.js and make sure the dependencies are installed before you run the following command:

node index.js