npm.io
0.0.6 • Published 5h ago

bigcommerce-store-api

Licence
MIT
Version
0.0.6
Deps
0
Vulns
0
Weekly
0

bigcommerce-store-api

This is a simple BigCommerce API client library written in Typescript, it allows developers to interact with the BigCommerce platform. By using this library, developers can easily integrate their applications with BigCommerce stores, enabling them to access and manipulate store data such as products, orders, customers, and more.

Getting Started

Installation

npm install bigcommerce-store-api

Basic Usage

import BigCommerceStoreApi from 'bigcommerce-store-api';

const apiClient = new BigCommerceStoreApi({
    storeHash: 'storeHash',
    accessToken: 'accessToken',
    // optional, only needed by the Current Customer API
    storeDomain: 'store.example.com',
    // optional, only needed by the Shipping Provider and Tax Provider APIs
    appDomain: 'app.example.com',
});

const response = await apiClient.v3.products.getProducts({
    limit: 10,
});

if (response.status === 'success') {
    console.log(response.data);
} else {
    console.log(response.errors);
}

const responseProduct = await apiClient.v3.products.getProduct(1234);

if (responseProduct.status === 'success') {
    console.log(responseProduct.data);
} else {
    console.log(responseProduct.errors);
}
Low level API

If you need to directly call the get, post put delete methods with a custom URL and custom parameter, you can get the apiClient.request object and call the corresponding method. For example:

apiClient.request.put({
    path: 'v3/catalog/products',
    contentType: 'application/json',
    body: [
        { id: 123, name: 'New Product Name' },
        { id: 456, name: 'New Product Name' }
    ],
    query: {
        include_fields: ['name'],
    },
});

Error Handling

bigcommerce-store-api will not throw an error, instead it return error details in response

const response = await apiClient.v3.products.getProduct(1234);

if (response.status === 'error') {
    console.log(response.errors);
}

Every response, success or error, also includes the response headers and the raw response_text to help with debugging:

const response = await apiClient.v3.products.getProducts();

console.log(response.http_status);
console.log(response.headers['x-rate-limit-requests-left']);
console.log(response.response_text);

But for program safety, I recommend implementing a try catch to ensure your code is not interrupted by a library bug. For example:

const response = await productApi
    .getProducts()
    .catch(error => {
        console.log(error);
    });

if (response) {
    console.log(response);
}

// or
try {
    const response = await productApi.getProducts();
    console.log(response);
} catch (error) {
    console.log(error);
}

Keywords