Lab - Create a Customer Flow in Postman
Introduction
In this exercise, you’ll build a workflow in Postman that mimics the standard operations a headless storefront would utilize to allow customer registration, address management, and login.
Prerequisites
- A BigCommerce sandbox store or trial store, or a full production store
- Postman
- An existing Postman environment and collection as configured in previous labs
Form Fields Configuration
To get the full benefit from this workflow, you should make sure that you have configured one to three custom Form Fields for customer account signup.
In your BigCommerce control panel, manage Form Fields in Settings > Advanced > Account sign up form. You’ll be building support for Account Signup Fields, not Address Fields. The following field types will be supported in your Postman logic, so make sure to set up one to three custom fields of one or more of these types.
- Pick List
- Radio Buttons
- Date Field
- Text Field
Postman Recap
As a reminder, your work in previous labs should have resulted in the following Postman configuration, which will be important for the following exercise steps:
- An environment with variables set for
v3_token,store_hash,storefront_channel_idandprivate_token(and optionallystorefront_tokenfrom the browser-token request) - A collection with Bearer Token authentication using the value of
private_token - A “REST Create Private Token (server-to-server)” request
- A GraphQL headers preset
Before beginning the following exercise, make sure to run “REST Create Storefront CIT” to populate storefront_impersonation_token on the environment with a valid token.
Query Custom Form Fields
In order to provide appropriately formatted custom field values for the account, you’ll first need to inspect the details of those form fields.
Step 1: Create the Request
- Create a new “GQL Get Form Fields” request and save it to your collection. This request requires the following details.
- In the Headers tab, select your GraphQL headers preset to apply “Accept” and “Content-Type” headers.
- Enter the following query in the Body tab:
- Send the request and observe the results.
Step 2: Add Tests and Store Values
This query inspects the details of the user-defined customer account registration Form Fields. Specifically, the details of the types RadioButtonsFormField, PicklistFormField, DateFormField and TextFormField.
The post-response script you’ll set up next will not only verify that at least one custom Form Field exists, but will also store collection variables with appropriate values for the fields that were found, in a format ready for the customer registration mutation you’ll configure later.
- Enter the following “Post-response” value in the Scripts tab.
- Send the request again and make sure the tests are successful.
- Add the following script after the initial tests:
This script adds one more test in the case of multi-choice style fields, verifying that at least one option exists.
From here, the script will store three different collection variables with values that will be expected by customer account registration. Which variables contain values will depend on the types of Form Fields you configured in your control panel.
- For multi-choice style fields (Pick List and Radio Button fields), the first available option will be stored.
- For Date fields, the value stored will be the minimum date/time if one is configured. In order of precedence, the value tried will then be the field’s default value, any configured maximum date/time, or the current time.
- Any Text fields will have a hard-coded test value stored.
Send the request again and verify that the variables customer_multiple_choices, customer_dates, and customer_texts are populated on the collection, with appropriate values depending on the types of your fields.
Register a Customer
Step 1: Configure New Collection Variables
- Add the following variables to your Postman collection, with appropriate values for the customer account you wish to create.
These values will be used both for account registration and for login, which is why you’re storing them in a persistent location.
Step 2: Create the Request
- Create a new HTTP request and save it in your collection with the name “GQL Register Customer”.
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers, Body type and “Disable cookie jar” setting.
- Enter the following query:
- Enter the following in the GraphQL Variables section of the Body tab. Feel free to change the customer name details. The values previously prepared when customer Form Fields were queried will be passed in for the various field entries; any value types for which you do not have corresponding fields will simply receive an empty array.
Don’t send the request just yet, as you’ll want to have post-response scripts in place before registering the new account.
Step 3: Add Tests and Store Values
- Enter the following “Post-response” value in the Scripts tab.
- Send the request and verify that tests are successful.
- In your BigCommerce control panel, navigate to the store’s customers and verify the details of the new customer record have been set correctly, including for custom fields.
Authenticate Customer
In this request, you’ll use the login mutation to authenticate the newly created customer account, using the same email and password you first used to register the customer.
In addition to verifying the user credentials, this request will also capture the Customer Access Token for use in the X-Bc-Customer-Access-Token header for the subsequent customer requests.
Step 1: Create the Request
- Create a new HTTP request and save it in your collection with the name “GQL Customer Login”.
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers (minus X-Bc-Customer-Id), Body type and “Disable cookie jar” setting.
- Enter the following query:
- Enter the following in the GraphQL Variables section of the Body tab.
- Send the request and observe the response.
Step 2: Add Tests
- Enter the following “Post-response” value in the Scripts tab.
- Send the request and verify that all tests succeed.
- Edit your collection variables and change
customer_passwordto a different value, then re-send the request. Verify that tests fail with these incorrect credentials, and observe the response. - Reset the
customer_passwordcollection variable to the correct password.
Step 3: Store Values
To finish out this request, you’ll capture both the “logged-in” customer’s ID and the unique Customer Access Token in collection variables.
- Add the following after the existing “Post-response” code in the Scripts tab.
- Send the request and verify that the
customer_idandcustomer_access_tokenvariables are set on your collection.
Add a Customer Address
Customer addresses have the same support for custom Form Fields that customer accounts do, so you could use the same workflow we have already seen to discover and then populate these fields when adding a new address. For simplicity’s sake, we’ll skip these custom Form Fields for addresses, providing only built-in field values. In order for this request to work correctly, you’ll need to make sure you have no Form Fields configured for addresses that are required.
You’ve captured a unique Customer Access Token identifying the “logged-in” customer in the previous request, and that context will be provided not in the body of the mutation or its variables, but in the X-Bc-Customer-Access-Token header.
Step 1: Create the Request
- Create a new HTTP request and save it in your collection with the name “GQL Add Customer Address.”
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers, Body type and “Disable cookie jar” setting.
- In the Headers, add a new header as follows.
- Enter the following query:
- Enter the following in the GraphQL Variables section of the Body tab. Feel free to change the address details as you please.
Step 2: Add Tests
- Enter the following “Post-response” value in the Scripts tab.
- Send the request and verify that all tests succeed.
- In your BigCommerce control panel, navigate to the matching customer account record and verify that the new address exists in the customer’s address book.
Fetch Customer Details
- Create a new HTTP request and save it in your collection with the name “GQL Get Customer.”
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers, Body type and “Disable cookie jar” setting. Make sure Headers includes X-Bc-Customer-Access-Token.
- Enter the following query:
- Send the request and observe the results.
OPTIONAL: Request Password Reset
The following requests will simulate the workflow required to allow a user to initiate a password reset request, then complete the password reset using the token provided in the resulting email.
If you wish to try out this workflow, this will require the ability to receive the password reset email sent by BigCommerce and retrieve the token provided in the reset link it contains.
You should therefore make sure that email address used previously to create the customer account is one for which you can successfully receive email. If needed, update the customer_email collection variable and re-run the “GQL Register Customer” and “GQL Customer Login” requests to capture the ID of a customer account with a valid email.
Step 1: Create the Request
- Create a new HTTP request and save it in your collection with the name “GQL Request Password Reset”.
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers (minus the “X-Bc-Customer-Access-Token” header), Body type and “Disable cookie jar” setting.
- Enter the following query:
- Enter the following in the GraphQL Variables section of the Body tab.
Note that you’re entering a purely arbitrary URL path to serve as the “password reset” destination on a hypothetical storefront. Your own application would be in control of what URL route is appropriate for customers to be directed to.
Step 2: Add Tests
- Enter the following “Post-response” value in the Scripts tab.
- Send the request and verify that all tests succeed.
Step 3: Verify Email Receipt
If your previous request succeeded, this should have initiated a reset email to the address associated with the customer account. The token needed for the password reset request can be obtained only from this email.
- Access the inbox for the email address associated with the customer account using the appropriate email application.
- Open the “Password Change Request” email that was received from your store.
This email should contain a link to a URL like the following:
https://{store-domain}/my-password-reset?c={customer-ID}&t=**{token}**
Note the URL path /my-password-reset, which was designated by the requestPasswordReset mutation itself. The part of the URL you are now concerned with is the {token} value passed to the t querystring parameter.
- Copy the token value from the URL contained in the password reset email.
- In Postman, edit your collection and add the variable
password_reset_tokenwith the token value you copied.
OPTIONAL: Reset Password
In this request, you’ll complete a customer password reset using the token value you previously captured.
Step 1: Enter a New Password Value
The intent is to update the customer account password with a new value, so you’ll need to start by providing this new value to Postman.
- Edit your collection and change the value of the
customer_passwordvariable to a different password string.
Step 2: Create the Request
- Create a new HTTP request and save it in your collection with the name “GQL Reset Password”
- Set the same configuration that was used for the previous request, including the HTTP method, URL, Authorization, Headers, Body type and “Disable cookie jar” setting.
- Enter the following query:
- Enter the following in the GraphQL Variables section of the Body tab.
Step 3: Add Tests
- Enter the following “Post-response” value in the Scripts tab.
- Send the request and verify that all tests succeed.
Step 4: Re-Authenticate
- Re-send the “GQL Customer Login” request to re-authenticate using the new password.
- Verify that all tests once again succeed.