Skip to main content

Headless with B2B

Note

B2B only works with customer accounts. To use customer accounts, you'll need to update existing auth/login code that's related to legacy customer accounts.

This guide will help you build either a dedicated or blended B2B custom storefront. It'll take you through the following steps:

  1. Creating a storefront customerAccessToken
  2. Retrieving a companyLocationId
  3. Using the customerAccessToken and companyLocationId, contextualize storefront queries to get B2B pricing, volume pricing, and quantity rules
  4. Using the customerAccessToken and companyLocationId, update cart to be aware of B2B specific rules and pricing

Completing these steps should result in a storefront and cart that are contextualized for a B2B customer. Checking out with the cart will lead to a B2B checkout.

Without contextualization, your store's base pricing and product publishing will be used. Your B2B customers won't see a tailored experience from Catalogs, Payment Terms, and other B2B features that you have set up.



Anchor to Step 1: Get a Customer Accounts Access Token representing your CustomerStep 1: Get a Customer Accounts Access Token representing your Customer

The Customer Accounts access_token is one component needed to contextualize Storefront API queries for B2B and set a buyer identity on cart. It's a token representing your customer, and is obtained at the outcome of an authorization flow they will go through. Specifically, refer to the step to obtain the access_token in the Customer Accounts API.

The access_token resulting from this process is used in the step to contextualize product queries and cart, below, where it is referred to as customerAccessToken.


Anchor to Step 2: Get a Company Location IDStep 2: Get a Company Location ID

A companyLocationId is the other component needed to contextualize Storefront API queries for B2B and set a buyer identity on cart.

To obtain a companyLocationId, make a Customer Account API query on the customer to get the locations they have access to.

Anchor to Building a location selector for a multi-location customerBuilding a location selector for a multi-location customer

If the B2B customer can buy for multiple locations, then you'll need to build a location selector so that they can choose which location they want to buy for. The selector can be rendered anywhere on your storefront. To provide a seamless user experience, we recommend redirecting the user to the location selector after successfully authenticating with the Customer Account API.

  1. Display all locations from the customer query in a location selector on your storefront
  2. Allow the user to select a location
  3. Save the selected location’s ID for later requests

Anchor to Step 3: Contextualize Storefront API RequestsStep 3: Contextualize Storefront API Requests

Including the buyer argument on the @inContext directive will contextualize any Storefront API queries for a B2B customer. Any queries and mutations that need to be contextualized should be updated to include a buyer. Use the customerAccessToken and companyLocationId that you obtained in Step 1 and Step 2.

With a contextualized query, you can access a quantityRule and quantityPriceBreaks on a product variant, as well as get a price that's contextualized for the B2B customer.

Contextualized RecommendedProducts Query

GraphQL mutation

query RecommendedProducts (
$buyer: BuyerInput
) @inContext(buyer: $buyer) {
products(first: 5) {
nodes {
id
variants(first: 5) {
nodes {
id
quantityRule {
maximum
minimum
increment
}
quantityPriceBreaks(first: 5) {
nodes {
minimumQuantity
price {
amount
currencyCode
}
}
}
}
}
}
}
}

Variables

"buyer": {
"customerAccessToken": "shpsb_eyJh123456789",
"companyLocationId": "gid://shopify/CompanyLocation/10079785100",
}
Note

The buyer and country arguments for @inContext are ignored for cart queries. In order to get a contextualized cart, follow Step 4: Set the Buyer Identity on Cart to explicitly set the buyer identity with cartBuyerIdentityUpdate or during cartCreate. Other arguments on the @inContext directive (language) will still work as expected for cart queries.


Anchor to Step 4: Set the Buyer Identity on CartStep 4: Set the Buyer Identity on Cart

Include buyer identity on cart with the storefront customerAccessToken from an authenticated user and the companyLocationId. For more details on carts, see the Create and update a cart with the Storefront API guide.

Call the cartCreate mutation. The buyerIdentity argument should be supplied with the input fields customerAccessToken and companyLocationId.

Setting Buyer Identity on Cart

GraphQL mutation

mutation cartCreate {
cartCreate {
cart {
# Cart fields
}
userErrors {
field
message
}
}
}

Variables

{
"buyerIdentity": {
"customerAccessToken": "shpsb_eyJh123456789",
"companyLocationId": "gid://shopify/CompanyLocation/10079785100",
}
}

The cartBuyerIdentityUpdate mutation also supports a buyerIdentity argument. Use this mutation to update and existing cart with the customerAccessToken and companyLocationId.

Caution

Updating the buyer identity on an existing cart with added products may result in an invalid cart. Products not published for the current B2B customer will be removed from cart. Products that are published, but with new quantity rules, will remain unchanged. Querying the cart will not return an error, but making updates to the cart may result in an error which can be read in the returned userErrors field. If the B2B customer continues to checkout with an invalid cart, the issues will be displayed as an error message.


Anchor to (Optional) Building a gated B2B store(Optional) Building a gated B2B store

With headless you have the flexibility to gate specific features to B2B customers. For example, you can, allow potential B2B customers to view products, but without prices or the ability to order.

When querying for a product, only include the price if there is a logged in session (valid customerAccessToken with a saved companyLocationId). Only show the "add to cart" button if there is a logged in session (valid customerAccessToken with a saved companyLocationId).


Was this page helpful?