Create a BXGY discount for a specific market
Description
Create a [BXGY discount](https://help.shopify.com/manual/discounts/discount-types/buy-x-get-y) that's applied when customers enter a code.
This example shows how to create a code discount that offers a 20% discount on up to two featured snowboards when they buy three featured snowboards, applying only to buyers in a specific market.
Query
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
Variables
{
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
}
cURL
curl -X POST \
https://your-development-store.myshopify.com/admin/api/unstable/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) { discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBxgy { title codes(first: 10) { nodes { code } } startsAt context { ... on DiscountMarkets { markets(first: 1) { nodes { id } } } } customerGets { value { ... on DiscountOnQuantity { effect { ... on DiscountPercentage { percentage } } quantity { quantity } } } items { ...collectionsFragment } } } } } userErrors { field code message } } } fragment collectionsFragment on DiscountCollections { collections(first: 10) { nodes { id title } } }",
"variables": {
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
}
}'
React Router
import { authenticate } from "../shopify.server";
export const loader = async ({request}) => {
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
{
variables: {
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
},
},
);
const json = await response.json();
return json.data;
}
Ruby
session = ShopifyAPI::Auth::Session.new(
shop: "your-development-store.myshopify.com",
access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
session: session
)
query = <<~QUERY
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
QUERY
variables = {
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
}
response = client.query(query: query, variables: variables)
Node.js
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
"variables": {
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
},
},
});
Shopify CLI
shopify app execute \
--query \
'mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}' \
--variables \
'{
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
}'
Direct API Access
const response = await fetch('shopify:admin/api/unstable/graphql.json', {
method: 'POST',
body: JSON.stringify({
query: `
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
context {
... on DiscountMarkets {
markets(first: 1) {
nodes {
id
}
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
`,
variables: {
"bxgyCodeDiscount": {
"code": "MARKETSALE",
"title": "MARKETSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"markets": {
"add": [
"gid://shopify/Market/128989799"
]
}
},
"startsAt": "2026-04-29T02:45:57-04:00",
"usesPerOrderLimit": 3
}
},
}),
});
const { data } = await response.json();
console.log(data);
Response
{
"discountCodeBxgyCreate": {
"codeDiscountNode": {
"codeDiscount": {
"title": "MARKETSALE",
"codes": {
"nodes": [
{
"code": "MARKETSALE"
}
]
},
"startsAt": "2026-04-29T06:45:57Z",
"context": {
"markets": {
"nodes": [
{
"id": "gid://shopify/Market/128989799"
}
]
}
},
"customerGets": {
"value": {
"effect": {
"percentage": 0.2
},
"quantity": {
"quantity": "2"
}
},
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1007901140",
"title": "Featured items"
}
]
}
}
}
}
},
"userErrors": []
}
}
Create a BXGY discount with a customer segment
Description
Create a [BXGY discount](https://help.shopify.com/manual/discounts/discount-types/buy-x-get-y) that's applied when customers enter a code.
This example shows how to create a code discount that offers a 20% discount on up to two featured snowboards to a specific customer segment when they buy three featured snowboards.
Query
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
Variables
{
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
}
cURL
curl -X POST \
https://your-development-store.myshopify.com/admin/api/unstable/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) { discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBxgy { title codes(first: 10) { nodes { code } } startsAt endsAt customerBuys { items { ...collectionsFragment } value { ... on DiscountQuantity { quantity } } } customerGets { value { ... on DiscountOnQuantity { effect { ... on DiscountPercentage { percentage } } quantity { quantity } } } items { ...collectionsFragment } } context { ... on DiscountCustomerSegments { segments { id } } } appliesOncePerCustomer } } } userErrors { field code message } } } fragment collectionsFragment on DiscountCollections { collections(first: 10) { nodes { id title } } }",
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
}
}'
React Router
import { authenticate } from "../shopify.server";
export const loader = async ({request}) => {
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
{
variables: {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
},
},
);
const json = await response.json();
return json.data;
}
Ruby
session = ShopifyAPI::Auth::Session.new(
shop: "your-development-store.myshopify.com",
access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
session: session
)
query = <<~QUERY
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
QUERY
variables = {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
}
response = client.query(query: query, variables: variables)
Node.js
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
},
},
});
Shopify CLI
shopify app execute \
--query \
'mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}' \
--variables \
'{
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
}'
Direct API Access
const response = await fetch('shopify:admin/api/unstable/graphql.json', {
method: 'POST',
body: JSON.stringify({
query: `
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
context {
... on DiscountCustomerSegments {
segments {
id
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
`,
variables: {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"title": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"context": {
"customerSegments": {
"add": [
"gid://shopify/Segment/210588551"
]
}
},
"endsAt": "2026-05-04T02:46:03-04:00",
"startsAt": "2026-04-29T02:46:03-04:00",
"usesPerOrderLimit": 3
}
},
}),
});
const { data } = await response.json();
console.log(data);
Response
{
"discountCodeBxgyCreate": {
"codeDiscountNode": {
"codeDiscount": {
"title": "SUMMERSALE",
"codes": {
"nodes": [
{
"code": "SUMMERSALE"
}
]
},
"startsAt": "2026-04-29T06:46:03Z",
"endsAt": "2026-05-04T06:46:03Z",
"customerBuys": {
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1007901140",
"title": "Featured items"
}
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"value": {
"effect": {
"percentage": 0.2
},
"quantity": {
"quantity": "2"
}
},
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1007901140",
"title": "Featured items"
}
]
}
}
},
"context": {
"segments": [
{
"id": "gid://shopify/Segment/210588551"
}
]
},
"appliesOncePerCustomer": false
}
},
"userErrors": []
}
}
Create a basic BXGY discount
Description
Create a [BXGY discount](https://help.shopify.com/manual/discounts/discount-types/buy-x-get-y) that's applied when customers enter a code.
This example shows how to create a discount that offers a 20% discount on up to two snowboards that are on sale
to all customers when they buy three featured snowboards. The code discount is valid during
the summer months and can be used a maximum of three times per order.
Query
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
Variables
{
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}
cURL
curl -X POST \
https://your-development-store.myshopify.com/admin/api/unstable/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) { discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBxgy { title codes(first: 10) { nodes { code } } startsAt endsAt customerBuys { items { ...collectionsFragment } value { ... on DiscountQuantity { quantity } } } customerGets { appliesOnOneTimePurchase appliesOnSubscription value { ... on DiscountOnQuantity { effect { ... on DiscountPercentage { percentage } } quantity { quantity } } } items { ...collectionsFragment } } customerSelection { ... on DiscountCustomerAll { allCustomers } } appliesOncePerCustomer usesPerOrderLimit } } } userErrors { field code message } } } fragment collectionsFragment on DiscountCollections { collections(first: 10) { nodes { id title } } }",
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}
}'
React Router
import { authenticate } from "../shopify.server";
export const loader = async ({request}) => {
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
{
variables: {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
},
},
);
const json = await response.json();
return json.data;
}
Ruby
session = ShopifyAPI::Auth::Session.new(
shop: "your-development-store.myshopify.com",
access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
session: session
)
query = <<~QUERY
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
QUERY
variables = {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}
response = client.query(query: query, variables: variables)
Node.js
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
},
},
});
Shopify CLI
shopify app execute \
--query \
'mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}' \
--variables \
'{
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}'
Direct API Access
const response = await fetch('shopify:admin/api/unstable/graphql.json', {
method: 'POST',
body: JSON.stringify({
query: `
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
`,
variables: {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2025-07-29T16:17:12-04:00",
"startsAt": "2025-07-24T16:17:12-04:00",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
},
}),
});
const { data } = await response.json();
console.log(data);
Response
{
"discountCodeBxgyCreate": {
"codeDiscountNode": {
"codeDiscount": {
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"codes": {
"nodes": [
{
"code": "SUMMERSALE"
}
]
},
"startsAt": "2025-07-24T20:17:12Z",
"endsAt": "2025-07-29T20:17:12Z",
"customerBuys": {
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1007901140",
"title": "Featured items"
}
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"appliesOnOneTimePurchase": true,
"appliesOnSubscription": false,
"value": {
"effect": {
"percentage": 0.2
},
"quantity": {
"quantity": "2"
}
},
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/711838235",
"title": "On Sale!"
}
]
}
}
},
"customerSelection": {
"allCustomers": true
},
"appliesOncePerCustomer": false,
"usesPerOrderLimit": 3
}
},
"userErrors": []
}
}