Your frontend files are in the web/ directory of your Gadget app.
Gadget has authorization built-in, so frontend app users will not be able to call actions until they are granted permission. You can manage API and data permissions in accessControl/permissions. Read more on frontend data security and access control below.
For more general information on building frontends with Gadget, see the frontend guide.
Getting started with single-click frontends
After setting up a BigCommerce connection, your web/ directory will contain the following files:
File path
Description
web/api.js
An API client unique to your Gadget project and used to call your backend.
web/main.jsx
The entry point for your frontend project, sets up the BigDesign ThemeProvider.
The default route that renders the frontend you see in a store.
You can start to make edits right away in web/routes/index.jsx and hot module reloading (HMR) will update your frontend in realtime.
Reading data from your backend
You can use the API client in web/api.js along with React hooks from the @gadgetinc/react package to call your backend actions or HTTP routes, which reads data from your database.
If you need data that is not being stored in your database, you can make requests to the BigCommerce API from an action. We do not recommend making requests to the BigCommerce API directly from your frontend.
Using @gadgetinc/react hooks to read data
The @gadgetinc/react package provides hooks to call your backend actions or HTTP routes. You can use these hooks to read data from your database. These hooks provide a simple, React-ful way to interact with your API and provide additional functionality like:
loading/fetching state,
error handling,
pagination, filtering, sorting, and searching,
and realtime queries
As an example, this is how you could read a list of products from your database using a useFindMany hook. This will fetch the first 50 products from the database and display them in a table, with a loading spinner while the request is fetching data. An error will also be displayed if there is an issue reading data:
Docs on pagination, filtering, sorting, and searching on read can be found in your your API reference.
Want your frontend to stay up to date with your database?
Your Gadget API supports realtime queries out of the box. Using realtime queries will keep your frontend up to date with your database without needing to poll your backend.
If you want to read data that is not stored in your Gadget database, you can make requests to the BigCommerce API from your backend action. This is the recommended way to read data from BigCommerce in your frontend.
Here is an example of how you could read product data from BigCommerce in a global action, call that action from your frontend in a useEffect hook, and display the returned data in a table.
And here is how you can call that global action from your frontend using the useGlobalAction hook. This will fetch the first 5 products from BigCommerce and display them in a table, with a loading spinner while the request is fetching data. An error will also be displayed if there is an issue calling the action:
You will also have to grant the bigcommerce-app-users role access to the bigcommerce/readProducts action in
accessControl/permissions!
Writing data to your database
To write data to your database, you can make use of the useAction, useGlobalAction, or useActionForm hooks from the @gadgetinc/react package. These hooks allow you to call your backend actions from your frontend.
Simple example with useAction
For example, if you wanted to save a record to a gizmo model when a button is clicked, you could use the useAction hook to call the gizmo.create action:
If you want to write data back to BigCommerce, you can use the BigCommerce API client in your backend action. This is the recommended way to write data back to BigCommerce in your frontend.
Here is an example of how you could write product data to BigCommerce in a global action, and then call that action from your frontend using useGlobalAction. First, the action:
And here is how you can call that global action, passing in values for the custom productId and value params, from your frontend using the useGlobalAction hook:
This pattern allows you to send data to BigCommerce on a frontend action. Firing an action doesn't need to use a form! You can use any event to trigger an action, like a button click.
Building forms
If you are building a form, you can use the useActionForm hook from the @gadgetinc/react package. This hook simplifies form handling by providing a way to handle form state, validation, and submission.
The useActionForm hook wraps the react-hook-form library and works nicely with BigDesign. Here is an example of how you could use useActionForm to create a form that writes data to a manufacturer model:
Notice how there are no useState hooks managing form data. The useActionForm hook handles all of that for you!
For more information on building forms and additional examples, see the forms guide.
Working with useActionForm and BigDesign
The BigDesign component library extends the native HTML form elements with additional props. This is great news when working with
useActionForm, as all of the BigDesign components are compatible with the register function provided by useActionForm.
The register function is used to associate the BigDesign component with the field on the model, handles form state, and allows you to add custom frontend validation.
Calling HTTP routes
There are cases where you need to use an HTTP route instead of an action, for example, when you need to stream a response back to your frontend.
The @gadgetinc/react package has a useFetch hook for calling HTTP routes, or you can use api.fetch() built into the Gadget API client.
More information on calling HTTP routes from your frontend can be found in the calling HTTP routes guide.
Calling the BigCommerce API from an HTTP route
When you make a request to a BigCommerce API from an HTTP route, you will need to make sure the instance of the Gadget API client in your route is properly authenticated. You can use connections.bigcommerce to create an instance of the BigCommerce API client, similar to actions.
Authenticated API clients
If you use the useFetch() hook from within a single-click frontend, your API client will already be authenticated with the BigCommerce store. This means you can use connections.bigcommerce.current to create an instance of the BigCommerce API client in your HTTP route.
If you are calling your HTTP route from an external source, you will need to authenticate the API client with the store hash. You can use connections.bigcommerce.forStoreHash() to create an instance of the BigCommerce API client in your HTTP route.
Gadget also handles authorization and access control for your APIs. When you make a request to your backend, Gadget will check if the user has the correct permissions to access the data. If the user does not have the correct permissions, Gadget will return a 403 error.
You can see permissions for your app at accessControl/permissions.
BigCommerce merchants who interact with your app frontend are granted a bigcommerce-app-users role. By default, they will only have read and write access to the bigcommerce/store/ model's actions.
For any additional model or global actions, you will need to manually grant the bigcommerce-app-users role access to those actions.
HTTP routes are not secure by default, so it is recommended to use actions unless you have a specific need for a route.
Granting permissions and data security
It is a good idea to only grant the permissions that are necessary for your app to function. This will help keep your app secure and
prevent unauthorized access to your data.
If you are building a public single-click app, you need to handle multi-tenancy so stores cannot access each other's data. Read our multi-tenancy docs for more information.
session management
The BigCommerce connection manages records of the backend session model when using @gadgetinc/react-bigcommerce. When a merchant loads a single-click frontend, the <Provider /> will:
handle the call to the /load endpoint
verify the jwt payload
ensure the user matches the store owner (when multiple users are not enabled), or manage the user session when multiple users are enabled
If the payload is valid, the connection will provision a new session record for the userId and store. Valid sessions will be granted the bigcommerce-app-users role, which is used to manage access to actions and models. This session is then passed to all your backend application's model filters and is available within actions.