• Home
  • Blog
  • Xperience by Kentico Headless API & Next.js Integration
Development

Xperience by Kentico Headless API & Next.js Integration

By Vincent Adeyemi | 27 March 2025
Logo of Xperience by Kentico with three ascending orange dots to the left of the text, over a lightly gradient background.

In the modern web development landscape, decoupled architectures and headless CMS (Content Management Systems) have become increasingly popular due to their flexibility and scalability. 

One of the leading headless CMS solutions is Xperience by Kentico, known for its robust API capabilities and developer-friendly environment. When combined with Next.js, a powerful React framework that enables server-side rendering (SSR) and static site generation (SSG), you can create highly performant, dynamic, and SEO-friendly web applications.

This blog post will walk you through how to integrate Xperience by Kentico's Headless API with a Next.js application. We’ll explore setting everything up, fetching and displaying content, and even some advanced tips to help you optimize your web app.


Why Use Xperience by Kentico and Next.js?

Overview of Xperience by Kentico Headless API

Xperience by Kentico headless API is a cloud-based headless CMS that allows developers to build applications with a focus on content rather than infrastructure. As a headless CMS, Xperience by Kentico decouples the content management back end from the presentation layer, providing content as a service through a powerful API. This approach offers several benefits:

  • Flexibility: Content can be reused across multiple platforms, including websites, mobile apps, and IoT devices.
  • Scalability: As a cloud-based service, Xperience by Kentico scales seamlessly with your needs.
  • Content-first approach: Focus on creating and managing content, while developers can work on the presentation independently.

Advantages of Using Next.js for Web Applications

Next.js is a React framework that simplifies the development of modern web applications. It provides features like server-side rendering (SSR) and static site generation (SSG) out of the box, making it an ideal choice for SEO-friendly and performant websites. Key benefits include:

  • Performance: Next.js applications are fast, thanks to its support for SSG and SSR, which reduce load times.
  • SEO-friendly: By rendering content on the server, Next.js improves SEO, ensuring that search engines can index your pages effectively.
  • Developer experience: Next.js provides great developer experience with built-in routing, fast refresh, and a flexible file-based system.

Setting Up the Development Environment

Prerequisites

Before getting started, ensure that you have the following installed:

  • Node.js (version 12 or later) 
  • npm or Yarn (package managers) 
  • Xperience by Kentico account with an existing project 

Initializing a Next.js Project

First, you'll need to create a new Next.js project. You can do this running the following command in a CLI:

npx create-next-app kentico-xperience-nextjs-app 


Navigate into your project directory:

cd kentico-xperience-nextjs-app 


Install Axios for making HTTP requests:

npm install axios 

Connecting Next.js with Xperience by Kentico

Fetching Content from Xperience by Kentico

To fetch content from Xperience by Kentico, you’ll use its REST API. Here’s how you can set up an API client using `axios`:

  • Set up environment variables: Create a `.env.local` file to store your environment variables, such as your API endpoint and any necessary keys:

NEXT_PUBLIC_KENTICO_API_URL=https://your-kentico-xperience-site.com/rest  

NEXT_PUBLIC_KENTICO_API_KEY=your_api_key 


  • Create a service to fetch content: In your project, create a utility file (e.g., lib/Xperience by Kentico.js) to handle API requests:

import axios from 'axios'; 

const apiClient = axios.create({ 

  baseURLprocess.env.NEXT_PUBLIC_KENTICO_API_URL, 

  headers: { 

   'Authorization':`Bearer ${process.env.NEXT_PUBLIC_KENTICO_API_KEY}`, 

  }, 

}); 

 

export async function getContentByType(type) { 

  const response = await apiClient.get(`/content/${type}`); 

  return response.data.items; 

} 


Creating an API Client in Next.js

The `getContentByType` function can now be used across your Next.js application to fetch data from Xperience by Kentico. For instance, you might fetch and display blog posts like this:

 

import { getContentByType } from '../lib/kentico'; 

export async function getStaticProps() { 

  const posts = await getContentByType('blog_post'); 

  return { 

    props: { 

      posts, 

    }, 

  }; 

} 

 

export default function Blog({ posts }) { 

  return ( 

    <div> 

      <h1>Blog Posts</h1> 

      <ul> 

        {posts.map(post => ( 

          <li key={post.id}>{post.title}</li> 

        ))} 

      </ul> 

    </div> 

  ); 

} 

This code assumes your API returns data in a structure where `items` is the key containing your content. You may need to adjust the parsing logic based on your specific API responses.

Rendering Content in Next.js

Using Static Site Generation (SSG)

Static Site Generation is a core feature of Next.js, allowing you to generate static HTML at build time. This is ideal for content that doesn’t change frequently, like blogs or product pages or documentation.

In the example above, `getStaticProps` is used to fetch data at build time, which is then passed as props to the component.

Server-Side Rendering (SSR) Options

For content that needs to be up-to-date with every request, you can use Server-Side Rendering (SSR). Next.js allows you to implement SSR using `getServerSideProps`.

export async function getServerSideProps() { 

  const posts = await getContentByType('blog_post'); 

  return { 

    props: { 

      posts, 

    }, 

  }; 

} 

 

This approach ensures that the content is fetched fresh on every request.


Implementing Dynamic Routing

Next.js supports dynamic routing based on file names. To create a dynamic route, use square brackets in the file name:

 

// pages/blog/[slug].js 

import { getContentByType } from '../../lib/Xperience by Kentico'; 

 

export async function getStaticPaths() { 

  const posts = await getContentByType('blog_post'); 

  const paths = posts.map(post => ({ 

    params: { slugpost.slug }, 

  })); 

  return { paths, fallback: false }; 

} 

 

export async function getStaticProps({ params }) { 

  const posts = await getContentByType('blog_post'); 

  const post = posts.find(p => p.slug === params.slug); 

  return { 

    props: { 

      post, 

    }, 

  }; 

} 

 

export default function Post({ post }) { 

  return ( 

    <div> 

      <h1>{post.title}</h1> 

      <p>{post.content}</p> 

    </div> 

  ); 

}


Advanced Techniques

Optimizing Performance with Incremental Static Regeneration (ISR)

Next.js introduces Incremental Static Regeneration (ISR), allowing you to update static content after the build without rebuilding the entire site. You can use ISR by specifying the `revalidate` property in `getStaticProps`.

 

export async function getStaticProps() { 

  const posts = await getContentByType('blog_post'); 

  return { 

    props: { 

      posts, 

    }, 

    revalidate: 10, // Revalidate every 10 seconds 

  };} 

This allows your pages to be regenerated in the background when they are requested, providing up-to-date content while maintaining the performance benefits of static sites.


Integrating Third-Party Services 

To enhance your web application, consider integrating third-party services like search engines (AlgoliaElasticSearch), analytics (Google Analytics, Segment), and more. This can be easily done within your Next.js application by installing the necessary SDKs and configuring them accordingly.

Conclusion and Next Steps

Building a web application with Xperience by Kentico and Next.js offers a powerful combination of content management and modern web development practices. By leveraging the strengths of both platforms, you can create a high-performance, scalable, and flexible web application.

Recap of Key Points 

  • Xperience by Kentico provides a robust headless CMS solution, enabling a content-first approach.
  • Next.js offers a powerful framework with built-in support for SSG, SSR, and ISR.

About The Author:

Vincent Adeyemi, Sr. Full Stack Developer | Sagepath Reply

Vincent is a Senior Full Stack Developer with over 8 years of expertise in the .NET framework, VueJS, Angular, and Kentico. He specializes in custom web development and has a proven track record of delivering successful projects, including REST API integrations.

Let’s Start a Conversation

Let’s Start a Conversation

Reach out to discuss your digital transformation needs and see how we can help. We would love to start a long-term partnership with your company.

Get in Touch