Integrate Next.js with Storyblok
Use Storyblok to manage the content of your Next.js project. Follow this guide to learn how to fetch content, register components, set up previews, configure a catch-all route, and build a content model.
Quickstart
Section titled “Quickstart”The fastest way to get started is using Storyblok’s Core Blueprint or CLI wizard:
Use Storyblok’s Core Blueprint to launch a Next.js project, complete with a space, GitHub repository, and deployment environment.
Log in to Storyblok, select + Add Space → Start from Blueprint, and follow the steps.
Use Storyblok’s CLI to set up a new space and scaffold a Next.js project. In the terminal, run:
storyblok create --template nextjsManual setup
Section titled “Manual setup”Alternatively, set up the project from scratch and configure your frontend code.
This guide has been tested with the following package versions:
next@16.4.6react@19.3.0react-dom@19.3.0@storyblok/react@8.0.0@storyblok/api-client@0.14.0Node.js v24.22.1
-
Create a new space
Log in to Storyblok, select + Add Space → New Space, and follow the steps.
-
Create a Next.js project
Create a new Next.js project with JavaScript and a
srcdirectory. In the terminal, run:Terminal window npx create-next-app@latest my-app --js --src-dir --no-cache-componentsThe pages in this guide fetch data while they render, so the project needs Cache Components turned off. For other options, refer to the official Next.js installation guide.
-
Install
@storyblok/reactand@storyblok/api-clientIn the terminal,
cdinto the project, and install dependencies.Terminal window npm install @storyblok/react @storyblok/api-client
Next, in the root of your project, create a .env file with the access token from your space.
STORYBLOK_DELIVERY_API_TOKEN=<YOUR-ACCESS-TOKEN>In src/lib, create a file named storyblok.js and initialize the Storyblok API client and React SDK.
import { createApiClient } from "@storyblok/api-client";import { defineStoryblokBlocks } from "@storyblok/react";
export const apiClient = createApiClient({ accessToken: process.env.STORYBLOK_DELIVERY_API_TOKEN, region: "eu", // Choose the correct region of your space.});
export const { StoryblokBlock } = defineStoryblokBlocks(() => ({ components: {},}));Set the region that matches the server location of your Storyblok space.
This guide uses @storyblok/api-client to fetch data. Alternatively, use storyblok-js-client, or the native fetch API.
Fetch a single story
Section titled “Fetch a single story”Replace the code in src/app/page.js with the following.
import { notFound } from "next/navigation";import { apiClient, StoryblokBlock } from "@/lib/storyblok";import { Suspense } from "react";
export default async function Home() { const storyPromise = apiClient.stories.get("home", { query: { version: "draft" } });
return ( <Suspense fallback={<main>Loading...</main>}> <PageContent storyPromise={storyPromise} /> </Suspense> );}
async function PageContent({ storyPromise }) { const { data, error } = await storyPromise;
if (error?.response.status === 404) { notFound(); } if (error) { throw error; }
return <StoryblokBlock block={data.story.content} />;}This page fetches the home story of your Storyblok space. The PageContent component awaits the request and streams in once it resolves, while Suspense shows the fallback in the meantime.
The API client returns a missing story as a 404 error. PageContent calls notFound() for it and throws every other error, such as an invalid access token, so it doesn’t show as a missing page.
StoryblokBlock renders content type and nestable blocks with the component registered under the block’s technical name. The home story uses the page content type, so StoryblokBlock looks for a page component.
Create and register blocks
Section titled “Create and register blocks”Create a src/components/Page.jsx component to render all stories of the page content type, such as the home story.
import { StoryblokBlock } from "@/lib/storyblok";
export default function Page({ block }) { return ( <main> {block.body?.map((nestedBlock) => ( <StoryblokBlock block={nestedBlock} key={nestedBlock._uid} /> ))} </main> );}Use StoryblokBlock to iterate through the body field and render the blocks in it.
Stories can have a body field that contains an array of custom type blocks, such as Feature, Teaser, and Grid.
Create the code for these components as follows.
export default function Feature({ block }) { return ( <div className="feature"> <span>{block.name}</span> </div> );}export default function Teaser({ block }) { return ( <div className="teaser"> <h2>{block.headline}</h2> </div> );}import { StoryblokBlock } from "@/lib/storyblok";
export default function Grid({ block }) { return ( <div className="grid"> {block.columns?.map((nestedBlock) => ( <StoryblokBlock block={nestedBlock} key={nestedBlock._uid} /> ))} </div> );}Similar to Page.jsx, Grid.jsx iterates over the columns block field.
Register the components in storyblok.js. The key must match the block’s technical name.
import { createApiClient } from "@storyblok/api-client";import { defineStoryblokBlocks } from "@storyblok/react";
import Page from "@/components/Page";import Teaser from "@/components/Teaser";import Feature from "@/components/Feature";import Grid from "@/components/Grid";
export const apiClient = createApiClient({ accessToken: process.env.STORYBLOK_DELIVERY_API_TOKEN, region: "eu", // Choose the correct region of your space.});
export const { StoryblokBlock } = defineStoryblokBlocks(() => ({ components: {}, components: { page: Page, teaser: Teaser, feature: Feature, grid: Grid, },}));Run the server and visit the site in your browser.
npm run devRelated resources
Section titled “Related resources”Was this page helpful?
This site uses reCAPTCHA and Google's Privacy Policy (opens in a new window).Terms of Service (opens in a new window) apply.
- Need help?Contact support (opens in a new window)
- Something isn’t right?Check system status (opens in a new window)
Get in touch with the Storyblok community