Run sample app
Set up the 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 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 with
Alternatively, clone
the sample app’s repository
and install the dependencies manually using your package manager of choice.
create-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.

In the
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_healthtech, or ficto_surgical.Enable content preview
The app uses the Next.js's preview mode
to display unpublished content from Kontent.ai.
In the
env.local file, set KONTENT_PREVIEW_API_KEY to your new Delivery API key.Run the app
Start a development server.
The application runs locally at
by default.
npm run devhttp://localhost:3000Activate content preview
To activate preview mode, visit the following URL using the default port:
If the app shows a blank page on startup or you see an error page, check that
To disable Preview mode, add
.
http://localhost:3000/api/preview?secret=mySuperSecret&type=website_root&slug=home. Replace the port if your app runs on a different one. This activates Next.js's Draft Mode, which lets the app use your stored API key to fetch unpublished content.KONTENT_PREVIEW_API_KEY isn't left empty in .env.local and that the key corresponds to the correct environment./api/exit-preview to your local URL. If you are using the default port, visit http://localhost:3000/api/exit-preview