JavaScript is required to view this website properly.

Step-by-Step Guide: How to Build a Blog with Sanity CMS

2026-02-04 · 3 min read

If you've been curious about using a headless CMS for your next project, you're in the right place. This guide walks you through setting up Sanity, a flexible CMS that makes content management easy. By the end, you'll have a working blog powered by Sanity and Gatsby.


Step 1: Setting Up Your Sanity Project

1.1 Install the Sanity CLI

Make sure Node.js and npm are installed. Then run:

bash
npm install -g @sanity/cli

1.2 Create a New Sanity Project

Create a new project:

bash
sanity init

Choose:

  • Project Name: my-sanity-blog
  • Dataset: production
  • Template: Clean project with no predefined schemas

Then move into the project:

bash
cd my-sanity-blog

1.3 Launch Sanity Studio

Start the studio:

bash
sanity start

Open http://localhost:3333 to see the studio.


Step 2: Creating Your First Schema

2.1 Create blogPost.js

Inside the schemas folder, create blogPost.js:

js
export default {
  name: 'blogPost',
  type: 'document',
  title: 'Blog Post',
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Title',
    },
    {
      name: 'slug',
      type: 'slug',
      title: 'Slug',
      options: { source: 'title', maxLength: 96 },
    },
    {
      name: 'body',
      type: 'array',
      title: 'Body',
      of: [{ type: 'block' }],
    },
    {
      name: 'mainImage',
      type: 'image',
      title: 'Main Image',
      options: { hotspot: true },
    },
    {
      name: 'author',
      type: 'reference',
      to: [{ type: 'author' }],
    },
    {
      name: 'publishedAt',
      type: 'datetime',
      title: 'Published At',
    },
  ],
};

2.2 Register the Schema

Update schema.js:

js
import createSchema from 'part:@sanity/base/schema-creator';
import schemaTypes from 'all:part:@sanity/base/schema-type';
import blogPost from './blogPost';

export default createSchema({
  name: 'default',
  types: schemaTypes.concat([blogPost]),
});

Restart the studio:

bash
sanity start

You should now see Blog Post as a document type.


Step 3: Connecting Sanity with Gatsby

3.1 Create a Gatsby Project

bash
npm install -g gatsby-cli
gatsby new my-gatsby-blog
cd my-gatsby-blog

3.2 Install the Sanity Source Plugin

bash
npm install gatsby-source-sanity

3.3 Configure gatsby-config.js

js
require('dotenv').config();

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-sanity',
      options: {
        projectId: 'your_project_id',
        dataset: 'production',
        apiVersion: '2023-11-12',
        token: process.env.SANITY_TOKEN,
        useCdn: true,
      },
    },
  ],
};

Add your token in .env:

bash
SANITY_TOKEN=your_sanity_token

3.4 Query Sanity Data

Create src/pages/blog.js:

jsx
import React from 'react';
import { graphql } from 'gatsby';

export const query = graphql`
  query {
    allSanityBlogPost {
      nodes {
        title
        slug {
          current
        }
        body {
          children {
            text
          }
        }
        publishedAt
      }
    }
  }
`;

const BlogPage = ({ data }) => {
  const posts = data.allSanityBlogPost.nodes;
  return (
    <div>
      <h1>My Blog</h1>
      {posts.map((post) => (
        <div key={post.slug.current}>
          <h2>{post.title}</h2>
          <p>{post.body[0]?.children[0]?.text}</p>
        </div>
      ))}
    </div>
  );
};

export default BlogPage;

3.5 Run Gatsby

bash
gatsby develop

Visit http://localhost:8000/blog.


Step 4: Deploying Your Site

You can deploy to Netlify or Vercel:

  1. Push to GitHub
  2. Connect the repo
  3. Deploy

Conclusion

That’s it — you now have a working blog with Sanity as a CMS and Gatsby as the frontend. If you want help customizing the schema or UI, feel free to reach out.

FAQ

Common questions about this kind of project

Short answers for teams evaluating a similar build, migration, or optimization project.

Can you build a project like "Step-by-Step Guide: How to Build a Blog with Sanity CMS"?+
Yes. I work on similar full-stack implementations and can scope architecture, delivery timeline, and rollout based on your product goals.
Do you provide remote development support for Engineering?+
Yes. I collaborate remotely with async updates, milestone-based delivery, and timezone-friendly communication.
Can you improve performance and SEO for this kind of project?+
Yes. I optimize Core Web Vitals, technical SEO, structured data, and information architecture for better visibility and conversion.
How do we start?+
Share your current stack and goals on the contact page. I will propose an audit plan with scope, timeline, and implementation path.

Want to build something similar?

I’m available for freelance and full‑time roles. Let’s talk.