What Actually Is a Headless CMS? (Explanation, Examples & Code)

6 min read
What Actually Is a Headless CMS? (Explanation, Examples & Code)

If you’ve been browsing web development forums or checking out modern tech stacks recently, you’ve almost certainly bumped into the term Headless CMS. It sounds a bit gruesome, but in the world of web architecture, it’s actually quite a brilliant concept.

If you've been browsing web development forums or checking out modern tech stacks recently, you've almost certainly bumped into the term Headless CMS. It sounds a bit gruesome, but in the world of web architecture, it's actually quite a brilliant concept.

Unlike a traditional CMS (like a standard WordPress setup) where the admin panel and the front-end website are glued together, a Headless CMS cuts that connection. It gives you full freedom to build your front end however you want, while still having a powerful backend to manage your content.

Let's break down what that actually means, why you might want to use it, and look at some real code to see how it works.

 

The Headless Concept Explained

In IT terms, the head usually refers to the front end—the website or app that the user sees and interacts with. The body is the back end—the database and content management interface.

Traditional CMS (Coupled): Think of a classic WordPress site. You install themes, and the CMS controls exactly how the site looks and how the content is stored. They are inseparable.

Headless CMS (Decoupled): You chop off the head (the predefined theme/front end). All that remains is the body—a raw content hub. This hub doesn't care how the content is displayed; it just waits for you to ask for data via an API.

 

Traditional vs. Headless: Key Differences

The biggest shift is in who controls the presentation layer. In a traditional CMS, your backend and frontend are married to each other. You edit content in WordPress, and it automatically shows up on your WordPress theme.

With Headless CMS, the backend only stores your content and serves it through an API (usually REST or GraphQL). Your frontend can be anything—a React app, a Vue.js site, a mobile app, or even a smart watch interface. The CMS doesn't dictate how things look; it just hands over the raw data.

Traditional CMS is limited by the theme engine and requires a specific server stack (like PHP + MySQL for WordPress). It's great for quick websites and blogs where you need something running fast without much customization.

Headless CMS offers unlimited flexibility. You can build your front end in any technology, host it on a CDN for blazing speed, and even serve the same content to multiple platforms simultaneously. Most Headless platforms work in the cloud as SaaS solutions, meaning you don't have to worry about infrastructure, scaling, or security updates.

 

Why Would You Bother? (Use Cases)

You might be thinking, "If I have to build the front end from scratch, isn't that more work?" Sometimes, yes. But the trade-off is massive flexibility. Here is where Headless shines:

Omnichannel Publishing: You write a product description once. The Headless CMS serves that same description to your website, your mobile app, and maybe even a smart watch or POS system. You manage everything from one panel, but publish everywhere.

Security: Since your front end is just communicating via API, you aren't exposing your database or admin panel directly to user traffic in the same way. The attack surface is much smaller.

Performance: You can use modern static site generators (like Next.js, Gatsby, or Astro) to pull content during the build process. The result is a lightning-fast website with no database queries slowing down page loads. Pages are pre-rendered and served from a CDN.

Flexibility for Developers: Front-end developers can work in React, Vue, or whatever framework they prefer, while content editors use a familiar CMS interface. Everyone stays in their comfort zone.

Note: You can actually use WordPress as a Headless CMS. You just ignore the theme folder and pull your posts via the built-in WP REST API. It's a great way to test the Headless concept without learning a new platform.

 

Code Example: Fetching Data

Let's look at the code. In a traditional setup, you'd write a PHP loop inside a theme file. In a Headless setup, you just fetch the data using JavaScript (or any language you prefer).

Here is a simple example of how you might pull the latest blog posts from a Headless source (like a WordPress REST API) using vanilla JavaScript:

javascript
const apiUrl = 'https://your-headless-site.com/wp-json/wp/v2/posts?per_page=3';

async function loadPosts() {
    try {
        const response = await fetch(apiUrl);
        
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }

        const posts = await response.json();
        const container = document.getElementById('blog-container');
        
        posts.forEach(post => {
            const article = document.createElement('article');
            article.innerHTML = `
                <h2>${post.title.rendered}</h2>
                <div class="content">${post.excerpt.rendered}</div>
                <a href="${post.link}">Read more</a>
            `;
            container.appendChild(article);
        });

    } catch (error) {
        console.error('Error fetching posts:', error);
    }
}

loadPosts();

In this scenario, your website doesn't need to know anything about WordPress database logic. It just asks for a list of posts and puts them on the screen. You could swap out WordPress for Contentful, Strapi, or any other Headless CMS without changing your frontend code much.

 

Popular Headless CMS Platforms

If you're curious about which platforms to try, here are some popular options:

Strapi: Open source, self-hosted, highly customizable. Great if you want full control over your infrastructure.

Contentful: Cloud-based, excellent for multi-platform delivery. Popular in enterprise environments.

Ghost: Originally built for blogging, clean SEO, minimal setup. Perfect for content-first sites.

Sanity: Real-time collaboration, flexible content modeling. Loved by agencies and large teams.

Each platform has different strengths depending on your project size, technical skills, and budget.

 

Summary

Headless CMS isn't a magic bullet for every project. If you need a simple brochure site for a local business, a standard page builder is probably faster and cheaper.

But if you are building a project that needs to scale, serve content to multiple platforms, or you just want total freedom to design a custom front end without fighting a CMS's theming engine, going Headless is the way forward. It separates the content management (for the editors) from the code (for the developers), keeping everyone happy.