Build a mobile storefront
Build a mobile app that fetches products from your store, creates a cart, and presents checkout. You'll use the Storefront API to fetch products and create a cart, and Checkout Kit to present Shopify's checkout inside your app.
If you already have a checkout URL, then go to Embed Checkout Kit for a quick start.
If you already have a checkout URL, then go to Embed Checkout Kit for a quick start.
Anchor to What you'll learnWhat you'll learn
In this tutorial, you'll:
- Query the Storefront API to fetch products and their variant IDs.
- Build a native product list with images, prices, and an add-to-cart action.
- Get a
checkoutUrlusing thecartCreatemutation or a cart permalink. - Install Checkout Kit and present Shopify checkout inside your app.
- Test the complete purchase flow from browsing to order confirmation.
Requirements
A Shopify development store with at least one product. Select Generate test data during creation so your store has products to work with.
The Headless channel installed in your store with a public access token.
Xcode 14 or later, targeting iOS 13 or later.
Project
Anchor to Fetch productsFetch products
Use the Storefront API to fetch products from your store. Each product includes variant IDs that you'll need when creating a cart.
Anchor to Query products from the Storefront APIQuery products from the Storefront API
Send a GraphQL query to https://{shop}.myshopify.com/api/{api-version}/graphql.json with your public access token in the X-Shopify-Storefront-Access-Token header. Replace {shop} with your store's myshopify.com subdomain.
This query fetches the first 10 products with their titles, images, and variant IDs. Each product's variant includes an id (for example, gid://shopify/ProductVariant/44217898279174) and a price. You'll use the variant id when creating a cart.
If you receive a 401 error, then check that your access token is correct and that you're using a public Storefront API token. The token goes in the X-Shopify-Storefront-Access-Token header, not the Authorization header.
If you receive a 401 error, then check that your access token is correct and that you're using a public Storefront API token. The token goes in the X-Shopify-Storefront-Access-Token header, not the Authorization header.
Anchor to Define response typesDefine response types
Create Decodable structs to parse the GraphQL JSON response. The Storefront API wraps results in edges and node objects, so these types unwrap that structure into Swift types you can use throughout your app.
Anchor to Display products in your UIDisplay products in your UI
Build a product list that renders the fetched products with an Add to Cart button for each product.
Anchor to Build the product listBuild the product list
Create a SwiftUI view that loads products on appear and renders each one with a title, price, image, and Add to Cart button. The view model calls fetchProducts() from the Storefront client and stores the results.
Your implementation depends on your app's design, so treat this as a starting point. When the buyer taps Add to Cart, the handler pulls the variant ID from the selected product and creates a cart.
Anchor to Get a checkout URLGet a checkout URL
You need a checkoutUrl to present to the buyer. You can create a cart through the Storefront API, or build a URL directly from variant IDs using a cart permalink.
Anchor to Create a cart with the Storefront APICreate a cart with the Storefront API
Send a cartCreate mutation with the variant ID as the merchandiseId in a CartInput line item. The response includes a checkoutUrl that you pass to Checkout Kit.
Build a checkoutUrl without the API by using a cart permalink. The format is https://{shop}.myshopify.com/cart/{variant_id}:{quantity}. Cart permalinks require numeric variant IDs (for example, 12345678901234), not the GraphQL format (gid://shopify/ProductVariant/12345678901234).
The cart ID includes a secret key in the format gid://shopify/Cart/{token}?key={secret}. Always store the complete ID including the ?key= parameter. Without it, queries strip buyer data and mutations fail.
The cart ID includes a secret key in the format gid://shopify/Cart/{token}?key={secret}. Always store the complete ID including the ?key= parameter. Without it, queries strip buyer data and mutations fail.
Anchor to Present checkout with Checkout KitPresent checkout with Checkout Kit
Install Checkout Kit and present Shopify checkout inside your app. Your app receives callbacks when checkout completes, fails, or the buyer cancels.
Anchor to Install Checkout KitInstall Checkout Kit
Add the checkout-sheet-kit-swift package to your project using Swift Package Manager, or add it through Xcode by going to File > Add Package Dependencies.
Anchor to Present checkout and handle eventsPresent checkout and handle events
Call present() with your checkoutUrl. Checkout Kit presents the store's checkout, including branding and all configured payment methods.
Implement the CheckoutDelegate protocol to respond when the buyer completes checkout, cancels, encounters an error, or taps an external link.
Request the checkoutUrl when the buyer is ready to check out. Checkout URLs can go stale, especially for authenticated checkouts. If the URL is stale, then query the cart again to get a new one.
Request the checkoutUrl when the buyer is ready to check out. Checkout URLs can go stale, especially for authenticated checkouts. If the URL is stale, then query the cart again to get a new one.
Anchor to Test the complete flowTest the complete flow
Verify the full flow by browsing products, completing a purchase, and checking the order in the Shopify admin.
Anchor to Run the app and complete a test purchaseRun the app and complete a test purchase
Run your app on a simulator, emulator, or device. Browse the product list and confirm that titles, prices, and images load from your store.
Tap Add to Cart on a product, then complete checkout. To process test payments, enable Shopify's Bogus Gateway in your development store by going to Settings > Payments.
After checkout completes, open your Shopify admin and go to Orders. The new order appears with the products you selected.
Troubleshooting
If products don't load, then check that your Storefront API public access token is correct and that you enabled Products and Collections permissions in the Headless channel. If checkout doesn't open, then verify that your checkoutUrl is valid and that the cart hasn't expired.
Anchor to Tutorial complete!Tutorial complete!
You've built a mobile storefront that fetches products, creates a cart, and presents Shopify checkout inside your app. This tutorial uses direct HTTP calls to keep things simple. For production apps, we recommend using Apollo for typed queries, caching, and @defer support. See the sample apps below for production-ready examples.
Anchor to Next stepsNext steps
Sample apps with production-quality UI and Checkout Kit integration.
Accelerated checkoutsAdd Shop Pay and Apple Pay buttons for accelerated checkouts.
Authenticate buyersPrefill checkout with a signed-in buyer's address and payment methods.
Preload checkoutFetch checkout in the background so it's ready when the buyer taps checkout.