To get started, you will need to have the following:
An Astro project - If you don’t have an Astro project yet, our Installation guide will get you up and running in no time.
A Contentful account and a Contentful space. If you don’t have an account, you can sign up for a free account and create a new Contentful space. You can also use an existing space if you have one.
Contentful credentials - You can find the following credentials in your Contentful dashboard Settings > API keys. If you don’t have any API keys, create one by selecting Add API key.
Contentful space ID - The ID of your Contentful space.
Contentful delivery access token - The access token to consume published content from your Contentful space.
Contentful preview access token - The access token to consume unpublished content from your Contentful space.
To add your Contentful space’s credentials to Astro, create an .env file in the root of your project with the following variables:
.env
CONTENTFUL_SPACE_ID=YOUR_SPACE_ID
CONTENTFUL_DELIVERY_TOKEN=YOUR_DELIVERY_TOKEN
CONTENTFUL_PREVIEW_TOKEN=YOUR_PREVIEW_TOKEN
Now, you can use these environment variables in your project.
If you would like to have IntelliSense for your Contentful environment variables, you can create a env.d.ts file in the src/ directory and configure ImportMetaEnv like this:
src/env.d.ts
interface ImportMetaEnv {
readonly CONTENTFUL_SPACE_ID:string;
readonly CONTENTFUL_DELIVERY_TOKEN:string;
readonly CONTENTFUL_PREVIEW_TOKEN:string;
}
Your root directory should now include these new files:
Astro components can fetch data from your Contentful account by using the contentfulClient and specifying the content_type.
For example, if you have a “blogPost” content type that has a text field for a title and a rich text field for content, your component might look like this:
A Contentful space - For this tutorial we recommend starting with an empty space. If you already have a content model, feel free to use it, but you will need to modify our code snippets to match your content model.
An Astro project integrated with the Contentful SDK - See integrating with Astro for more details on how to set up an Astro project with Contentful.
Create a new interface called BlogPost and add it to your contentful.ts file in src/lib/. This interface will match the fields of your blog post content type in Contentful. You will use it to type your blog post entries response.
This fetch call will return an array of your blog posts at entries.items. You can use map() to create a new array (posts) that formats your returned data.
The example below returns the items.fields properties from our Content model to create a blog post preview, and at the same time, reformats the date to a more readable format.
If you’re using Astro’s default static mode, you’ll use dynamic routes and the getStaticPaths() function. This function will be called at build time to generate the list of paths that become pages.
Create a new file named [slug].astro in src/pages/posts/.
As you did on index.astro, import the BlogPost interface and contentfulClient from src/lib/contentful.ts.
This time, fetch your data inside a getStaticPaths() function.
Then, map each item to an object with a params and props property. The params property will be used to generate the URL of the page and the props property will be passed to the page component as props.
const data = await contentfulClient.getEntries<BlogPost>({
content_type: "blogPost",
"fields.slug": slug,
});
} catch (error) {
return Astro.redirect("/404");
}
---
To pass post data to the template section, create a post object outside the try/catch block.
Use documentToHtmlString to convert content from a Document to HTML, and use the Date constructor to format the date. title can be left as-is. Then, add these properties to your post object.
If 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 Contentful events.
In your Contentful space settings, click on the Webhooks tab and create a new webhook by clicking the Add Webhook button. Provide a name for your webhook and paste the webhook URL you copied in the previous section. Finally, hit Save to create the webhook.
Now, whenever you publish a new blog post in Contentful, a new build will be triggered and your blog will be updated.