> ## Documentation Index
> Fetch the complete documentation index at: https://buttercms.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK

> The ButterCMS JavaScript SDK is isomorphic — use it in the browser or Node.js — with built-in TypeScript definitions for autocompletion and type safety.

## Installation

> [tab] npm
>
> ```bash theme={null}
> npm install buttercms --save
> ```

> [tab] yarn
>
> ```bash theme={null}
> yarn add buttercms
> ```

> [tab] pnpm
>
> ```bash theme={null}
> pnpm add buttercms
> ```

> [tab] CDN
>
> ```html theme={null}
> <script type="module">
>   import Butter from "https://cdn.jsdelivr.net/npm/buttercms@3.0.3/dist/butter.esm.js";
> </script>
> ```

> [!NOTE]
>
> **Requirements:** Node.js version 20 or greater for server-side usage.

## Initialization

Initialize the SDK with your Read API token from the [ButterCMS Settings](https://buttercms.com/settings/).

> [tab] ES Modules
>
> ```javascript theme={null}
> import Butter from "buttercms";
>
> const butter = Butter("your_api_token");
> ```

> [tab] CommonJS
>
> ```javascript theme={null}
> const Butter = require("buttercms").default;
>
> const butter = Butter("your_api_token");
> ```

> [tab] Browser (CDN)
>
> ```html theme={null}
> <script type="module">
>   import Butter from "https://cdn.jsdelivr.net/npm/buttercms@3.0.3/dist/butter.esm.js";
>   const butter = Butter("your_api_token");
> </script>
> ```

## Configuration options

You can customize the SDK behavior by passing an options object:

```javascript theme={null}
const butter = Butter("your_api_token", {
  testMode: true,          // Enable preview mode for draft content
  timeout: 5000,           // Request timeout in milliseconds (default: 3000)
  cache: "only-if-cached", // Cache strategy (default: "default")
  onRequest: (resource, config) => {
    // Pre-request hook - modify headers or params
  },
  onResponse: (response, config) => {
    // Post-response hook - process responses
  },
  onError: (errors, config) => {
    // Error handling hook
  }
});
```

| Option | Type | Default | Description |
| - | - | - | - |
| `testMode` | boolean | `false` | Enable preview mode to access draft content |
| `timeout` | number | `3000` | Request timeout in milliseconds |
| `cache` | string | `"default"` | Cache strategy for requests |
| `onRequest` | function | - | Hook called before each request |
| `onResponse` | function | - | Hook called after each response |
| `onError` | function | - | Hook called on errors |

## API methods

All methods return Promises and support both `.then()/.catch()` and `async/await` patterns.

### Pages

Retrieve and search pages created in the ButterCMS dashboard.

```javascript theme={null}
// Retrieve a single page
const page = await butter.page.retrieve("page_type", "page_slug", {
  locale: "en",
  preview: 1,     // Include draft content
  levels: 2       // Depth of nested references
});
console.log(page.data.data);

// List all pages of a type
const pages = await butter.page.list("page_type", {
  page: 1,
  page_size: 10,
  locale: "en",
  order: "-published"  // Sort by published date descending
});
console.log(pages.data.data);

// Search pages
const results = await butter.page.search("search query", {
  page: 1,
  page_size: 10
});
```

### Collections

Fetch content from Collections (structured data tables). `content.retrieve()` fetches one collection per call. Pass a single-element array with the collection's key.

```javascript theme={null}
// Retrieve collection content
const faq = await butter.content.retrieve(["faq"], {
  locale: "en",
  levels: 2
});
console.log(faq.data.data.faq);

// Retrieve several collections in parallel
const [faqResponse, navigationResponse] = await Promise.all([
  butter.content.retrieve(["faq"]),
  butter.content.retrieve(["navigation"])
]);
console.log(faqResponse.data.data.faq);
console.log(navigationResponse.data.data.navigation);
```

### Blog Posts

Access the built-in Blog Engine for posts, categories, tags, and authors.

```javascript theme={null}
// List blog posts
const posts = await butter.post.list({
  page: 1,
  page_size: 10,
  exclude_body: true,  // Exclude body for faster response
  category_slug: "news",
  tag_slug: "featured"
});

// Retrieve a single post
const post = await butter.post.retrieve("post-slug");
console.log(post.data.data.title);
console.log(post.data.data.body);

// Search posts
const searchResults = await butter.post.search("search query", {
  page: 1,
  page_size: 10
});
```

### Authors

```javascript theme={null}
// List all authors
const authors = await butter.author.list({
  include: "recent_posts"
});

// Retrieve a single author
const author = await butter.author.retrieve("jennifer-smith", {
  include: "recent_posts"
});
```

### Categories

```javascript theme={null}
// List all categories
const categories = await butter.category.list({
  include: "recent_posts"
});

// Retrieve a single category
const category = await butter.category.retrieve("news", {
  include: "recent_posts"
});
```

### Tags

```javascript theme={null}
// List all tags
const tags = await butter.tag.list({
  include: "recent_posts"
});

// Retrieve a single tag
const tag = await butter.tag.retrieve("featured", {
  include: "recent_posts"
});
```

### Feeds

Retrieve RSS, Atom, and Sitemap feeds.

```javascript theme={null}
// Get RSS feed
const rss = await butter.feed.retrieve("rss");

// Get Atom feed
const atom = await butter.feed.retrieve("atom");

// Get Sitemap
const sitemap = await butter.feed.retrieve("sitemap");
```

## Query parameters reference

| Parameter | Applies To | Description |
| - | - | - |
| `page` | Posts, Pages | Page number for pagination |
| `page_size` | Posts, Pages | Number of items per page |
| `exclude_body` | Posts | Exclude post body for faster response |
| `author_slug` | Posts | Filter posts by author |
| `category_slug` | Posts | Filter posts by category |
| `tag_slug` | Posts | Filter posts by tag |
| `include` | Authors, Categories, Tags | Include `recent_posts` with response |
| `locale` | Pages, Collections | Locale code for localized content |
| `preview` | Pages | Set to `1` to include draft content |
| `levels` | Pages, Collections | Depth of nested references (1–5, default 2) |

## Request cancellation

Cancel pending requests when needed (useful for React cleanup):

```javascript theme={null}
// Cancel all pending post requests
butter.post.cancelRequest();

// Cancel all pending page requests
butter.page.cancelRequest();
```

## Complete example

```javascript theme={null}
import Butter from "buttercms";

const butter = Butter(process.env.BUTTER_API_TOKEN);

async function fetchHomePage() {
  try {
    // Fetch the home page
    const page = await butter.page.retrieve("landing_page", "home", {
      locale: "en"
    });

    // Fetch navigation from collection
    const navigation = await butter.content.retrieve(["main_navigation"]);

    // Fetch recent blog posts
    const posts = await butter.post.list({
      page: 1,
      page_size: 3,
      exclude_body: true
    });

    return {
      page: page.data.data,
      navigation: navigation.data.data.main_navigation,
      recentPosts: posts.data.data
    };
  } catch (error) {
    console.error("Error fetching content:", error);
    throw error;
  }
}

// Usage
fetchHomePage().then(data => {
  console.log("Home page:", data.page);
  console.log("Navigation:", data.navigation);
  console.log("Recent posts:", data.recentPosts);
});
```

## Resources

> [card] [GitHub Repository](https://github.com/ButterCMS/buttercms-js)
>
> View source code, report issues, and contribute

> [card] [npm Package](https://www.npmjs.com/package/buttercms)
>
> Package details and version history


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.