Exploring Bigcommerce API with JavaScript

Bigcommerce is a popular e-commerce platform used by small and large businesses alike. The platform provides a robust set of APIs that makes it easier to build apps, plugins, integration and other custom solutions. In this post, we will explore these public APIs and how you can use them using JavaScript.

Getting Started with Bigcommerce API

Before we get started, you will need to get an API key and access token. You can generate these by logging into your Bigcommerce account and navigating to Advanced Settings > API Accounts. Once you have these credentials, you can make API requests using different HTTP methods such as GET, POST, PUT, DELETE etc.

Creating a Simple Application

To get started, let's create a simple application that uses the Bigcommerce API to get and display the list of products on a webpage.

First, create an HTML document with a div element to hold the products list.

<!doctype html>
<html lang="en">
<head>
    <title>Bigcommerce API Example</title>
</head>
<body>
    <div id="products"></div>
    <script src="app.js"></script>
</body>
</html>

Next, create a JavaScript file named app.js and add the following code to it:

const apiUrl = 'https://api.bigcommerce.com/stores/{store_hash}/v3/catalog/products';
const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'Authorization': 'Bearer {access_token}'
};

fetch(apiUrl, {
  method: 'GET',
  headers: headers
})
.then(response => response.json())
.then(data => {
  let productsHtml = '<h2>Products</h2><ul>';
  data.data.forEach(product => {
    productsHtml += `<li>${product.name}</li>`;
  });
  productsHtml += '</ul>';
  document.getElementById('products').innerHTML = productsHtml;
})
.catch(error => console.error(error));

Replace {store_hash} and {access_token} placeholders in the apiUrl and headers variables respectively with your own credentials. This JavaScript code makes a GET request to the Bigcommerce API endpoint that returns a list of products. The response is then parsed as JSON and displayed on the webpage.

Open the HTML file in your browser and you should see a list of product names.

More API Examples

The Bigcommerce API provides many other endpoints for working with orders, customers, categories, brands and more. Here are a few more code examples for working with these endpoints.

Create a Customer

const apiUrl = 'https://api.bigcommerce.com/stores/{store_hash}/v3/customers';
const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'Authorization': 'Bearer {access_token}'
};
const requestBody = {
  "email": "example@example.com",
  "first_name": "John",
  "last_name": "Doe",
  "phone": "555-555-5555"
};

fetch(apiUrl, {
  method: 'POST',
  headers: headers,
  body: JSON.stringify(requestBody)
})
.then(response => response.json())
.then(data => {
  console.log(data);
})
.catch(error => console.error(error));

Update a Product

const productUrl = 'https://api.bigcommerce.com/stores/{store_hash}/v3/catalog/products/{product_id}';
const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'Authorization': 'Bearer {access_token}'
};
const requestBody = {
  "name": "New Product Name",
  "type": "physical",
  "price": 19.99,
  "categories": [1,2,3], // category IDs
  // other product fields to update
};

fetch(productUrl, {
  method: 'PUT',
  headers: headers,
  body: JSON.stringify(requestBody)
})
.then(response => response.json())
.then(data => {
  console.log(data);
})
.catch(error => console.error(error));

Conclusion

In this post, we explored the Bigcommerce API and demonstrated how to use it using JavaScript. Using these APIs, you can build apps, plugins, integrations, and other custom solutions that automate and enhance the functionality of your Bigcommerce store. Happy Building!

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf