Search code examples
gatsbynetlifynetlify-cmssanity

Gatsby.js - Export sanity.io data ( And maybe to netlify-cms in markdown files)


The goal

I want to migrate (maybe convert) the pages data from the old site to the new site.

The outline

  • I'm going to do a rebrand on an existing Gatsby website for a client.
  • The client has provided me with the source files for Sanity-Studio & Gatsby
  • I was able to locally build both the Sanity-Studio & the Gatsby site

The problem

  • The actual data, from what I understand, is still hosted on the sanity.io account of the previous developer.
  • I cannot access this data, not even locally.
  • I cannot add an new account to the local sanity-studio without logging in with the original account.
  • I prefer to do this automated, since there are about 100 pages in total.

The question

Part 1

  • Is there a way to receive/access this data and to be able to migrate (or export / import) it to my new (different) Gatsby site?

Part 2

  • I was planning of leaving Sanity.io out of it and use Gatsby.js + Netlify-CMS -> Can I convert all the data from Sanity.io to Markdown files, so that I can use it with Netlify-CMS?

Solution

  • First of all, feel free to contact us in our developer community or on [email protected] to see if we can figure something out when it comes to getting to your client’s content and letting you continue building with the current stack without hitches.

    I'll still try to give you some answers here.

    Part 1

    You can get a full export of the public part of the dataset at the export endpoint: curl https://<projectId>.api.sanity.io/v1/data/export/<dataset>/ > backup.ndjson. The asset documents will contain the URLs to the asset binaries that you have to download separately. Check out how we deal with it in our export-module. I note that it would be helpful if the module would let you export public data without requiring a token.

    Another approach is to use Gatsby, query the data you need and output it as markdown/JSON structured in the way you need it from a page template.

    Part 2

    If you want to convert data from Sanity into Markdown that's totally possible. Check out this demo on Codesandbox for how to go about it.