Querying data with the Storefront API
The Storefront API lets you build custom storefronts with any language and technology. It provides unauthenticated access using GraphQL to products, collections, customers, carts, checkouts, and other store resources that you can use to build custom purchasing experiences.
What you'll learn
Anchor link to section titled "What you'll learn"After you've finished this tutorial, you'll have accomplished the following:
- Requested unauthenticated Storefront API access scopes for your app
- Generated a Storefront API access token
Queried products and collections
Requirements
Anchor link to section titled "Requirements"You've created a public or custom app with Storefront API access enabled in the app setup. If you've created a public app, then you need to turn your app into a sales channel.
You've created products, product variants, and collections in your store.
Step 1: Request access scopes
Anchor link to section titled "Step 1: Request access scopes"You can request unauthenticated Storefront API access scopes during OAuth, or during authentication in the Shopify admin.
The following example URL shows a request for unauthenticated scopes during OAuth:
Step 2: Generate a Storefront API access token
Anchor link to section titled "Step 2: Generate a Storefront API access token"You can generate a Storefront API access token using the StorefrontAccessToken
REST resource or the storefrontAccessTokenCreate
GraphQL mutation.
For custom apps created in the Shopify admin, the Storefront API access token is available on the app credentials page in the Shopify admin after the app is installed.
Include the Storefront API access token in the request header for each of your queries to the Storefront API:
Step 3: Make queries
Anchor link to section titled "Step 3: Make queries"After you've requested access scopes for your app and generated a Storefront API access token, you can make queries using the Storefront API.
Query products
Anchor link to section titled "Query products"To query a list of products, you can query products
. The products
field uses an argument (for example, first
) to specify the number of results to query.
The following example shows how to query the IDs of the first 5 products in your store. For more information on selecting which set of results to query, refer to Paginating results with GraphQL.
Query a single product
Anchor link to section titled "Query a single product"Products are identified by a globally unique ID, which can be used to query for information. To query a single product, you can use the product
field at the query root and provide the product ID.
Query product variants
Anchor link to section titled "Query product variants"A product variant represents a different version of a product, such as differing sizes or differing colors. You can query the variants associated with a product by querying variants
on the Product
type.
Query product media
Anchor link to section titled "Query product media"You can use the Storefront API to query a product's media and display it on a storefront.
Specify the media
field on the Product
type to query for a product's media. Then use a fragment to specify the fields that you want to return for each possible media type.
Query collections
Anchor link to section titled "Query collections"A collection represents a grouping of products that a store owner can create to organize them or make their stores easier to browse. For example, a merchant might create a collection for a specific type of product that they sell, such as footwear.
Merchants can create collections by selecting products individually or by defining rules that automatically determine whether products are included.
The following example shows how to query for collections and the products that belong to those collections.
- Filter products in a collection with the Storefront API.
- Manage a cart with the Storefront API.
- Create and complete a checkout with the Storefront API.
- Learn about the different tools that you can use to create unique buying experiences anywhere your customers are, including websites, apps, and video games.