Quickstart: Connect to BigCommerce
This guide will show you how to set up a BigCommerce connection, subscribe to BigCommerce webhooks, and store the webhook data in a Gadget database.
Prerequisites
Before starting, you will need:
Step 1: Create a new Gadget app
First, create a new Gadget app and connect it to BigCommerce.
- Create a new Gadget app (https://gadget.new), select the BigCommerce app type, and enter a domain name
- Click the Connect to BigCommerce button on your app's Home page OR navigate to Settings → Plugins → BigCommerce
Step 2: Create a BigCommerce app, set callback URLs, and select scopes
Now create a BigCommerce app and connect it to the Gadget app.
- Create a new app in the BigCommerce Developer Portal and name your app
- Copy the Auth callback URL, and Load callback URL from Gadget to your BigCommerce app
- Select the Products Read-only OAuth scope and click Update & Close in the BigCommerce app
Step 3: Set up client credentials and install on a sandbox store
Finish connection setup by copying the client credentials from BigCommerce back to Gadget and installing the app on a sandbox store.
- Click View Client ID for your new app in BigCommerce and copy the Client ID and Client Secret to Gadget, then click Continue
- In your BigCommerce sandbox store, navigate to Apps → My Draft Apps, hover over your newly added app, and click Learn more
- Click Install
Congrats, you have successfully installed your app on a BigCommerce sandbox store!
The frontend you see is your Gadget app's React frontend built with the BigDesign library and embedded in the sandbox store.
You will also have a new data model at api/models/bigcommerce/store
that holds data for the BigCommerce sandbox you installed your app on.
Gadget takes care of the boilerplate that goes into setting up full-stack BigCommerce apps. By setting up the connection, you have done the following:
- set up a full-stack application
- handled BigCommerce OAuth and callbacks for a single-click app
- created a
bigcommerce/store
data model and saved the store data - taken care of frontend session management with the
session
model - installed the BigDesign library for your React frontend
Step 4: Subscribe to BigCommerce webhooks
BigCommerce webhooks are available as triggers on global actions.
To subscribe to BigCommerce webhooks:
- Navigate to
/api/actions
in Gadget, click +, and create a new action namedhandleProductCreate.js
- Click + in the Triggers card in your action and select BigCommerce
- Select the
store/product/created
webhook scope used to trigger the action
The action will now be run when the webhook is triggered.
BigCommerce webhook payloads only include the id
of the resource that triggered the webhook. To fetch the full resource data, you can use the included BigCommerce API client to fetch the resource data in the action.
- Copy-paste the following code into the action file:
1import type { ActionOptions } from "gadget-server";23export const run: ActionRun = async ({ params, logger, api, connections }) => {4 // get the BigCommerce API client for the current store5 const bigcommerce = connections.bigcommerce.current;6 // fetch the product data7 const product = await bigcommerce?.v3.get("/catalog/products/{product_id}", {8 path: {9 product_id: params.id,10 },11 });12 // log the product data and webhook payload13 logger.info({ product, params }, "product data and webhook payload");14};1516export const options: ActionOptions = {17 triggers: {18 bigcommerce: {19 webhooks: ["store/product/created"],20 },21 },22};
1import type { ActionOptions } from "gadget-server";23export const run: ActionRun = async ({ params, logger, api, connections }) => {4 // get the BigCommerce API client for the current store5 const bigcommerce = connections.bigcommerce.current;6 // fetch the product data7 const product = await bigcommerce?.v3.get("/catalog/products/{product_id}", {8 path: {9 product_id: params.id,10 },11 });12 // log the product data and webhook payload13 logger.info({ product, params }, "product data and webhook payload");14};1516export const options: ActionOptions = {17 triggers: {18 bigcommerce: {19 webhooks: ["store/product/created"],20 },21 },22};
- To test the webhook, create a new product in your BigCommerce sandbox store.
You should see the product data logged in the Gadget Logs.
To learn more about subscribing to BigCommerce webhooks, see the webhooks guide.
Step 5: Save the BigCommerce webhook payload
Now you can store that BigCommerce product data in your database so you can use it in your app.
- Right click on
api/models/bigcommerce
, select Add model, and create a newproduct
model - Navigate to
api/models/bigcommerce/product/schema
and add fields to the model with the following validations:
Field name | Field type | Validations |
---|---|---|
bigcommerceId | number | Uniqueness, Required |
name | string | Required |
store | belongs to |
Note: The store
field is a belongs to relationship to the bigcommerce/store
model. This is used to associate the product with the store it was created in. The inverse of the relationship should be a has many so that bigcommerce/store
has many bigcommerce/product
.
You can add more fields as needed, or remove fields you don't need! It is a good idea to only store the data required to power your app.
- Update
api/actions/handleProductCreate.js
to save the product data to the database:
1import { ActionOptions } from "gadget-server";23export const run: ActionRun = async ({ params, logger, api, connections }) => {4 // get the BigCommerce API client for the current store5 const bigcommerce = connections.bigcommerce.current;67 if (!bigcommerce) {8 throw new Error("Missing bigcommerce connection");9 }1011 // fetch the product data12 const product = await bigcommerce.v3.get("/catalog/products/{product_id}", {13 path: {14 product_id: params.id,15 },16 });1718 if (!product) {19 throw new Error("Missing product");20 }2122 // log the product data and webhook payload23 logger.info({ product, params }, "product data and webhook payload");2425 // store product data in the product data model26 await api.bigcommerce.product.create({27 bigcommerceId: product.id,28 name: product.name,29 store: {30 // get the bigcommerce/store id for the record stored in Gadget31 _link: connections.bigcommerce.currentStoreId,32 },33 });34};3536export const options: ActionOptions = {37 triggers: {38 bigcommerce: {39 webhooks: ["store/product/created"],40 },41 },42};
1import { ActionOptions } from "gadget-server";23export const run: ActionRun = async ({ params, logger, api, connections }) => {4 // get the BigCommerce API client for the current store5 const bigcommerce = connections.bigcommerce.current;67 if (!bigcommerce) {8 throw new Error("Missing bigcommerce connection");9 }1011 // fetch the product data12 const product = await bigcommerce.v3.get("/catalog/products/{product_id}", {13 path: {14 product_id: params.id,15 },16 });1718 if (!product) {19 throw new Error("Missing product");20 }2122 // log the product data and webhook payload23 logger.info({ product, params }, "product data and webhook payload");2425 // store product data in the product data model26 await api.bigcommerce.product.create({27 bigcommerceId: product.id,28 name: product.name,29 store: {30 // get the bigcommerce/store id for the record stored in Gadget31 _link: connections.bigcommerce.currentStoreId,32 },33 });34};3536export const options: ActionOptions = {37 triggers: {38 bigcommerce: {39 webhooks: ["store/product/created"],40 },41 },42};
- Create another product in your BigCommerce sandbox store and open
api/models/bigcommerce/product/data
to see your saved product data
Every time you add a product to your BigCommerce sandbox store, the product data will be also saved to your Gadget database!
For more information on working with data in BigCommerce, see our data guide.
Congrats! You have learned how to start building BigCommerce apps in Gadget!
Next steps
Now that we have set up a BigCommerce connection and learned how to subscribe to webhooks and store data in a Gadget database, you can start building a single-click app!
Check out our tutorial for building a full-stack, single-click app that automatically adds search keywords to products: