Getting started with Sanity as a headless CMS
Tags: javascript, cms, webdev, beginners
Published: 2018-11-13 12:36:05

---
title: Getting started with Sanity as a headless CMS
published: true
description: Sanity is the perfect companion for when you want to get started with the JAMstack, or just need API for your project. In this tutorial you'll learn how to get started with using Sanity — from configuring the React editor with JavaScript, to querying the API and migrating the content datasets.
tags: javascript, cms, webdev, beginners
canonical_url: https://www.sanity.io/blog/getting-started-with-sanity-as-a-headless-cms
cover_image: https://cdn.sanity.io/images/3do82whm/production/102b140f79bf19fb6abf35a91350be5e25cf1e62-2825x1590.png
---

_Sanity is the perfect companion for when you want to get started with the JAMstack, or just need API for your project. In this tutorial you'll learn how to get started with using Sanity — from configuring the React editor with JavaScript, to querying the API and migrating the content datasets._

If you find yourself in need of some backend, the chances are that Sanity is one of the quickest ways to get started without later punishing you. Spin up a new Sanity project for your weekend project. For that time you need an API to test something quickly. Or you can use it for more serious stuff. For example, when you need a conduit between a legacy system and the technology you prefer to use. And when you need humans to be in control of digital content, Sanity has an open source editor, that you can configure with simple JavaScript, customize with React, and host anywhere you want – be it [Netlify][1], [Now][2], or on Sanity's cloud. 

It's one of the best and well thought-through content backends I've worked with, and that's why I now work for Sanity – and loving every single day of it. 

{% youtube 2ceM_tSus_M %}
_Watch the video "Getting Started with Sanity"_

You can create as many free Sanity projects you want and get a lot done within the developer tier, and the [pricing][3] is transparent and flexible. 

You should be up and running within minutes – just use your Google- or GitHub-login, accept the [Terms of Service][4], and that's it. Begin with installing the Sanity CLI and initiate a new project with:

`npm i -g @sanity/cli && sanity init`

The instructions of the `sanity init` command will take you through creating a project and downloading the Sanity Studio locally. If you run `sanity start` in your project folder, the CLI will open a local server with hot-module reloading so you can configure it without having to refresh the browser manually. The Studio is always connected to the hosted real-time backend, which means, that as soon as you begin working with content in it, that content will also be available and queryable in the API. In other words, it's swift to get a project going where your editor can begin working with content, that you can fetch and use in your project.

It can be useful to try some of the preconfigured templates the first time, but to show you how easy it is to configure the Studio, let me show you the code for a simple post entry:
    
```js
    export default {
      name: 'post',
      title: 'Post',
      type: 'document',
      fields: [
        {
          name: 'title',
          title: 'Title',
          type: 'string'
        }
      ]
    }
```

The Studio will now look like this:

![][5]

If you don't care about the Studio, that's completely OK. You can just as easily use Sanity's patch API to create documents and edit them. To make a queryable document, it just needs a [write token][6], a `_type` key, and there are some conventions for different data types that you'll learn pretty quickly. Using the [JavaScript client][7], you can create a document like this:
    
```js    
    import sanityClient from '@sanity/cli'
    const client = sanityClient({
      projectId: '',
      dataset: '',
      token: ''
    })
    
    client.create({ _type: "post", title: "Hello World" }).then(console.log)
    > { _createdAt: '2018-11-10T20:06:56Z',
    _id: 'wK0NtpsoCZRvwU3EDtEub2',
    _rev: 'wK0NtpsoCZRvwU3EDtEuYI',
    _type: 'test',
    _updatedAt: '2018-11-10T20:06:56Z',
    title: 'hello world' }
```

And if you want to change (or add another) value, you run:
    
```js    
    client.patch('wK0NtpsoCZRvwU3EDtEub2').set({ title: 'Hello Worlds!'}).then(console.log)
    >_createdAt: '2018-11-10T20:06:56Z',
    _id: 'wK0NtpsoCZRvwU3EDtEub2',
    _rev: 'FUGFhuJJo756q4bO12e29f',
    _type: 'test',
    _updatedAt: '2018-11-10T20:10:31Z',
    title: 'Hello Worlds!' }
```

You can use Sanity CLI and create a new document with sanity documents create and write the data in your favorite text editor. 

## Querying data with GROQ

Now that you have a document you can query it with the API using GROQ (GRaph Oriented Query language). It is a declarative language designed to query collections of largely schema-less JSON documents. Its primary design goals are expressive filtering, joining of several documents into a single response, and shaping the response to fit the client application. To try it out, you can also do this right in the CLI when you are in a project folder, like this:
    
```js    
    sanity documents query "[*_type == 'post']"
```

When you want to query content from your frontend, you can use one of the client libraries. There are libraries for [PHP][8], [.NET][9], and [JavaScript][10], with the latter one, you fetch documents like this:
    
```js      
    client.fetch("[*_type == 'post']").then(documents => console.log(documents))
```

You can also type this into your browser and see the data there, or use anything that can send HTTPS requests: `https://.api.sanity.io/v1/data/query=[*_type == "post"]`

With GROQ you can also do something called [_projections_][11], where you have a lot of control over how your data is formatted. It's used by inserting curly brackets `{}` after a filter, where you can select the fields you want inside them. For example, if we wanted to only include the `title` field for our posts, we could do this: 

```js        
    sanity documents query "[*_type == 'post']{title}"
```

GROQ is a powerful query language, you can learn more about it by [exploring the documentation][12].

## Migrating your content is easy

The whole point of a hosted content backend is that it should be simple for you [to migrate, duplicate, and move your data][13]. Especially if you want to use it for something in production, you should have an easy way of duplicating that content into an environment where you can test and experiment. In Sanity, it's straightforward to export all your content and assets in a neat [ndjson-file][14] (and a folder with all your assets), using the CLI:
    
```js    
    sanity dataset export 
```

And vice versa:
    
```js    
    sanity dataset import datasetExport.ndjson
```

You can even get an instant ndjson dump of all your content using the export API (it's 🚀 fast too!): `https://PROJECT_ID.api.sanity.io/v1/data/export/DATASET_NAME`

## Next steps...

You should now have a basic understanding of the stuff you can do with Sanity. There's a lot more to discover, but the nice thing is that you can do it step by step as your needs and ambitions rise. You may want to learn more about how you can [completely tailor your studio for your clients need][15], or [take advantage of the rich image metadata][16], or dive into some advanced [patching and mutation techniques][17]. You can also check out the frontend example in [React Native][18], [Vue][19], [Next.js][20], and [Nuxt][21]. 

Whatever you choose to use Sanity for, there is also a [community][22] that would love to hear about it and offers help if some roadblock comes in your way. 

[1]: https://netify.com
[2]: https://zeit.co/now
[3]: https://www.sanity.io/pricing
[4]: https://www.sanity.io/legal/tos
[5]: https://cdn.sanity.io/images/3do82whm/production/7ee2991c9e70b10d3b7545a82ea85006dc006f41-2360x1644.png
[6]: https://www.sanity.io/docs/http-auth
[7]: https://www.sanity.io/docs/client-libraries/js-client
[8]: https://packagist.org/packages/sanity/sanity-php
[9]: https://github.com/oslofjord/sanity-linq
[10]: https://www.npmjs.com/package/@sanity/client
[11]: https://www.sanity.io/docs/groq/groq-pipeline-components
[12]: https://www.sanity.io/docs/reference/groq
[13]: https://www.sanity.io/docs/migrating-data
[14]: http://ndjson.org/
[15]: https://www.sanity.io/docs/content-studio/structure-builder
[16]: https://css-tricks.com/five-interesting-ways-to-use-sanity-io-for-image-art-direction/
[17]: https://www.sanity.io/docs/http-api/http-patches
[18]: https://github.com/sanity-io/example-app-react-native
[19]: https://github.com/sanity-io/example-frontend-vue-js
[20]: https://github.com/sanity-io/example-frontend-next-js
[21]: https://github.com/sanity-io/example-ecommerce-snipcart-vue
[22]: https://slack.sanity.io