Creating a Storefront App
This guide covers a series of queries and mutations to handle typical storefront application scenarios.
Let's set up our Firmhouse project before we begin. You can find step-by-step instructions here to configure your project. Below is a list of things you should complete before we start:
Create products. You can follow this guide.
If your project type is Product-as-a-service, Create at least one Plan to use as your default plan. You can follow this guide.
Set up a payment provider.
Generate a Project Access Token by going to the Settings > Integrations page. For this tutorial a token with
Storefrontaccess type will be sufficient.
Fetching products
To display the products, you need to use the products query. This query gives you all the information about the products you have set up in the Firmhouse Portal.
const projectAccessToken = '<Your-project-access-token>'
const headers = new Headers({
'Content-Type': 'application/json',
'X-Project-Access-Token': projectAccessToken
})
const graphql = JSON.stringify({
query: `query products($after: String, $first: Int){
products(after: $after, first: $first) {
totalCount
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
nodes {
available
eligibleForDiscount
graceCancellationEnabled
graceCancellationPeriod
graceCancellationUnit
id
imageUrl
interval
intervalUnitOfMeasure
mandatory
maximumCommitmentEnabled
maximumCommitmentPeriod
maximumCommitmentUnit
metadata
minimumCommitmentEnabled
minimumCommitmentPeriod
minimumCommitmentUnit
nthProductFree
priceCents
priceExcludingTaxCents
priceWithSymbol
productType
shopifyProductId
shopifyVariantId
sku
slug
supplier
taxAmountCents
taxPercentage
title
}
}
}
`,
variables: {
after: null,
first: 10
}
})
const requestOptions = {
method: 'POST',
headers,
body: graphql,
};
const response = await fetch("https://portal.firmhouse.com/graphql", requestOptions)
const data = await response.json()
const {totalCount, nodes: products, pageInfo} = data.data.products
Fetching Plans
If your project's subscription type is Product as Service, you can use the plans query. This query gives you all the information about the plans you have set up in the Firmhouse Portal.
Creating a cart
To start ordering products, you need to create a cart with createCart mutation.
Adding products to the cart
Once the cart is created, you can start adding items to it with createOrderedProduct mutation. Note that we're using the subscriptionToken value in X-Subscription-Token header.
You can use the orderedProduct variable to display notifications about added products. To show cart information, such as all ordered products, total price, payment terms, etc., you can use the subscription variable.
Removing products from the cart
To let users remove products from their cart, you can use the destroyOrderedProduct mutation.
The fields retrieved in this query are the same as those in createOrderedProduct. Therefore you can use the same logic to handle the response that updates the Cart UI or notifications.
Updating the quantities of products in the cart
To allow users to change the quantities of products in their shopping cart, you can use the updateOrderedProductQuantity mutation.
The fields retrieved in this query are the same as those in createOrderedProduct. Therefore you can use the same logic to handle the response that updates the Cart UI or notifications.
Updating the subscription plan
You can change the active subscription plan with updatePlan mutation.
Fetching current cart information
You can use getSubscription query to get the current state of the cart. It's helpful when customers return to the application after leaving. You can use this query to retrieve cart information using the subscription token that you saved in a cookie or local storage. It's also useful for fetching details when they navigate to checkout page.
You can see the list of available fields for a subscription here.
Updating address details
To update the subscription details, you can use the updateAddressDetails mutation. This is helpful for checkout pages where users can enter their payment, invoice, and shipping information.
If there are any validation errors, you can find them in errors variable. If there is an error in request, you can find them in requestErrors.
Generating payment links
After you have added the products to your cart and filled in the required information using the updateAddressDetails mutation, you can use the createSubscriptionFromCart mutation to create a payment link (paymentUrl). You can then direct the user to the payment provider with that link to complete the payment process. If the payment is successful, the user will be redirected to the returnUrl that you provided as parameter for the mutation.
If any required fields are missing, the paymentUrl will be null and you can check which fields are missing in the errors variable.
If you haven't added a payment method to your Firmhouse project, this request will fail with a 500 error.
Last updated
Was this helpful?