Run this in your commandline:

npm install buttercms --save

Butter can also be loaded using a CDN:

<script src="https://cdnjs.buttercms.com/buttercms-1.0.17.min.js"></script>

The source code is available on Github.

Set your API token:

var butter = require('buttercms')('your_api_token');

Using ES6:

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

Using CDN:

<script src="https://cdnjs.buttercms.com/buttercms-1.0.17.min.js"></script>

<script>
  var butter = Butter('your_api_token');
</script>

Then run:

butter.post.list({page: 1, page_size: 10}).then(function(response) {
  console.log(response)
})

This API request fetches your blog posts. Your account comes with one example post which you'll see in the response.

Next, run:

butter.content.retrieve(['homepage_headline']).then(function(response) {
  console.log(response)
});

This API request fetches homepage headline content. You can setup your own custom content fields to manage any content kind of content you need.

ButterCMS lets you manage content using our dashboard and integrate it into your front-end of choice with our API. You can use ButterCMS for new projects as well as add it to existing codebases.

ButterCMS comes with a blogging interface and APIs for blog posts, categories, authors, and XML feeds. For other use cases you can setup custom content fields based on your needs.

For example, if you wanted to enable a non-technical person to edit some copy on your homepage, you might create two custom content fields called "Homepage Headline" and "Homepage Main Paragraph". The non-technical person would be able to edit the values of the fields and the JSON API output would look something like this:

{
  "homepage_headline": "OMG I can edit this!",
  "homepage_paragraph": "I love ButterCMS"
}

Custom content fields can be used for more complex cases like creating dynamic pages and managing knowledgebases, which we will cover in this guide.

Table of Contents

Introduction

Learn how to quickly build a CMS-powered blog with JavaScript. This guide uses React.js but Butter works with any client-side JavaScript framework including Angular and Vue.js. For server-side integration see our Node.js guide.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Display posts

To display posts we create a few routes (using react-router) and components that fetch blog posts from the Butter API. See our API reference for additional options such as filtering by category or author. The response also includes some metadata we'll use for pagination.

routes.jsx:

import React from 'react';
import { Router, IndexRoute, Route } from 'react-router';

import App from './App';
import BlogHome from './BlogHome';
import BlogPost from './BlogPost';

const Routes = (props) => (
  <Router {...props}>
    <Route path="/blog" component={App}>
      <IndexRoute component={BlogHome} />
      <Route path="/p/:page" component={BlogHome} />
      <Route path="/post/:slug" component={BlogPost} />
    </Route>
  </Router>
);

export default Routes;

BlogHome.js:

import React, { Component } from 'react';
import { Link } from 'react-router'
import Butter from 'buttercms'

const butter = Butter('your_api_token');

class BlogHome extends Component {

  constructor(props) {
    super(props);

    this.state = {
      loaded: false
    };
  }

  fetchPosts(page) {
    butter.post.list({page: page, page_size: 10}).then((resp) => {
      this.setState({
        loaded: true,
        resp: resp.data
      })
    });
  }

  componentWillMount() {
    let page = this.props.params.page || 1;

    this.fetchPosts(page)
  }

  componentWillReceiveProps(nextProps) {
    this.setState({loaded: false});

    let page = nextProps.params.page || 1;

    this.fetchPosts(page)
  }

  render() {
    if (this.state.loaded) {
      const { next_page, previous_page } = this.state.resp.meta;

      return (
        <div>
          {this.state.resp.data.map((post) => {
            return (
              <div key={post.slug}>
                <Link to={`/post/${post.slug}`}>{post.title}</Link>
              </div>
            )
          })}

          <br />

          <div>
            {previous_page && <Link to={`/p/${previous_page}`}>Prev</Link>}

            {next_page && <Link to={`/p/${next_page}`}>Next</Link>}
          </div>
        </div>
      );
    } else {
      return (
        <div>
          Loading...
        </div>
      )
    }
  }
}

export default BlogHome;

Our BlogPost.js compnent for displaying a full post includes information such as author and publish date. See a full list of available post properties in our API reference. We use react-helmet to set HTML title and meta tags for SEO.

import React, { Component } from 'react';
import Butter from 'buttercms'
import { Helmet } from "react-helmet";

const butter = Butter('your_api_token');

class BlogPost extends Component {

  constructor(props) {
    super(props);

    this.state = {
      loaded: false
    };
  }

  componentWillMount() {
    let slug = this.props.params.slug;

    butter.post.retrieve(slug).then((resp) => {
      this.setState({
        loaded: true,
        post: resp.data.data
      })
    });
  }

  render() {
    if (this.state.loaded) {
      const post = this.state.post;

      return (
        <div>
          <Helmet>
            <title>{post.seo_title}</title>
            <meta name="description" content={post.meta_description} />
            <meta name="og:image" content={post.featured_image} />
          </Helmet>

          <h1>{post.title}</h1>
          <div dangerouslySetInnerHTML={{__html: post.body}} />
        </div>
      );
    } else {
      return (
        <div>
          Loading...
        </div>
      );
    }
  }
}

export default BlogPost;

Categories, Tags, and Authors

Use Butter's APIs for categories, tags, and authors to feature and filter content on your blog.

See our API reference for more information about these objects:

RSS, Atom, and Sitemap

Butter generates RSS, Atom, and sitemap XML markup. To use these on your blog, return the generated XML from the Butter API with the proper content type headers.

Comments

Butter doesn't provide an API for comments due to the excellent existing options that integrate easily. Two popular servies we recommend are:

Both products are free, include moderation capabilities, and give your audience a familiar commenting experience. They can also provide some additional distribution for your content since users in their networks can see when people comment on your posts. For a minimalist alternative to Disqus, check out RemarkBox or for an open-source option, Isso.

CSS

Butter integrates into your front-end so you have complete control over the design of your blog. Use the following CSS as boilerplate for post content styling.

.post-container {
  h1, h2, h3, h4, h5 {
    font-weight: 600;
    margin-bottom: 1em;
    margin-top: 1.5em;
  }

  ul, ol {
    margin-bottom: 1.25em;

    li {
      margin-bottom: 0.25em;
    }
  }

  p {
    font-family: Georgia, Cambria, "Times New Roman", Times, serif;
    font-size: 1.25em;
    line-height: 1.58;
    margin-bottom: 1.25em;
    font-weight: 400;
    letter-spacing: -.003em;
  }

  /* Responsive default image width */
  img {
    max-width: 100%;
    height: auto;
  }

  /* Responsive floating */
  @media only screen and (min-width: 720px)  {
    .butter-float-left {
      float: left;
      margin: 0px 10px 10px 0px;
    }

    .butter-float-right {
      float: right;
      margin: 0px 0px 10px 10px;
    }
  }

  /* Image caption */
  figcaption {
    font-style: italic;
    text-align: center;
    color: #ccc;
  }

  p code {
    padding: 2px 4px;
    font-size: 90%;
    color: #c7254e;
    background-color: #f9f2f4;
    border-radius: 4px;
    font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
  }

  pre {
    display: block;
    padding: 1em;
    margin: 0 0 2em;
    font-size: 1em;
    line-height: 1.4;
    word-break: break-all;
    word-wrap: break-word;
    color: #333333;
    background-color: #f5f5f5;
    font-family: Menlo, Monaco,Consolas, "Courier New", monospace;
  }
}

Migration

To import content from another platform like WordPress or Medium, send us an email.

Table of Contents

Introduction

Learn how to make content on a static page editable using Butter and JavaScript. This guide uses React.js but Butter works with any client-side JavaScript framework including Angular and Vue.js. For server-side integration see our Node.js guide.

Making your content dynamic with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Setup content fields

Let's suppose we want to add a CMS to a static FAQ page with a title and a list of questions with answers. Here's the initial code for a React component with the Butter API client installed:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('your_api_token');

class Faq extends Component {
  render() {
    <div>
      <h1>FAQ</h1>

      <ul>
        <li>
          <h4>When was this company started?</h4>
          <p>2014</p>
        </li>
        <li>
          <h4>What forms of payment do you accept?</h4>
          <p>Credit cards and checks.</p>
        </li>
      </ul>
    </div>
  }
}

export default Faq;

Making your content dynamic with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

To setup custom content fields, first sign in to the Butter dashboard.

Create a new workspace or click on an existing one. Workspaces let you organize content fields in a friendly way for content editors and have no effect on development or the API. For example, a real-estate website might have a workspace called "Properties" and another called "About Page".

Once you're in a workspace click the button to create a new content field. Choose the "Object" type and name the field "FAQ Headline":

After saving, add another field but this time choose the "Collection" type and name the field FAQ Items:

On the next screen setup two properties for items in the collection:

Now go back to your workspace and update your heading and FAQ items.

Integrate your app

To display this dynamic content in our app, we fetch the fields with an API call and then reference them in our view. Here's what the code looks like:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('your_api_token');

class Faq extends Component {

  constructor(props) {
    super(props);

    this.state = {
      content: null
    };
  }

  componentWillMount() {
    let slug = this.props.params.slug;

    butter.content.retrieve(["faq_headline", "faq_items"]).then((resp) => {
      this.setState({
        content: resp.data.data
      })
    });
  }

  render() {

    if (this.state.content) {
      return (
        <div>
          <h1>{this.state.content.faq_headline}</h1>

          <ul>
            {this.state.content.faq_items.map(item => {
              return (
                <li>
                  <h4>{item.question}</h4>
                  <p>{item.answer}</p>
                </li>
              );
            })}
          </ul>
        </div>
      );
    } else {
      return (
        <div>
          Loading...
        </div>
      );
    }
  }
}

export default Faq;

That's it! The values entered in the Butter dashboard will immediately update the content in our app.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Table of Contents

Introduction

Learn how to create dynamic pages using Butter and JavaScript. This guide uses React.js but Butter works with any client-side JavaScript framework including Angular and Vue.js. For server-side integration see our Node.js guide.

Creating dynamic pages with Butter is a two-step process:

1) Setup custom content fields in Butter
2) Integrate the fields into your application

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Setup content fields

Let's suppose we want to be able to create pages for retail-chain locations using ButterCMS and we're starting out with a basic static page template for a single location:

routes.jsx:

import React from 'react';
import { Router, IndexRoute, Route } from 'react-router';

import Location from 'Location';

const Routes = (props) => (
  <Router {...props}>
    <Route path="/location/:slug" component={Location} />
  </Router>
);

export default Routes;

Location.js:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('your_api_token');

class Location extends Component {
  render() {
    return (
      <div>
        <h1>Widget Store - Chicago</h1>
        <img width="100%" src="/images/chicago.jpg">
        <p>Our Chicago location is located at 566 Wacker Drive.</p>
      </div>
    );
  }
}

export default Location;

Enabling new pages to be created with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

To setup custom content fields, first sign in to the Butter dashboard.

Create a new workspace or click on an existing one. Workspaces let you organize content fields in a friendly way for content editors and have no effect on development or the API. For example, a real-estate website might have a workspace called "Properties" and another called "About Page".

Once you're in a workspace click the button to create a new content field. Choose the "Collection" type and name the field "Location Pages":

On the next screen, we'll setup the properties for each page in the collection.

Now go back to the work space and create a location:

Integrating your app

To create these pages in our app, we create a dynamic route that fetches content for the page by using a URL parameter. Here's what the code looks like:

Location.js:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('your_api_token');

class Location extends Component {

  constructor(props) {
    super(props);

    this.state = {
      content: null
    };
  }

  componentWillMount() {
    let slug = this.props.params.slug;

    butter.content.retrieve([`location_pages[slug=${slug}]`]).then((resp) => {
      this.setState({
        content: resp.data.data
      })
    });
  }

  render() {
    if (this.state.content) {
      const location = this.state.content.location_pages[0];

      return (
        <div>
          <h1>Widget Store - {location.name}</h1>
          <img width="100%" src={location.feature_image}>
          <p>{location.description}</p>
        </div>
      );
    } else {
      return (
        <div>
          Loading...
        </div>
      );
    }
  }
}

export default Location;

That's it! If you browse to /locations/chicago, you'll see the content we just entered into Butter.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Get started now

Sign up with Google Sign up with Github
or