๐ง Shopify Client SDK
Welcome to the heart of @nextshopkit/sdk
and @nextshopkit/pro
.
Everything starts with createShopifyClient()
โ a typed, server-safe way to access Shopifyโs Storefront API.
๐ What is the client?โ
The client is your gateway to Shopify. It wraps all of your requests and gives you access to prebuilt methods like:
getProduct()
โ Fetch a single product by handlegetCollection()
โ Fetch multiple products with filters, sorting, and pagination within a collectiongetPolicies()
โ Fetch refund, privacy, terms, and shipping policies (PRO only)
โ๏ธ Initializing the clientโ
You only need to do this once โ usually inside:
lib/NextShopKit/client.ts
When initializing the client, you can optionally enable caching to improve performance:
enableMemoryCache
: stores Shopify responses in memory for quicker reuseenableVercelCache
: integrates with Vercel ISR caching (use withrevalidate
)defaultCacheTtl
: memory cache duration in secondsdefaultRevalidate
: Vercel revalidate time in seconds
These options reduce redundant API calls and improve response times.
- TypeScript
- JavaScript
import {
createShopifyClient,
GetProductOptions,
FetchProductResult,
GetPoliciesOptions,
FetchPoliciesResult,
GetCollectionOptions,
FetchCollectionResult,
} from "@nextshopkit/sdk"; // or "@nextshopkit/pro"
// Cache options can be used both for CORE and PRO tier.
const client = createShopifyClient({
shop: process.env.SHOPIFY_STORE_DOMAIN!,
token: process.env.SHOPIFY_ACCESS_TOKEN!,
apiVersion: "2025-01",
enableMemoryCache: true,
defaultCacheTtl: 300,
enableVercelCache: true,
defaultRevalidate: 60,
});
// The cache option specified here overrides the default
// client-level cache settings and is exclusively available
// in the PRO tier. Note that the CORE tier does not
// support function-specific cache configurations.
export const getProduct = async (
args: GetProductOptions
): Promise<FetchProductResult> =>
client.getProduct(args, {
cacheTtl: 60,
revalidate: 60,
useMemoryCache: true,
useVercelCache: true,
});
export const getCollection = async (
args: GetCollectionOptions
): Promise<FetchCollectionResult> => client.getCollection(args);
export const getPolicies = async (
args: GetPoliciesOptions
): Promise<FetchPoliciesResult> => client.getPolicies(args);
export default client;
import { createShopifyClient } from "@nextshopkit/sdk"; // or "@nextshopkit/pro"
// Cache options can be used both for CORE and PRO tier.
const client = createShopifyClient({
shop: process.env.SHOPIFY_STORE_DOMAIN,
token: process.env.SHOPIFY_ACCESS_TOKEN,
apiVersion: "2025-01",
enableMemoryCache: true,
defaultCacheTtl: 300,
enableVercelCache: true,
defaultRevalidate: 60,
});
// The cache option specified here overrides the default
// client-level cache settings and is exclusively available
// in the PRO tier. Note that the CORE tier does not
// support function-specific cache configurations.
export const getProduct = async (args) =>
client.getProduct(args, {
cacheTtl: 60,
revalidate: 60,
useMemoryCache: true,
useVercelCache: true,
});
export const getCollection = async (args) => client.getCollection(args);
export const getPolicies = async (args) => client.getPolicies(args);
export default client;
๐ Environment Variablesโ
Your .env.local
file must contain:
SHOPIFY_ACCESS_TOKEN=your-access-token
SHOPIFY_STORE_DOMAIN=your-store.myshopify.com
These values are never exposed to the client. Always use the SDK on the server only.
๐งฉ What does the client return?โ
Every SDK method returns the same structure:
const { data, error } = await getProduct({ handle: "solar-kit-9kw" });
if (error || !data) {
// Handle the error gracefully
}
You always get:
data
: the structured result (ornull
if not found)error
: a parsed error object (ornull
if success)
๐ Method Overviewโ
Method | Description | Tier | Link |
---|---|---|---|
getProduct() | Fetch a product by handle | CORE/PRO | See docs โ |
getCollection() | Fetch multiple products with filters & sorting | CORE/PRO | See docs โ |
getPolicies() | Fetch shop policies (refund, privacy, etc.) | PRO | See docs โ |
โ ๏ธ Server-only usage requiredโ
Because it uses secrets from .env
, you must use these functions only:
- Inside
server components
(App Router) - In
"use server"
utilities - In
API routes
orroute handlers
Using it on the client would expose sensitive data.
โ Best practicesโ
- Place the client setup inside
lib/NextShopKit/client.ts
- Create wrappers like
getProduct
,getCollection
, andgetPolicies
so you can mock or extend them later - Handle
error
andnull data
gracefully - Group usage per domain (
lib/NextShopKit/products.ts
,cart.ts
, etc.)
๐งช Need examples?โ
Description
Learn how to initialize the Shopify client using createShopifyClient and access all SDK functions including getProduct, getCollection, and getPolicies.