Run the Next.js sample app
Set up the Next.js sample app, connect it to a Kontent.ai sample project, and see firsthand how a front-end application pulls and displays content in a realistic multi-brand setup.
Before you start
Make sure you have the following ready:- A Kontent.ai
account
- Node.js
- Your favorite code editor, for example, Visual Studio Code
Prepare your sample project
The Next.js sample app connects to the multi-brand sample project, a Kontent.ai project that contains content for three fictional brands, each with its own website. This gives you a realistic example of how Kontent.ai handles content for multiple channels from a single project. Using your own copy of the sample project lets you experiment freely with its content and structure.- In Kontent.ai
, click your initials in the bottom left corner.
- Select Projects.
- Click Create new project in the top right corner.
- Select Multi-site sample project and click Continue.
Initialize the app
Initialize the application and its dependencies withcreate-next-app using either npm or yarn.
npx create-next-app --example https://github.com/kontent-ai/sample-app-next-js sample-app-next-js
# or
yarn create next-app --example https://github.com/kontent-ai/sample-app-next-js sample-app-next-jsSet up environment variables
The app's root directory contains the.env.local.template file that defines the variables used to configure the app.
Copy the file .env.local.template and name the copy .env.local. This preserves the template as a reference.
Most variables in the file can be left at their default values. In the following steps, you'll fill in three required variables:
NEXT_PUBLIC_KONTENT_ENVIRONMENT_ID— connects the app to your sample projectNEXT_PUBLIC_KONTENT_COLLECTION_CODENAME— determines which brand's content the app displaysKONTENT_PREVIEW_API_KEY— enables the app to display unpublished content
Connect your Kontent.ai project
Each Kontent.ai project has one or more environments. The environment ID uniquely identifies which one the app connects to, and this is what tells the app where to pull content from. To find your environment ID:- In Kontent.ai, open your sample project.
- In
Environment settings > General, click
to copy the Environment ID to your clipboard.

env.local file, set the variable NEXT_PUBLIC_KONTENT_ENVIRONMENT_ID to your environment ID.
Choose which content to display
The Multi-site sample project represents a fictional healthcare company with three distinct brands: Ficto Imaging, Ficto Healthtech, and Ficto Surgical. Each brand has its own website and content, represented by a collection in Kontent.ai. In theenv.local file, set NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME to the codename of the brand you want the app to display: ficto_imaging, ficto_surgical, or ficto_healthtech.
Enable content preview
The app uses the Next.js's preview mode to display unpublished content from Kontent.ai. To create a Delivery API key, go toenv.local file, set KONTENT_PREVIEW_API_KEY to your new Delivery API key.
Run the app
Start a Node development server.The application will open automatically in your browser at
.
npm run devhttp://localhost:3000