Create gates in the Shopify admin
This is the first part of a tutorial series to build a tokengating app. Read the overview section before starting this tutorial.
What you'll learn
Anchor link to section titled "What you'll learn"In this tutorial, you'll use Node.js, React, Shopify App Bridge and Polaris components to create an admin app and use Gates API to create gates.
The admin app has two pages:
- Tokengates index: Uses the Polaris
List
component to display previously created tokengates. - Tokengate form: Enables merchants to create a new tokengate with a name, a discount, unlocking token addresses, and a list of products to be gated.
The backend consists of a Node.js Express app that leverages the Gates API to create, update, and delete gate configurations and gate subjects.
Requirements
Anchor link to section titled "Requirements"- You've created a Partner account.
- You've created a development store that uses generated test data.
You've created an app that uses the Express.js app template and Shopify CLI.
You can create a new app using the Express.js app template by running the following command:
Step 1: Start your development server
Anchor link to section titled "Step 1: Start your development server"You can view and test your frontend components in the Shopify admin as you build them. After you've created an app, navigate to your app's directory and start your local server to build and preview your app:
Step 2: Create a form to configure a product gate
Anchor link to section titled "Step 2: Create a form to configure a product gate"The form enables merchants to:
- Define an exclusive discount
- Define segments by adding token collections
- Apply tokengates to products
Install required packages
Anchor link to section titled "Install required packages"You need to install @shopify/react-form to your required packages. We use this to manage React forms by providing custom hooks.
Create the form component
Anchor link to section titled "Create the form component"The form submits the form data to the Gates API to create a new gate.
- In the
/web/frontend/pages
directory, create a new file calledCreateTokengate.jsx
. The app boilerplate code comes with automatic routing, where a new route is created when a file is added to this directory. Add the following code to the file:
Create a resource picker component for the form
Anchor link to section titled "Create a resource picker component for the form"- In the
/web/frontend/components
directory, create a new file calledTokengatesResourcePicker.jsx
. Add the following code to the file:
Step 3: Create the list component
Anchor link to section titled "Step 3: Create the list component"The list component fetches the gates from the Gates API.
- In the
/web/frontend/components
directory, create a new file calledTokengatesList.jsx
. Add the following code to the file:
Add the list component to the home page
Anchor link to section titled "Add the list component to the home page"Import the TokengatesList
component and include the component markup in your index page.
- Open
/web/frontend/pages/index.jsx
. Replace your code with:
In the browser, view your development store. After this step, your form should look like this:
Step 4: Create the backend for the admin app UI to interact with the Gates API
Anchor link to section titled "Step 4: Create the backend for the admin app UI to interact with the Gates API"Now that you have created the frontend, you need to request additional scopes for your app to interact with gates. After, add the backend to create discounts, create, retrieve and delete gates.
Setup the gates scope
Anchor link to section titled "Setup the gates scope"- In
/shopify.app.toml
, update the existing scopes. Modify the existing scopes declaration to add the
read_gates
,write_gates
,read_discounts
, andwrite_discounts
scopes.Deploy your updated configuration to Shopify:
Start your development server. If your server is already running, restart it.
View your app again in your store admin. If prompted for an app update that applies the new scopes, then accept the update.
Enable the unstable
API
Anchor link to section titled "Enable the unstable API"The Gates API is available in the unstable
API version. Configure the Shopify API client to use the unstable
API version.
- Open
/web/shopify.js
Set the
shopify
configuration keyapiVersion
tounstable
.
Create the backend to interact with the Gates API
Anchor link to section titled "Create the backend to interact with the Gates API"- Create a new directory
/web/api/
, and inside create a new file calledconstants.js
. Add the following code to the file:
In the
/web/api
directory, create a new file calledcreate-discount.js
.Add the following code to the file:
In the last part of this tutorial series, after deploying your function to apply the gate discount, you'll come back to fill in YOUR_FUNCTION_ID
.
- In the
/web/api/
directory, create a new file calledcreate-gate.js
. Add the following code to the file:
- In the
/web/api/
directory, create a new file calledretrieve-gates.js
. Add the following code to the file:
In the
/web/api/
directory, create a new file calleddelete-gate.js
.Add the following code to the file:
Step 5: Add the API endpoints to the server
Anchor link to section titled "Step 5: Add the API endpoints to the server"The app uses authenticated endpoints to create, update, delete, and view the gates. You'll import the helpers createGate
, retrieveGates
, and deleteGate
that you created in the previous steps, and call those endpoints with a GET
and POST
request.
- Open
/web/index.js
. Replace the code with:
Step 6: Create a gate
Anchor link to section titled "Step 6: Create a gate"The admin UI to create tokengates is complete. Try it out by entering details in the form and pressing create. The gate should be created and included in the tokengate list.
To ensure later steps in this tutorial are successful, make sure to:
- Input token addresses in the Segments field that you have access to
- Select at least one product when creating a tokengate
- Not leave the discount or name fields blank
- If you're using a custom storefront built using Hydrogen or the Headless channel, then follow the build a tokengated storefront tutorial series.
- If you plan to integrate your app with a regular storefront, then show gates on the storefront using a theme app extension.