Overview
This guide shows you how to integrate Marble CMS with Astro using Content Collections. You’ll learn how to fetch blog posts with type-safe loaders, generate static pages, and render content with full TypeScript support. To integrate Marble in your Astro application, you first need an Astro site. If you don’t have one, you can check out the Astro documentation to get started, or use our Astro blog template for a ready-to-go solution. Astro’s Content Collections are the best way to manage content in your project. We’ll use a Content Loader to fetch your posts from Marble and make them available in your Astro site with full type-safety. You can learn more about this in the Astro Content Collections documentation.1
Set up environment variables
First, add your Marble API URL and Workspace Key to your environment variables. Create a
.env file in the root of your Astro project and add the following:.env
2
Configure your content collection
Next, define your content collection. This tells Astro how to fetch and validate your data from Marble. Create a file named
src/config.ts and add the following code.This configuration defines a posts collection, and includes a loader function that fetches your posts from the Marble API.For complete TypeScript type definitions, see our TypeScript types reference. The schema below matches the
Post type from the Marble API.src.config.ts
3
Display a list of posts
Now, you can use
getCollection() to fetch all your posts and display them on a page. Create a file at src/pages/blog/index.astro to list all your posts.src/pages/blog/index.astro
4
Display a single post
To display individual posts, create a dynamic route that fetches a single post based on its slug. Create a file at
src/pages/blog/[...slug].astro.This page uses getStaticPaths() to generate a static page for every post at build time.src/pages/blog/[...slug].astro
Rendering HTML ContentWe use the
set:html directive to render the post content. Marble sanitizes all HTML content on the server, ensuring it’s safe to render directly in your application.