Skip to main content

Fetching data

The Shop Minis GraphQL API allows you to fetch data for shops and products to display within your Shop Mini.

Fetching data using the SDK

The SDK provides a useMinisQuery hook for querying the API from within your Shop Mini:

import {useMinisQuery} from '@shopify/shop-minis-platform-sdk'
import ExampleMiniProductQuery from './ExampleMiniProductQuery.graphql'

const Example = () => {
const {loading, error, data} = useMinisQuery(ExampleMiniProductQuery, {
variables: {
shopId: 'gid://shopify/Shop/68822335510',
productId: 'gid://shopify/Product/7982542651414',
},
})
const shopName = data?.shop?.name

return (
<>
{shopName ? <Text variant="headerNormal">{shopName}</Text> : null}
{/*…*/}
</>
)
}

Run the following command to generate .graphql.d.ts files for each of your .graphql files:

npx shop-minis generate-graphql-types

The command also has a --watch flag for rapid query development.

npx shop-minis generate-graphql-types --watch

Exploring the API

Requests from within your Shop Mini using the useMinisQuery hook are automatically authenticated as the current Shop user. However, during development, you may want to explore the capabilities provided by the Shop Minis API by querying it directly.

You can explore the API in sandbox mode by setting the X-Shop-Mini-Sandbox header:

curl -X POST \
https://server.shop.app/minis/api/alpha/graphql.json \
-H 'Content-Type: application/graphql' \
-H 'X-Shop-Mini-Sandbox: 1' \
-d '{your_query}'

Sandbox mode is limited to a single shop with test products. The following example fetches a test product:

query {
shop(id: "gid://shopify/Shop/68822335510") {
product(id: "gid://shopify/Product/7982542651414") {
title
featuredImage {
url
}
}
}
}

Reference

See the Shop Minis GraphQL API schema documentation for reference.