Create a Next.js and ButterCMS App That Builds and Deploys with Vercel

Deploy your Next.js and ButterCMS app with Vercel in a serverless environment.

In this guide, you will discover how to create a Next.js app that displays links to posts from the Vercel blog by utilizing the ButterCMS content management system, before deploying with a single command to Vercel.

Next.js from Vercel is a production-ready framework that helps you create fast React apps. ButterCMS is a drop-in headless CMS with zero headaches that allows you to never worry about hosting, upgrades, uptime, security or performance whilst storing all your content in one place.

By following this guide, you will create a clone of the example app, a starting point to get you up and running with your own Next.js + ButterCMS app in minutes.

Step 1: Create your ButterCMS Content

From your ButterCMS Collections dashboard, create a new Collection called Posts by clicking the + New Collection button.

Creating a Collection for your Next.js + ButterCMS project using the ButterCMS Collections dashboard.

Add the following fields to your Collection, all of type Short Text, by clicking the Add New Property button:

  • title
  • date
  • alt
  • image
  • url

Your Posts Collection should look like this:

An example Collection for your Next.js + ButterCMS project.

Next, click the + Add to Posts button to create a post, providing the relevant details.

That's it for creating content! You can edit both the Posts Collection and its content at any time, either by adding or removing Posts or changing the Collection Item Properties.

Next, select Settings from the account dropdown menu at the top left of the screen. Here you will find your Read API Token, make a note of this so you can use it later on in your app.

Step 2: Creating your Next.js App


mkdir my-nextjs-buttercms-project && cd my-nextjs-buttercms-project

Creating and entering into the project directory.


npm init -y

Initializing your project with a package.json file.


Next, add the project dependencies:

npm i next react react-dom

Adding next, react and react-dom as dependencies to your project.


import { useEffect, useState } from 'react'
import Head from 'next/head'
import Post from '../components/post'

function HomePage() {
  const [posts, setPosts] = useState([])
  useEffect(() => {
    async function getPosts() {
      const res = await fetch(
      const { data } = await res.json()
      const allPosts = data.posts
  }, [])
  return (
        <title>Next.js + ButterCMS</title>
      {posts.length > 0
        ? => (
        : null}

export default HomePage

An example index.js page for your Next.js + ButterCMS project.

Let's take look at what this file achieves.

Inside the useEffect hook, an asynchronous function, getPosts, is defined, this is then called in the useEffect hook to retrieve the posts on initial load.



  return (
    <div className="container">
      <a href={url}>
      <div className="text">
      <style jsx>{`
        .container {
          cursor: pointer;
          height: 453px;
          margin-bottom: 48px;
        a {
          border-bottom: none;
        a:hover {
          border-bottom: none;
        .text {
          margin-top: -160px;
          padding: 24px;
          position: absolute;
        h2 {
          color: white;
          font-size: 24px;
          margin-bottom: 0;
        h4 {
          font-size: 16px;
          font-weight: 500;
          margin-top: 8px;

export default Post

An example post.js component for your Next.js + ButterCMS project.


Step 3: Adding an Environment Variable


  "build": {
    "env": {
      "BUTTER_API_TOKEN": "@butter_api_token"

An example vercel.json file for your Next.js + ButterCMS project.

With your vercel.json file created, you should add a next.config.js file at the root of your project directory with the code below:

module.exports = {
  env: {

An example next.config.js file for your Next.js + ButterCMS project.

The next.config.js file provides access to environment variables inside your Next.js app.

Now, add the following build script to your package.json file:

  "scripts": {
    "build": "next build"

Adding a build script to your package.json file for your Next.js + ButterCMS project.

Next, you will make your API key available to your app during local development by creating a .env.local file.

Create a .env.local file at the root of your project directory with the following code, adding your API key obtained in step 1 where instructed:


An example .env.local file for your Next.js + ButterCMS project.

Lastly, to make your API key available for cloud deployment, create a Secret with the command below:

vercel secrets add BUTTER_API_TOKEN your-space-id

Adding the BUTTER_API_TOKEN secret to your project using Secrets.

With those steps out the way you are now able to run your app. You can develop your app locally using the following command:

next dev

Using the next dev command to run the app locally.

Step 4: Deploying the App

To deploy your Next.js + ButterCMS app with a Vercel for Git Integration, make sure it has been pushed to a Git repository.

Import the project into Vercel using your Git Integration of choice:


Written By
Written by mcsdevmcsdev