Introducing the CMS API that allows you to pull content from your CMS
Introducing the CMS API that allows you to pull content from your CMS
To fetch content from your CMS, you can use the CMS API. The CMS API is an interface that allows you to pull content from your CMS and display it on your website. This is useful if you want to display dynamic content on your website that is stored in your CMS.
Creating a CMS client
To create a CMS client, you can use the createCmsClient
function. This function returns a client that you can use to fetch content from your CMS.
import { createCmsClient } from '@kit/cms';const client = await createCmsClient();
The implementation depends on the CMS you are using. By default, the CMS client uses the keystatic
CMS. If you are using a different CMS, you can specify the CMS client to use by setting the CMS_CLIENT
environment variable.
CMS_CLIENT=keystatic
Fetching content items
To fetch content items from your CMS, you can use the getContentItems
function.
// import the createCmsClient functionimport { createCmsClient } from '@kit/cms';// create a clientconst client = await createCmsClient();// Fetch content itemsconst { items, count } = await client.getContentItems({ collection: 'posts',});
The getContentItems
function takes an object with the following properties:
collection
- The collection to fetch content items from.limit
- The number of content items to fetch.offset
- The offset to start fetching content items from.language
- The language to fetch content items in.sortBy
- The field to sort content items by.sortDirection
- The direction to sort content items in.
import { createCmsClient } from '@kit/cms';const getContentItems = cache( async (language: string | undefined, limit: number, offset: number) => { const client = await createCmsClient(); return client.getContentItems({ collection: 'posts', limit, offset, language, sortBy: 'publishedAt', sortDirection: 'desc', }); },);
NB: how these values are used is entirely dependent on the CMS you are using. For example, Wordpress will only support posts
or pages
as collections.
Additionally: how the language filtering is implemented is also dependent on the CMS you are using.
Fetching a single content item
To fetch a single content item from your CMS, you can use the getContentItemBySlug
function.
import { createCmsClient } from '@kit/cms';const client = await createCmsClient();// Fetch a single content itemconst item = await client.getContentItemBySlug({ slug: 'hello-world', collection: 'posts'});
The getContentItemBySlug
function takes an object with the following properties:
slug
- The slug of the content item to fetch.collection
- The collection to fetch the content item from.
Rendering pages using content from your CMS
You can use the getContentItemBySlug
function to fetch content from your CMS and render pages using that content.
For example, if you want to store your Terms and Conditions in your CMS, you can fetch the content item for the Terms and Conditions page and render the page using that content.
import { createCmsClient } from '@kit/cms';async function TermsAndConditionsPage() { const client = await createCmsClient(); const { content, title } = await client.getContentItemBySlug({ slug: 'terms-and-conditions', collection: 'pages', }); return ( <div> <h1>{title}</h1> <div>{content}</div> </div> );}