You can use the GraphQL Admin API to manage customer segments. This guide shows you how to create, retrieve, update, and delete customer segments.
## Requirements
- The store you're working with has [customer saved searches](/docs/api/admin-graphql/2022-01/queries/customersavedsearches) and [customers](/docs/api/admin-graphql/latest/objects/Customer).
- Your app can make [authenticated requests](/docs/api/admin-graphql#authentication) to the GraphQL Admin API.
- Your app has the `read_customers` and `write_customers` [access scopes](/docs/api/usage/access-scopes). Learn how to [configure your access scopes using Shopify CLI](/docs/apps/build/cli-for-apps/app-configuration).
## Step 1: Create a segment
To create a new segment, use the [`segmentCreate`](/docs/api/admin-graphql/latest/mutations/segmentCreate) mutation, and specify the name and query of the segment as arguments. In the following example, the query specifies customers that have subscribed to email marketing:
## Step 2: Retrieve customer segments
You can retrieve a list of customer segments from a shop by querying [`segments`](/docs/api/admin-graphql/latest/queries/segments). In the following example, the response body returns the first segment, including the segment’s ID and name, and whether the segment is a default that was created by Shopify. The query also includes cursor and pagination information.
The query in the response body includes a combination of conditions on facts about the segment. In this case, the segment query specifies customers that have subscribed to email marketing.
### Query an individual segment
To query an individual segment, query the [`segment`](/docs/api/admin-graphql/latest/queries/segment) object and pass the segment ID as an argument:
### Query a list of segment members
To retrieve a list of members associated with an individual segment, use the [`customerSegmentMembers`](/docs/api/admin-graphql/latest/queries/customerSegmentMembers) query, and pass in the segment attribute and conditions. You can query a maximum of 250 segments. The maximum value that `first` and `last` arguments accept is `250`.
The following example shows how to retrieve a list of segment members that aren't subscribed to email communications. The first member of the segment is returned, including their customer ID, first name, last name, and default email address. Additional attribute statistics are also returned, including the member's total number of orders from the store and the average amount spent.
As of version 2023-01 of the GraphQL Admin API, the most complex queries might be processed asynchronously. You can [migrate your app to support async queries](/docs/apps/build/marketing-analytics/customer-segments/migrate-to-async-queries).
## Step 3: Retrieve segment filters
You can use the [`segmentFilters`](/docs/api/admin-graphql/latest/queries/segmentFilters) query to retrieve a list of filters [(attributes)](/docs/api/shopifyql/segment-query-language-reference#attributes) added to segment queries. In the following example, the first two segment filters are returned, including a human-readable, localized name for the filter and the filter name in ShopifyQL.
### Query segment filter suggestions
You can use the [`segmentFilterSuggestions`](/docs/api/admin-graphql/latest/queries/segmentFilterSuggestions) query to retrieve a list of filter suggestions associated with a segment:
### Query segment value suggestions
You can use the [`segmentValueSuggestions`](/docs/api/admin-graphql/latest/queries/segmentValueSuggestions) query to retrieve a list of value suggestions associated with a segment:
## Step 4: Apply a discount to a customer segment
After you've retrieved a list of segments from the store, you can use a [code discount](/docs/apps/build/discounts#related-mutations-and-queries) mutation to specify which `segmentId` you want to apply a discount to.
The following example shows how to use the [`discountCodeBasicCreate`](/docs/api/admin-graphql/latest/mutations/discountcodebasiccreate) mutation to apply a 10%-off all items discount, named "Discount", to two customer segments:
## Step 5: Update a segment
To update an existing segment, use the [`segmentUpdate`](/docs/api/admin-graphql/latest/mutations/segmentUpdate) mutation, and specify the `id` of the `segment` you that you want to update:
## Step 6 (Optional): Delete a segment
To permanently remove a segment from a shop, use the [`segmentDelete`](/docs/api/admin-graphql/latest/mutations/segmentDelete) mutation, and specify the `id` of the `segment` that you want to delete:
## Next steps
- Migrate from [saved searches to customer segments](/docs/apps/build/marketing-analytics/customer-segments/migrate-saved-searches).