How to setup NextJS to work with Contentful

A step by step guide for setting up a clean NextJS project to work with Contentful.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 04/17/2024 at 18:49
Last Updated on 04/18/2024 at 15:00
<p>Two flowers that represents NextJS and Contentful side by side with the text "Setup” beneath it.</p>

A step by step guide for setting up a clean NextJS project to work with Contentful.

SubscribeDownload Open Source Project

Please note that NextJS has an out of the box solution that functions with Contentful.

Contentful: Next.js Starter with Contentful

The solution laid out in this tutorial is aimed at those looking to add Contentful to an existing NextJS project.

This solution can be found on our Open Source Starter project on the branch tutorial/contentful/setup.

View Repository

Please note that this solution does not include how we created the types or the additional helpers.

Step One: Setup Contentful

<p>A screenshot of Contenful showing sample content.</p>

Create or login to Contentful, setup the content model and create some content.

Visit ContentfulContentful: Content Modeling basics

Step Two: Install Contentful

<p>A screenshot of Terminal showing you how to add Contentful.</p>

In Terminal, set the root directory to that of the NextJS project and then run the line below to install Contentful.

yarn add contentful

Step Three: Gather the API Keys

<p>A screenshot of Contentful showing you how if you click Settings a pop up appears by which you can access the API keys.</p>

In Contentful, go to Settings > API Keys.

<p>A screenshot of Contentful showing an API Key page.</p>

Within the API Keys, select an API Key and take a note of the:

  • SpaceId.
  • Content Delivery API - access token.
  • Content Preview API - access token.

Step Four: Create the Environment Variables

<p>A screenshot of VSCode showing you how we added the environment variables.</p>

In the NextJS project, create an environment variable for each of the following:

  • CONTENTFUL_SPACE_ID for the SpaceId.
  • CONTENTFUL_ACCESS_TOKEN for the Content Delivery API - access token.
  • CONTENTFUL_PREVIEW_API_ACCESS_TOKEN for the Content Preview API - access token.
How to create and use environment variables in an .env file in NextJSHow to add, edit or remove environment variables in Vercel

Step Five: Create the Contentful Client

<p>A screenshot of VSCode showing the Contentful Client code.</p>

In the NextJS project, create a new file and add code similar to the one below to create the Contentful client.

Please note that we suggest that you place this file under utils > contentful.

Step Six: Setup the first Query

<p>A screenshot of Contentful showing code for querying a page.</p>

In the NextJS project, create a new file and write your first query. An example is found below.

Please note that we suggest that you place this file under utils > contentful > queries.

Step Seven: Update the page

<p>A screenshot of VSCode showing how we updated the page to handle a Contentful query.</p>

On the NextJS page (i.e. home page), update the file to make a request for the data, using code similar to the one found below.

Please note that you cannot use getStaticProps within a NextJS app page. Instead, you must create a "lib" or "utils" function that handles the request for you.

Step Eight: Test

<p>A screenshot of Chrome showing that an image now loads.</p>

Run the app and confirm that the data is being fetched correctly.

Having issues loading hosted images?

If you are running into issues loading hosted images, consult the guide linked below.

How to setup NextJS to work with images hosted on Contentful

Looking to learn more about NextJS, ReactJS or web development?

Search our blog to find educational content on NextJS, ReactJS and web development.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe