get-collections
Retrieve Shopify store collections with optional filters for name and result limits to manage product groupings effectively.
Instructions
Get all collections
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of collections to return | |
| name | No | Filter collections by name |
Implementation Reference
- src/index.ts:484-510 (registration)Tool registration for 'get-collections', including inline schema (limit, name params) and handler that calls ShopifyClient.loadCollectionsserver.tool( "get-collections", "Get all collections", { limit: z .number() .optional() .default(10) .describe("Maximum number of collections to return"), name: z.string().optional().describe("Filter collections by name"), }, async ({ limit, name }) => { const client = new ShopifyClient(); try { const collections = await client.loadCollections( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, { limit, name } ); return { content: [{ type: "text", text: JSON.stringify(collections, null, 2) }], }; } catch (error) { return handleError("Failed to retrieve collections", error); } } );
- Core handler logic for loading collections (both custom and smart) via Shopify REST API, handling pagination with next cursorasync loadCollections( accessToken: string, shop: string, queryParams: ShopifyCollectionsQueryParams, next?: string ): Promise<LoadCollectionsResponse> { const nextList = next?.split(","); const customNext = nextList?.[0]; const smartNext = nextList?.[1]; let customCollections: ShopifyCollection[] = []; let customCollectionsNextPage; let smartCollections: ShopifyCollection[] = []; let smartCollectionsNextPage; if (customNext !== "undefined") { const customRes = await this.shopifyHTTPRequest<ShopifyCustomCollectionsResponse>({ method: "GET", url: `https://${shop}/admin/api/${this.SHOPIFY_API_VERSION}/custom_collections.json`, accessToken, params: { limit: queryParams.limit, page_info: customNext, title: customNext ? undefined : queryParams.name, since_id: customNext ? undefined : queryParams.sinceId, }, }); customCollections = customRes.data?.custom_collections || []; customCollectionsNextPage = ShopifyClient.getShopifyOrdersNextPage( customRes.headers?.get("link") ); } if (smartNext !== "undefined") { const smartRes = await this.shopifyHTTPRequest<ShopifySmartCollectionsResponse>({ method: "GET", url: `https://${shop}/admin/api/${this.SHOPIFY_API_VERSION}/smart_collections.json`, accessToken, params: { limit: queryParams.limit, page_info: smartNext, title: smartNext ? undefined : queryParams.name, since_id: smartNext ? undefined : queryParams.sinceId, }, }); smartCollections = smartRes.data?.smart_collections || []; smartCollectionsNextPage = ShopifyClient.getShopifyOrdersNextPage( smartRes.headers?.get("link") ); } const collections = [...customCollections, ...smartCollections]; if (customCollectionsNextPage || smartCollectionsNextPage) { next = `${customCollectionsNextPage},${smartCollectionsNextPage}`; } else { next = undefined; } return { collections, next }; }
- TypeScript type definitions for collections query parameters, collection data, API responses, and LoadCollectionsResponse used by the tool handlerexport type ShopifyCollectionsQueryParams = { sinceId?: string; // Retrieve all orders after the specified ID name?: string; limit: number; }; export type ShopifyCollection = { id: number; handle: string; title: string; updated_at: string; body_html: Nullable<string>; published_at: string; sort_order: string; template_suffix?: Nullable<string>; published_scope: string; image?: { src: string; alt: string; }; }; export type ShopifySmartCollectionsResponse = { smart_collections: ShopifyCollection[]; }; export type ShopifyCustomCollectionsResponse = { custom_collections: ShopifyCollection[]; }; export type LoadCollectionsResponse = { collections: ShopifyCollection[]; next?: string; };
- Interface definition for ShopifyClientPort.loadCollections method signature used by the toolloadCollections( accessToken: string, myshopifyDomain: string, queryParams: ShopifyQueryParams, next?: string ): Promise<LoadCollectionsResponse>;