Frequently Asked Questions

Using the Gatsby File System Route API with Hygraph

What is the Gatsby File System Route API and how does it help with Hygraph projects?

The Gatsby File System Route API allows you to automatically generate pages based on your data structure, significantly reducing the need for manual page creation in gatsby-node.js. When used with Hygraph, it enables you to create dynamic blog post pages by leveraging the slug field from your Hygraph content model. This approach streamlines the process of building pages from your CMS data.

How do I set up Gatsby to use Hygraph content with the File System Route API?

To set up Gatsby with Hygraph using the File System Route API, start by creating a Hygraph project (the "Blog Starter" template is recommended). Generate an API token for accessing your content. In your Gatsby project, install the gatsby-source-graphcms plugin and configure it in gatsby-config.js with your Hygraph endpoint and token, preferably using environment variables stored in .env.local. This setup allows Gatsby to source content from Hygraph for page generation.

How do I create dynamic blog post pages using collection routes in Gatsby?

To create dynamic blog post pages, place a file named {graphCmsPost.slug}.js in your src/pages directory. This file uses Gatsby's collection route syntax, where graphCmsPost is your GraphQL type and slug is the unique field. Inside this file, export a page query that fetches the blog post by slug, and render the post's title, date, content, and author. This setup enables Gatsby to automatically generate a page for each blog post in your Hygraph project.

What are the benefits of using environment variables for API configuration in this setup?

Using environment variables (such as in a .env.local file) for your Hygraph API endpoint and token helps keep sensitive information secure and makes it easier to manage different environments (development, staging, production). This practice is recommended for maintaining security and flexibility in your Gatsby-Hygraph integration.

Where can I find a complete example of using the Gatsby File System Route API with Hygraph?

A complete example is available on GitHub at hygraph-examples/with-gatsby-filesystem-routing. This repository demonstrates the full setup, including project structure, configuration, and code for generating pages from Hygraph content using Gatsby's File System Route API.

5 proven Claude + Hygraph MCP workflows that actually save you time

Using the Gatsby File System Route API with Hygraph

We'll use Gatsby collection routes to automatically create our blog post pages using the slug field.
Jamie Barton

Last updated by Jamie 

Jun 12, 2026

Originally written by Jamie 

May 06, 2021
Gatsby File System Route API page generation

It wasn't too long ago Gatsby introduced the File System Route API to a handful of users to test. Since then, this feature landed in Gatsby for all, and you use it to significantly reduce boilerplate when creating pages programmatically.

In most cases, you can get by without ever needing to create another gatsby-node.js file in your project, if all you are doing is building pages from data.

In this short post, we'll explore using "collection routes" to build pages for our blog posts.

The easiest way to follow along with this post is to create a new Hygraph project using the "Blog Starter" template.

Hygraph project creation screen highlighting the Blog Starter template for the Gatsby File System Route API guide

Next, you'll want to configure a new API token for accessing DRAFT, or PUBLISHED entries, or both.

Inside of your Gatsby project, or if you're starting from scratch, install the gatsby-source-graphcms dependency:

npm install -E gatsby-source-graphcms

Then add the plugin, along with your API endpoint + token to gatsby-config.js:

require("dotenv").config();
module.exports = {
plugins: [
{
resolve: "gatsby-source-graphcms",
options: {
endpoint: process.env.GRAPHCMS_ENDPOINT,
token: process.env.GRAPHCMS_TOKEN,
stages: ["DRAFT"],
},
},
],
};

You'll notice we're using ENV variables above. You should create the file .env.local and add the values there, in the format of:

GRAPHCMS_ENDPOINT=...
GRAPHCMS_TOKEN=...

At this point, you can open GraphiQL to explore the generated docs, but if we stick to the default fragments the plugin creates for us, we can continue...

Inside the directory src/pages, create the file {graphCmsPost.slug}.js - here we're using dot notation to reference our GraphQL type graphCmsPost and the field slug.

Inside the file, we can export a page query that fetches our blog post by slug. The slug variable is passed from the file name (route) to our query.

Add the following query to the file:

import React from "react";
import { graphql } from "gatsby";
export const pageQuery = graphql`
query PostPageQuery($slug: String!) {
graphCmsPost(slug: { eq: $slug }) {
title
date
content {
html
}
author {
name
}
}
}
`;

Then all that's left to do is render something to the page with our title, date, content, and author.

export default function PostPage({ data: { graphCmsPost } }) {
return (
<React.Fragment>
<h1>{graphCmsPost.title}</h1>
<p>
By {graphCmsPost.author.name} on {graphCmsPost.date}
</p>
<div dangerouslySetInnerHTML={{ __html: graphCmsPost.content.html }} />
</React.Fragment>
);
}

That's it! See this example over on GitHub to learn more.

Blog Author

Jamie Barton

Jamie Barton

Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.

Share with others

Sign up for our newsletter!

Be the first to know about releases and industry news and insights.