Joel Dare

Export Your Kindle Library

The instructions below will allow you to download a list of books from your Kindle Library.

// Initial setup
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""
let markdownData = ""

// Send an XHR request to retrieve a list of books sorted by acquisition_desc
function getItemsList(paginationToken = null) {
  let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=acquisition_desc&querySize=50'
  xhr.open('GET', url, false)
  xhr.send()  
}

// Request the page
xhr.onreadystatechange = function() {
  switch ( xhr.readyState ) {
    case 0:
      console.log('uninitialized')
      break
    case 1:
      console.log('loading...')
      break
    case 4:
      if(xhr.status == 200) {
        let data = xhr.responseText
        data = JSON.parse(data)
        if(data.itemsList) {
          items.push(...data.itemsList)
        }
        if(data.paginationToken) {
          getItemsList(data.paginationToken)
        }
      } else {
        console.log('Failed')
      }
      break
  }
}

getItemsList()

// Export to Markdown
items.forEach(item => {
  console.log(item);
  csvData += '"' + item.asin + '","' + item.title + '","' + item.authors[0] + '","' + item.percentageRead + '"\n'
  markdownData += '| ' + item.asin + ' | ' + item.title + ' | ' + item.authors[0] + ' | ' + item.percentageRead + ' |\n'
})

//window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData)
window.location = 'data:text/markdown;charset=utf8,' + encodeURIComponent(markdownData)

I originally got the following code from usayamadx/ExportKindle.js then modified it slightly to add author and purchase date.