create Storefront Clientutility
The function creates helpers that enable you to quickly query the Shopify Storefront API.
When used on the server, it is recommended to use the prop. When used on the client, it is recommended to use the
prop or consider using
instead.
- Anchor to input1input1StorefrontClientPropsrequired
- getPrivateTokenHeaders(props?: Partial<Pick<StorefrontClientProps, "contentType">> & Pick<StorefrontClientProps, "privateStorefrontToken"> & { buyerIp?: string; }) => Record<string, string>
Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over
.
By default, it will use the config you passed in when calling
. However, you can override the following settings on each invocation of
:
Note that
defaults to what you configured in
and defaults to
'json'
, but a specific call may require usinggraphql
. When usingon the
body
, use'json'
; otherwise, use'graphql'
.- getPublicTokenHeaders(props?: Partial<Pick<StorefrontClientProps, "contentType">> & Pick<StorefrontClientProps, "publicStorefrontToken">) => Record<string, string>
Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the public token which increases the chance of throttling but also can be exposed to clients. Server-side calls should prefer using
.
By default, it will use the config you passed in when calling
. However, you can override the following settings on each invocation of
:
Note that
defaults to what you configured in
and defaults to
'json'
, but a specific call may require usinggraphql
. When usingon the
body
, use'json'
; otherwise, use'graphql'
.- getShopifyDomain(props?: Partial<Pick<StorefrontClientProps, "storeDomain">>) => string
Creates the fully-qualified URL to your myshopify.com domain.
By default, it will use the config you passed in when calling
. However, you can override the following settings on each invocation of
:
- getStorefrontApiUrl(props?: Partial<Pick<StorefrontClientProps, "storeDomain" | "storefrontApiVersion">>) => string
Creates the fully-qualified URL to your store's GraphQL endpoint.
By default, it will use the config you passed in when calling
. However, you can override the following settings on each invocation of
:
StorefrontClientReturn
StorefrontClientProps
- contentType
Customizes which `"content-type"` header is added when using `getPrivateTokenHeaders()` and `getPublicTokenHeaders()`. When fetching with a `JSON.stringify()`-ed `body`, use `"json"`. When fetching with a `body` that is a plain string, use `"graphql"`. Defaults to `"json"` Can also be customized on a call-by-call basis by passing in `'contentType'` to both `getPrivateTokenHeaders({...})` and `getPublicTokenHeaders({...})`, for example: `getPublicTokenHeaders({contentType: 'graphql'})`
'json' | 'graphql'
- privateStorefrontToken
The Storefront API delegate access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) and [delegate access token](https://shopify.dev/apps/auth/oauth/delegate-access-tokens) documentation for more details.
string
- publicStorefrontToken
The Storefront API access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details.
string
- storeDomain
The host name of the domain (eg: `{shop}.myshopify.com`).
string
- storefrontApiVersion
The Storefront API version. This should almost always be the same as the version Hydrogen React was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details.
string
{
/** The host name of the domain (eg: `{shop}.myshopify.com`). */
storeDomain?: string;
/** The Storefront API delegate access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) and [delegate access token](https://shopify.dev/apps/auth/oauth/delegate-access-tokens) documentation for more details. */
privateStorefrontToken?: string;
/** The Storefront API access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details. */
publicStorefrontToken?: string;
/** The Storefront API version. This should almost always be the same as the version Hydrogen React was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details. */
storefrontApiVersion?: string;
/**
* Customizes which `"content-type"` header is added when using `getPrivateTokenHeaders()` and `getPublicTokenHeaders()`. When fetching with a `JSON.stringify()`-ed `body`, use `"json"`. When fetching with a `body` that is a plain string, use `"graphql"`. Defaults to `"json"`
*
* Can also be customized on a call-by-call basis by passing in `'contentType'` to both `getPrivateTokenHeaders({...})` and `getPublicTokenHeaders({...})`, for example: `getPublicTokenHeaders({contentType: 'graphql'})`
*/
contentType?: 'json' | 'graphql';
}
StorefrontClientReturn
- getPrivateTokenHeaders
Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`. By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`: - `contentType` - `privateStorefrontToken` - `buyerIp` Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.
(props?: Partial<Pick<StorefrontClientProps, "contentType">> & Pick<StorefrontClientProps, "privateStorefrontToken"> & { buyerIp?: string; }) => Record<string, string>
- getPublicTokenHeaders
Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the public token which increases the chance of throttling but also can be exposed to clients. Server-side calls should prefer using `getPublicTokenHeaders()`. By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPublicTokenHeaders({...})`: - `contentType` - `publicStorefrontToken` Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.
(props?: Partial<Pick<StorefrontClientProps, "contentType">> & Pick<StorefrontClientProps, "publicStorefrontToken">) => Record<string, string>
- getShopifyDomain
Creates the fully-qualified URL to your myshopify.com domain. By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getShopifyDomain({...})`: - `storeDomain`
(props?: Partial<Pick<StorefrontClientProps, "storeDomain">>) => string
- getStorefrontApiUrl
Creates the fully-qualified URL to your store's GraphQL endpoint. By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getStorefrontApiUrl({...})`: - `storeDomain` - `storefrontApiVersion`
(props?: Partial<Pick<StorefrontClientProps, "storeDomain" | "storefrontApiVersion">>) => string
{
/**
* Creates the fully-qualified URL to your myshopify.com domain.
*
* By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getShopifyDomain({...})`:
*
* - `storeDomain`
*/
getShopifyDomain: (
props?: Partial<Pick<StorefrontClientProps, 'storeDomain'>>,
) => string;
/**
* Creates the fully-qualified URL to your store's GraphQL endpoint.
*
* By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getStorefrontApiUrl({...})`:
*
* - `storeDomain`
* - `storefrontApiVersion`
*/
getStorefrontApiUrl: (
props?: Partial<
Pick<StorefrontClientProps, 'storeDomain' | 'storefrontApiVersion'>
>,
) => string;
/**
* Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`.
*
* By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`:
*
* - `contentType`
* - `privateStorefrontToken`
* - `buyerIp`
*
* Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.
*/
getPrivateTokenHeaders: (
props?: OverrideTokenHeaderProps &
Pick<StorefrontClientProps, 'privateStorefrontToken'> & {
/**
* The client's IP address. Passing this to the Storefront API when using a server-to-server token will help improve your store's analytics data.
*/
buyerIp?: string;
},
) => Record<string, string>;
/**
* Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the public token which increases the chance of throttling but also can be exposed to clients. Server-side calls should prefer using `getPublicTokenHeaders()`.
*
* By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPublicTokenHeaders({...})`:
*
* - `contentType`
* - `publicStorefrontToken`
*
* Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.
*/
getPublicTokenHeaders: (
props?: OverrideTokenHeaderProps &
Pick<StorefrontClientProps, 'publicStorefrontToken'>,
) => Record<string, string>;
}
Example code
Client Example in NextJS
Examples
Example code
Description
I am the default example
Client Example in NextJS
import {createStorefrontClient} from '@shopify/hydrogen-react'; export const client = createStorefrontClient({ storeDomain: 'https://{store-name}.myshopify.com', storefrontApiVersion: '2025-01', privateStorefrontToken: '{private token for server-side requests}', }); // in another file where you need to make queries, for example in NextJS server-side: // a Storefront API query const GRAPHQL_QUERY = ` query { shop { name } } `; // make the request export async function getServerSideProps() { // Get the Storefront API url const response = await fetch(client.getStorefrontApiUrl(), { body: JSON.stringify({ query: GRAPHQL_QUERY, }), // Generate the headers using the private token. Additionally, you can pass in the buyer's IP address from the request object to help prevent bad actors from overloading your store. headers: client.getPrivateTokenHeaders({buyerIp: '...'}), method: 'POST', }); if (!response.ok) { throw new Error(response.statusText); } const json = await response.json(); return {props: json}; }