get-customers
Retrieve Shopify customer data with pagination support, using a limit and next page cursor to manage large datasets efficiently.
Instructions
Get shopify customers with pagination support
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Limit of customers to return | |
| next | No | Next page cursor |
Implementation Reference
- src/index.ts:246-269 (registration)Registration of the 'get-customers' MCP tool, including input schema (limit, next) using Zod and thin handler that instantiates ShopifyClient and calls loadCustomers, returning JSON response.server.tool( "get-customers", "Get shopify customers with pagination support", { limit: z.number().optional().describe("Limit of customers to return"), next: z.string().optional().describe("Next page cursor"), }, async ({ limit, next }) => { const client = new ShopifyClient(); try { const response = await client.loadCustomers( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, limit, next ); return { content: [{ type: "text", text: JSON.stringify(response, null, 2) }], }; } catch (error) { return handleError("Failed to retrieve customers data", error); } } );
- Core handler logic in ShopifyClient.loadCustomers: Makes HTTP GET request to Shopify Admin API /customers.json with pagination (limit, page_info), selects fields id/email/tags, extracts customers and computes next page cursor from Link header.async loadCustomers( accessToken: string, shop: string, limit?: number, next?: string ): Promise<LoadCustomersResponse> { const res = await this.shopifyHTTPRequest<{ customers: any[] }>({ method: "GET", url: `https://${shop}/admin/api/${this.SHOPIFY_API_VERSION}/customers.json`, accessToken, params: { limit: limit ?? 250, page_info: next, fields: ["id", "email", "tags"].join(","), }, }); const customers = res.data.customers; const nextPageInfo = ShopifyClient.getShopifyOrdersNextPage( res.headers.get("link") ); return { customers, next: nextPageInfo }; }
- Type definition for output of loadCustomers (LoadCustomersResponse), including array of ShopifyCustomer and optional next page cursor.export type LoadCustomersResponse = { customers: Array<ShopifyCustomer>; next?: string | undefined; };
- Type definition for ShopifyCustomer, used in LoadCustomersResponse, defining structure of customer data returned by Shopify API.export type ShopifyCustomer = { id?: number; email?: string; first_name?: string; last_name?: string; phone?: string; orders_count?: number; email_marketing_consent?: { state?: "subscribed" | "not_subscribed" | null; opt_in_level?: "single_opt_in" | "confirmed_opt_in" | "unknown" | null; consent_updated_at?: string; }; sms_marketing_consent?: { state?: string; opt_in_level?: string | null; consent_updated_at?: string; consent_collected_from?: string; }; tags?: string; currency?: string; default_address?: { first_name?: string | null; last_name?: string | null; company?: string | null; address1?: string | null; address2?: string | null; city?: string | null; province?: string | null; country?: string | null; zip?: string | null; phone?: string | null; name?: string | null; province_code?: string | null; country_code?: string | null; country_name?: string | null; }; };
- Static helper method ShopifyClient.getShopifyOrdersNextPage extracts the next page_info cursor from Shopify's Link header for pagination.static getShopifyOrdersNextPage(link: Maybe<string>): string | undefined { if (!link) return; if (!link.includes("next")) return; if (link.includes("next") && link.includes("previous")) { return link .split('rel="previous"')[1] .split("page_info=")[1] .split('>; rel="next"')[0]; } return link.split("page_info=")[1].split('>; rel="next"')[0]; }