Building a Headless CMS with Strapi and Gatsby Frontend
Elementary, my dear developer! We'll create a headless CMS using Strapi (backend API) and integrate it seamlessly with a Gatsby frontend (static site generator with GraphQL). This setup is perfect for blogs, portfolios, or e-commerce sites: Strapi handles content management, Gatsby fetches data via GraphQL and generates blazing-fast static pages.
Prerequisites
Node.js (v18+)
Yarn or npm (I'll use Yarn for speed)
Basic familiarity with CLI
We'll build a simple blog example with posts (title, content, slug, image).
Step 1: Set Up Strapi (Headless CMS Backend)
1.1 Create Strapi Project
bash
npx create-strapi-app@latest my-strapi-blog --quickstart
cd my-strapi-blog
This sets up Strapi v5 with SQLite (dev-friendly). It auto-starts at http://localhost:1337/admin.
1.2 Create Content Type: "Post"
Open http://localhost:1337/admin in your browser.
Log in (create super admin on first run).
Go to Content-Type Builder > Create new collection type:
Display name: Post
Add fields:
Field
Type
Config
title
Text
Required, Unique
content
Rich Text
-
slug
UID
From title
image
Media
Single, Images
publishedAt
Date
- (auto for publish)
Save & Publish.
1.3 Permissions & Public Access
Go to Settings > Roles > Public.
Enable find and findOne for Post.
Restart Strapi: yarn develop.
1.4 Add Sample Data
In Content Manager > Post > Create entries:
Title: "My First Post", Slug: "first-post", add content & image.
Your API is ready! Test: http://localhost:1337/api/posts?populate=* (fetches with images).
Pro Tip: For production, deploy to Render/ Railway (free tiers) and set env vars for DB (PostgreSQL).
Step 2: Set Up Gatsby Frontend
2.1 Create Gatsby Site
bash
yarn create gatsby my-gatsby-blog
cd my-gatsby-blog
yarn develop # Runs at http://localhost:8000
2.2 Install Strapi Plugin
bash
yarn add gatsby-source-strapi
2.3 Configure Gatsby (gatsby-config.js)
js
module.exports = {
siteMetadata: {
title: 'My Strapi + Gatsby Blog',
},
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`, // Your Strapi URL
collectionTypes: [{ name: `Post`, endpoint: `posts` }],
singleTypes: [], // For pages/home if needed
loginData: { // If auth needed (public is fine here)
identifier: ``,
password: ``,
},
},
},
`gatsby-plugin-image`,
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
{
resolve: `gatsby-plugin-mdx`, // For rich text
options: { extensions: [`.mdx`, `.md`] },
},
],
};