Kontent.ai & Astro
Kontent.ai is a headless CMS that allows you to manage content in a structured and modular way, supported by AI capabilities.
Integrating with Astro
Section titled Integrating with AstroIn this section, you’ll use the Kontent.ai TypeScript SDK to connect your Kontent.ai project to your Astro application.
Prerequisites
Section titled PrerequisitesTo get started, you’ll need the following:
-
Kontent.ai project - If you don’t have a Kontent.ai account yet, sign up for free and create a new project.
-
Delivery API keys - You will need the Environment ID for published content and the Preview API key for fetching drafts (optional). Both keys are located in the Environment Settings -> API keys tab in Kontent.ai.
Setting up credentials
Section titled Setting up credentialsTo add your Kontent.ai credentials to Astro, create a .env
file in the root of your project with the following variables:
Now, these environment variables can be used in your Astro project.
If you would like to get TypeScript IntelliSense for these environment variables, you can create a new env.d.ts
file in the src/
directory and configure ImportMetaEnv
like this:
Your root directory should now include these new files:
Directorysrc/
- env.d.ts
- .env
- astro.config.mjs
- package.json
Installing dependencies
Section titled Installing dependenciesTo connect Astro with your Kontent.ai project, install the Kontent.ai TypeScript SDK:
Next, create a new file called kontent.ts
in the src/lib/
directory of your Astro project.
This implementation creates a new DeliveryClient
object using credentials from the .env
file.
Finally, the root directory of your Astro project should now include these new files:
Directorysrc/
Directorylib/
- kontent.ts
- env.d.ts
- .env
- astro.config.mjs
- package.json
Fetching data
Section titled Fetching dataThe DeliveryClient
is now available to all components. To fetch content, use the DeliveryClient
and method chaining to define your desired items. This example shows a basic fetch of blog posts and renders their titles in a list:
You can find more querying options in the Kontent.ai documentation.
Making a blog with Astro and Kontent.ai
Section titled Making a blog with Astro and Kontent.aiWith the setup above, you are now able to create a blog that uses Kontent.ai as the source of content.
Prerequisites
Section titled Prerequisites-
Kontent.ai project - For this tutorial, using a blank project is recommended. If you already have some content types in your content model, you may use them, but you will need to modify the code snippets to match your content model.
-
Astro project configured for content fetching from Kontent.ai - see above for more details on how to set up an Astro project with Kontent.ai
Setting up content model
Section titled Setting up content modelIn Kontent.ai, navigate to Content model and create a new content type with the following fields and values:
- Name: Blog Post
- Elements:
- Text field
- Name: Title
- Element Required: yes
- Rich text field
- Name: Teaser
- Element Required: yes
- Allowed in this element: only check Text
- Rich text field
- Name: Content
- Element Required: yes
- Date & time field
- Name: Date
- URL slug field
- Name: URL slug
- Element Required: yes
- Auto-generate from: select “Title”
- Text field
Then, click on Save Changes.
Creating content
Section titled Creating contentNow, navigate to Content & assets tab and create a new content item of type Blog Post. Fill the fields using these values:
- Content item name: Astro
- Title: Astro is amazing
- Teaser: Astro is an all-in-one framework for building fast websites faster.
- Content: You can use JavaScript to implement the website functionality, but no client bundle is necessary.
- Date & time: select today
- URL slug: astro-is-amazing
When you’re finished, publish the blog post using the Publish button at the top.
Note: Feel free to create as many blog posts as you like before moving to the next step.
Generating content model in TypeScript
Section titled Generating content model in TypeScriptNext, you’ll generate TypeScript types out of your content model.
First, install the Kontent.ai JS model generator, ts-node, and dotenv:
Then, add the following script to package.json:
Because the types require structural information about your project that is not available in the public API, you also need to add a Content Management API key to the .env
file. You can generate the key under Environment settings -> API keys -> Management API.
Finally, add the script generate-models.ts
that configures the model generator to generate the models:
Now, execute it:
Displaying a list of blog posts
Section titled Displaying a list of blog postsNow you’re ready to fetch some content. Go to the Astro page where you want to display a list of all blog posts, for example, the homepage index.astro
in src/pages
.
Fetch all blog posts in the frontmatter of the Astro page:
If you skipped the model generation, you can also use an untyped object and string literal to define the type:
The fetch call will return a response
object which contains a list of all blog posts in data.items
. In the HTML section of the Astro page, you can use the map()
function to list the blog posts:
Generating individual blog posts
Section titled Generating individual blog postsThe last step of the tutorial is to generate detailed blog post pages.
Static site generation
Section titled Static site generationIn this section, you’ll use the Static (SSG) Mode with Astro.
First, create a file [slug].astro
in /src/pages/blog/
which needs to export a function getStaticPaths
that collects all data from the CMS:
So far, the function fetches all blog posts from Kontent.ai. The code snippet is exactly the same as what you used on the home page.
Next, the function must export paths and data for each blog post. You named the file [slug].astro
, so the param which represents the URL slug is called slug
:
The last part is to provide the HTML template and display each blog post:
Navigate to your Astro preview (http://localhost:3000/blog/astro-is-amazing/ by default) to see the rendered blog post.
Server-side rendering
Section titled Server-side renderingIf you’ve opted into SSR mode, you will use dynamic routes to fetch the page data from Kontent.ai.
Create a new file [slug].astro
in /src/pages/blog/
and add the following code. The data fetching is very similar to previous use cases but adds an equalsFilter
that lets us find the right blog post based on the used URL:
If you’re not using generated types, you can instead use string literals to define the content item type and the filtered element codename:
Lastly, add the HTML code to render the blog post. This part is the same as with static generation:
Publishing your site
Section titled Publishing your siteTo deploy your website, visit the deployment guides and follow the instructions for your preferred hosting provider.
Rebuild on Kontent.ai changes
Section titled Rebuild on Kontent.ai changesIf your project is using Astro’s default static mode, you will need to set up a webhook to trigger a new build when your content changes. If you are using Netlify or Vercel as your hosting provider, you can use its webhook feature to trigger a new build from Kontent.ai events.
Netlify
Section titled NetlifyTo set up a webhook in Netlify:
- Go to your site dashboard and click on Build & deploy.
- Under the Continuous Deployment tab, find the Build hooks section and click on Add build hook.
- Provide a name for your webhook and select the branch you want to trigger the build on. Click on Save and copy the generated URL.
Vercel
Section titled VercelTo set up a webhook in Vercel:
- Go to your project dashboard and click on Settings.
- Under the Git tab, find the Deploy Hooks section.
- Provide a name for your webhook and the branch you want to trigger the build on. Click Add and copy the generated URL.
Adding a webhook to Kontent.ai
In the Kontent.ai app, go to Environment settings -> Webhooks. Click on Create new webhook and provide a name for your new webhook. Paste in the URL you copied from Netlify or Vercel and select which events should trigger the webhook. By default, for rebuilding your site when published content changes, you only need Publish and Unpublish events under Delivery API triggers. When you’re finished, click on Save.
Now, whenever you publish a new blog post in Kontent.ai, a new build will be triggered and your blog will be updated.