Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

buttercms

buttercms34.2kMIT3.0.2TypeScript support: included

ButterCMS API Client

buttercms, butter, cms, api, headless cms

readme

ButterCMS JS client

npm version

Documentation

For a comprehensive list of examples, check out the API documentation.

ButterCMS-JS version 2 will be supported until May 2025, when Node v18 sunsets. ButterCMS-JS version 3 is our current version and will be supported until May 2026 when Node v20 sunsets ButterCMS-JS version 4 slated for launch in December 2025 when Node v22 is moved to maintenance mode.

Installation

Requires Node.js version 20 or greater.

npm install buttercms --save

Butter can also be included directly in HTML:

<script src="https://cdn.jsdelivr.net/npm/buttercms@3.0.2/dist/butter.min.js"></script>

Native Fetch

ButterCMS-JS version 2 will be using the native fetch API. This means that the fetch API will be used to make requests to the ButterCMS API. This is a breaking change for anyone using version 1 of the ButterCMS-JS package.

Native fetch is built into Node v18 as well as all modern browsers. This lessens the need for third-party fetch libraries and achieves consistency between Node and browser environments.

Overview

Every resource is accessed via your butter instance:

Using ES6 or Typescript:

import Butter from "buttercms";
const butter = Butter("your_api_token");

Using CDN:

<script>
  const butter = Butter("your_api_token");
</script>

Every resource method returns a promise:

// Get blog posts
butter.post.list({page: 1, page_size: 10})
  .then(function(response) {
    console.log(response)
  })
  .catch(function(error) {
    console.error(error)
  })

If using async/await:

async function getPosts() {
  try {
    const response = await butter.post.list({page: 1, page_size: 10});
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

Pages

Where you see params it is a plain js object, e.g. {page: 1}. For a list of params see the API documentation

  • page

    • retrieve(page_type, page_slug[, params])
    • list(page_type[, params])
    • search(query[, params])
    • cancelRequest()
    // Get page
    butter.page.retrieve("casestudy", "acme-co").then(function(resp) {
    console.log(resp)
    });

If using async/await:

async function getPage() {
  try {
    const response = await butter.page.retrieve("casestudy", "acme-co");
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

Collections

  • content
    • retrieve(collection[, params])
    • cancelRequest()
// Get FAQ
butter.content.retrieve("faq", {locale: "es"}).then(function(resp) {
  console.log(resp)
});

If using async/await:

async function getFAQ() {
  try {
    const response = await butter.content.retrieve("faq", {locale: "es"});
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

Preview mode

Preview mode can be used to setup a staging website for previewing content fields or for testing content during local development. To fetch content from preview mode add an additional argument, true, to the package initialization:

const butter = Butter(
  "your butter API token", 
  { testMode: true }
);

Or use an environment variable:

const butter = Butter(
  "your butter API token", 
  { testMode:  process.env.BUTTER_PREVIEW_MODE }
);

Blog Engine

  • post
    • retrieve(slug[, params])
    • list([params])
    • search(query[, params])
    • cancelRequest()
  • category
    • retrieve(slug[, params])
    • list([params])
    • cancelRequest()
  • tag
    • retrieve(slug[, params])
    • list([params])
    • cancelRequest()
  • author
    • retrieve(slug[, params])
    • list([params])
    • cancelRequest()
  • feed
    • retrieve(type[, params])
    • cancelRequest()

See our node app for a full example.

Timeouts

The default timeout threshold is 3000ms but you can change it:

const butter = Butter(
  "your butter API token", 
  { timeout: 5000 }
);

Caching

The default cache is set to default:

const butter = Butter(
  "your butter API token", 
  { cache: "only-if-cached" }
);

Canceling a request

Each resource returns a cancelRequest method that can be used to cancel a request:

butter.post.cancelRequest();

This will cancel all pending requests for that resource type. It will catch the cancelation and return a rejected Promise for your .catch() method or be able to be caught in an async function via a try/catch block.

Hooks

If you need to custom headers, caching, automatic retry or any other specific functionality on the transport layer, you can hook up into our fetch hooks. The following hooks are available as Butter configuration options:

const butter = Butter(
  "your butter API token", 
  { 
    onError: Function,
    onRequest: Function,
    onResponse: Function
  }
);

onRequest - called prior to making a request to the API. This can be declared as an async function to allow for async operations to be performed before the request is made.

onRequest(resource, config)

resource - The resource is the Butter API endpoint to be called config - This is the configuration object that will be used to make the request

config includes:

cancelRequest - A function that can be called to cancel the request in the hook headers - The headers that will be sent with the request. This is in the JS Headers API format. Users are able to modify these headers in the hook. options - Options are the original config options set when initializing the Butter instance. This includes the cache, testMode, timeout. Hook functions are not included in this object. params - The query parameters that will be sent with the request. Users are able to modify these parameters in the hook. type - The type of resource api that is being requested. This string helps the developer to differentiate what resource is being called in the global hook.

options, headers, and params are to be returned by the onRequest hook for further processing and request.

onResponse - called after a response is received from the API. This can be declared as an async function to allow for async operations to be performed after the response is received.

onResponse (response, config)

response - The response object that was received from the API. This is in the JS Fetch Response API format and is a clone of the original response object. This will allow the user to parse the response to get the data they need whether it be JSON, text, blob, etc. The original response will be returned as JSON to the resource function call's promise resolution.

config - This is the configuration object that was used to make the request

config includes:

options - Options are the original config options set when initializing the Butter instance. This includes the cache, testMode, timeout. Hook functions are not included in this object. params - The query parameters were sent with the request. type - The type of resource api that was requested. This string helps the developer to differentiate what resource was called in the global hook.

onResponse expects no return values.

onError - called when an error is received from the API. This is not considered an async function.

onError (errors, config)

errors - the errors returned from the API and/or the fetch request. Comes in the following object format:

{
    details: "Error details",
}

config includes:

options - Options are the original config options set when initializing the Butter instance. This includes the cache, testMode, timeout. Hook functions are not included in this object. params - The query parameters were sent with the request. type - The type of resource api that was requested. This string helps the developer to differentiate what resource was called in the global hook.

onError expects no return values.

Documentation

Documentation is available at https://buttercms.com/docs/api/node

Other

View NodeJS Blog engine and Full CMS for other examples of using ButterCMS with NodeJS.

changelog

Changelog

3.0.2 (2025-04-16)

Bug Fixes

  • add missing page params in typings (d84eed0)

3.0.1 (2025-04-08)

Bug Fixes

  • 3154 remove importing unused AxiosInstance, modify config (10439bb)

3.0.0 (2024-12-19)

⚠ BREAKING CHANGES

  • upgrade to node 20

Features

2.3.1 (2024-12-06)

Bug Fixes

  • Exports types for resolution in ESM Modules (7f3dbaa)

2.3.0 (2024-11-13)

Features

  • add V2 Typings to JS SDK (41e2fe8)

2.2.0 (2024-11-11)

Features

  • update builds to include dev builds (3488632)

Bug Fixes

  • split production builds (1205812)

2.1.4 (2024-09-04)

Bug Fixes

  • update cdn link in readme (d2be0db)

2.1.3 (2024-08-25)

Bug Fixes

  • update main mount in package, configure npm to see minified file output (18731dd)

2.1.2 (2024-08-22)

Bug Fixes

  • error in test action (b0867ca)
  • update actions for publishing to npm (b0867ca)

2.1.1 (2024-08-12)

Bug Fixes

  • add in full cause to thrown errors (6ede5e4)
  • update abortcontroller/abortSignal for use in Reactive Native (758172b)
  • update error catching to catch all produced errors (d91d47f)

2.1.0 (2024-07-29)

Features

  • update response structure to echo schema expected from TS defs and v1 (1a748fa)

2.0.3 (2024-06-14)

Bug Fixes

2.0.2 (2024-06-13)

Bug Fixes

  • enhanced garbage collection on fetch aborts & error messaging (14e97e3)

2.0.1 (2024-04-10)

Bug Fixes

  • update readme re: support and use of native fetch (b193bca)

2.0.0(2023-02-01)

Added

  • Added support for Node.js v18
  • Added support for native fetch API
  • Added explicit cancelRequest method to cancel requests
  • Added native fetch timeout support
  • Added 'onRequest' hook to modify request before it is sent
  • Added 'onError' hook to inspect error before it is thrown if error before server
  • Added 'onError' hook to inspect error after it is thrown if error is from fetch or internal browser issue
  • Added 'onResponse' hook to inspect response before it is returned

Removed

  • Removed support for Node.js v14 and v16
  • Removed support for axios

1.2.16 (2023-12-07)

This is the last release for Node v14 (and 16) and uses axios for API requests. This branch will no longer be maintained.

Updated

  • Updated version number

1.2.15 (2023-10-23)

Bug Fixes

  • bump @babel/traverse from 7.21.4 to 7.23.2 (8e3fcd5)

1.2.14 (2023-09-19)

Bug Fixes

1.2.13 (2023-09-15)

Bug Fixes

  • issue calling axios.create undefined (0cc86c9)

1.2.12 (May 1st, 2023)

Updated

  • Upgrade axios to 1.3.6
  • Upgrade webpack to 5.81.0

Added

  • Added testing with jest

1.2.11 (April 17, 2023)

Updated

  • Updated axios from 1.2.5 to ^1.3.5
  • Fixed bug with axios.create()

1.2.10 (Feb 27, 2023)

Added

  • Added webpack-cli to run webpack from CLI

Updated

  • Updated dependencies:
    • Updated axios from ~0.21.1 to ^1.2.5
    • Updated uglify-js from ^2.8.22 to ^3.17.4
    • Updated async from 2.6.3 to 2.6.4
    • Updated tar from 4.4.13 to 4.4.19
    • Updated ini from 1.3.5 to 1.3.8
    • Updated decode-uri-component from 0.2.0 to 0.2.2
    • Updated webpack from 2.7.0 to 5.75.0,
  • Updated README.md to bump the JavaScript version

Removed

  • Removed json5 and loader-utils as they are no longer used after updating webpack dependency

1.0.0 (Apr 9, 2016)

  • Initial release