Gatsby Cloud Docs

Getting started with Gatsby Preview and Contentstack

Learn how to connect Gatsby Preview with Contentstack

Table of Contents

What are Gatsby Cloud and Contentstack, and why use them together?

Contentstack is a headless CMS that content editors can use to edit and publish content. Gatsby Cloud allows you to integrate your site with Contentstack in order to run performant builds and preview content changes made in the CMS before publishing.

Setting up a Contentstack and Gatsby site

First, you’ll need a Gatsby site that’s pulling source data from a Contentstack project via the gatsby-source-contentstack plugin and its source code needs to live on GitHub. If you haven’t set that up yet, try using the gatsby-starter-contentstack starter.

Signing in to Gatsby Cloud

Select Sign in with GitHub. You’ll be asked to authorize the Gatsby Cloud app with your GitHub account. If you need to request access to one or more repositories, you can click “request access” here or later, when creating an instance.

Once signed in, configuring Gatsby Cloud with Contentstack requires several steps that are walked through below.

Creating an instance

Once you’ve authenticated with Cloud and GitHub, you can create an instance from the “Create a new Gatsby Cloud site” page.

Use the “I already have a Gatsby site” flow to manually integrate your site.

Add my own site

Pick your Gatsby site from the list of GitHub repositories. You can use the search input to narrow down the list.

Gatsby Cloud Add an instance page

If you don’t see your site, it might be because it belongs to a GitHub organization, rather than your personal account. You can connect a new GitHub Organization.

Note: Repositories must contain one Gatsby project configured at their root to be enabled. Gatsby Cloud works best with Gatsby version 2.1.0 and higher.

Select branch and publish directory

You’ll need to select a branch and then indicate the publish directory where the gatsby-config.js lives. If you leave the field blank, it defaults to the root of the site.

Select branch and directory

Once the branch and directory path are correct, select “Next.”

Create the instance

Integration Step - automatic or manual

Manual Integration

First, click “Skip this step” to configure Contentstack manually.

Gatsby Cloud will automatically try and detect environment variables necessary in your gatsby-config.js. However — consider adding any additional ones or variables that automatic detection may have missed. See “Setting up Environment Variables” for more info.

Note that you will be able to add, delete, or update these later on in “Site Settings”.

Once you’ve added the necessary environment variables, you can press “Create site” which will create your instance in Gatsby Cloud!

Site is Created

After following the “Automatic Integration” or “Manual Integration” flow you now have an instance of Gatsby Cloud configured with environment variables and a provided Gatsby Preview instance you can share with your team. Woo-hoo!

Configuring Cloud Webhooks

In Contentstack, navigate to Settings using the gear icon and then to “Webhooks”. Select “create new webhook.” Navigate to Gatsby Cloud, and then to the Settings for your Gatsby Cloud instance. Copy the webhook URL, navigate back to Contentstack webhook, and paste the webhook URL into the field labelled “URL to notify *“.

A screenshot showing where to copy and paste the Gatsby webhook into Contentstack

In Contentstack, there are options to add webhook responses to certain events. Add all the options you’d like. A best practice is to add webhook responses for nearly everything you can! For example, select any content type, asset, or entry and add responses for any options available, such as create, update, delete, successful publish, and unsuccessful publish.

A screenshot showing examples of webhook options

Setting up Environment Variables

An environment variable references a value that can affect how running processes will behave on a computer, for example in staging and production environments. You must save environment variables in Gatsby Cloud to authorize your instance to pull source data from Contentstack.

You will need to add into Gatsby Cloud any environment variable required for your app to run, such as deployment or test environment configuration settings.

You will also need to add in the following Gatsby Cloud-specific environment variables:

  1. In your Contentstack account, navigate to Settings using the gear icon in the menu and select “Environments”.

  2. Create an environment named “Preview”, and omit the Base URL for now. It will be filled in later.

  3. Navigate to Settings using the gear icon again and select “Tokens”.

  4. Create a new token, give it a name and add it to the scope for the “Preview” environment.

A screenshot showing how to add the token to the preview environment

  1. After creating the token you will see an API token and a Delivery Token. You will add these values as environment variables in Gatsby Cloud.
VariableDescription
CONTENTSTACK_API_KEYAPI Key
CONTENTSTACK_ACCESS_TOKENDelivery Token
CONTENTSTACK_ENVIRONMENTName of the environment

This matches the gatsby-config.js of the site:

module.exports = {
  plugins: [
    {
      resolve: "gatsby-source-contentstack",
      options: {        api_key: process.env.CONTENTSTACK_API_KEY,        access_token: process.env.CONTENTSTACK_ACCESS_TOKEN,        environment: process.env.CONTENTSTACK_ENVIRONMENT,      },    },
  ],
}

Select Next

Select Next and wait for the first preview instance to be created.

Configuring the Preview Environment

After your Preview instance is created, copy the Preview instance URL. Then navigate to your Contentstack stack’s setting (gear icon), navigate to “Environments”, edit the preview environment, and paste the Gatsby Preview URL as the base URL.

Testing Gatsby Preview instance

When you publish changes to your content in Contentstack, your Gatsby Preview instance should update quickly.