Anchor to discountCodeBasicCreatediscount
discountCodeBasicCreate
mutation
Requires Apps must have access scope.
Creates an amount off discount that's applied on a cart and at checkout when a customer enters a code. Amount off discounts can be a percentage off or a fixed amount off.
Anchor to Arguments
Arguments
- Anchor to basicCodeDiscountbasic•
Code Discount DiscountCode requiredBasic Input! The input data used to create the discount code.
Was this section helpful?
Anchor to DiscountCodeBasicCreatePayload returnsDiscountCodeBasicCreatePayload returns
- Anchor to codeDiscountNodecode•
Discount Node The discount code that was created.
- Anchor to userErrorsuser•
Errors [DiscountUser non-nullError!]! The list of errors that occurred from executing the mutation.
Was this section helpful?
- Create a discount with a minimum purchase
- Create a limited time discount
- discountCodeBasicCreate reference
Examples
1const { admin } = await authenticate.admin(request);23const response = await admin.graphql(4 `#graphql5 mutation CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {6 discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {7 codeDiscountNode {8 id9 codeDiscount {10 ... on DiscountCodeBasic {11 title12 startsAt13 endsAt14 customerSelection {15 ... on DiscountCustomers {16 customers {17 id18 }19 }20 }21 customerGets {22 value {23 ... on DiscountPercentage {24 percentage25 }26 }27 }28 }29 }30 }31 userErrors {32 field33 message34 }35 }36 }`,37 {38 variables: {39 "basicCodeDiscount": {40 "title": "10% off selected items",41 "code": "10FORYOU",42 "startsAt": "2025-01-01T00:00:00Z",43 "endsAt": "2025-12-31T23:59:59Z",44 "customerSelection": {45 "customers": {46 "add": [47 "gid://shopify/Customer/624407574"48 ]49 }50 },51 "customerGets": {52 "value": {53 "percentage": 0.154 },55 "items": {56 "all": true57 }58 },59 "minimumRequirement": {60 "subtotal": {61 "greaterThanOrEqualToSubtotal": "50.0"62 }63 },64 "usageLimit": 100,65 "appliesOncePerCustomer": true66 }67 },68 },69);7071const data = await response.json();72
mutation CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
title
startsAt
endsAt
customerSelection {
... on DiscountCustomers {
customers {
id
}
}
}
customerGets {
value {
... on DiscountPercentage {
percentage
}
}
}
}
}
}
userErrors {
field
message
}
}
}
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 CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) { discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) { codeDiscountNode { id codeDiscount { ... on DiscountCodeBasic { title startsAt endsAt customerSelection { ... on DiscountCustomers { customers { id } } } customerGets { value { ... on DiscountPercentage { percentage } } } } } } userErrors { field message } } }",
"variables": {
"basicCodeDiscount": {
"title": "10% off selected items",
"code": "10FORYOU",
"startsAt": "2025-01-01T00:00:00Z",
"endsAt": "2025-12-31T23:59:59Z",
"customerSelection": {
"customers": {
"add": [
"gid://shopify/Customer/624407574"
]
}
},
"customerGets": {
"value": {
"percentage": 0.1
},
"items": {
"all": true
}
},
"minimumRequirement": {
"subtotal": {
"greaterThanOrEqualToSubtotal": "50.0"
}
},
"usageLimit": 100,
"appliesOncePerCustomer": true
}
}
}'
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
title
startsAt
endsAt
customerSelection {
... on DiscountCustomers {
customers {
id
}
}
}
customerGets {
value {
... on DiscountPercentage {
percentage
}
}
}
}
}
}
userErrors {
field
message
}
}
}`,
{
variables: {
"basicCodeDiscount": {
"title": "10% off selected items",
"code": "10FORYOU",
"startsAt": "2025-01-01T00:00:00Z",
"endsAt": "2025-12-31T23:59:59Z",
"customerSelection": {
"customers": {
"add": [
"gid://shopify/Customer/624407574"
]
}
},
"customerGets": {
"value": {
"percentage": 0.1
},
"items": {
"all": true
}
},
"minimumRequirement": {
"subtotal": {
"greaterThanOrEqualToSubtotal": "50.0"
}
},
"usageLimit": 100,
"appliesOncePerCustomer": true
}
},
},
);
const data = await response.json();
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
title
startsAt
endsAt
customerSelection {
... on DiscountCustomers {
customers {
id
}
}
}
customerGets {
value {
... on DiscountPercentage {
percentage
}
}
}
}
}
}
userErrors {
field
message
}
}
}`,
"variables": {
"basicCodeDiscount": {
"title": "10% off selected items",
"code": "10FORYOU",
"startsAt": "2025-01-01T00:00:00Z",
"endsAt": "2025-12-31T23:59:59Z",
"customerSelection": {
"customers": {
"add": [
"gid://shopify/Customer/624407574"
]
}
},
"customerGets": {
"value": {
"percentage": 0.1
},
"items": {
"all": true
}
},
"minimumRequirement": {
"subtotal": {
"greaterThanOrEqualToSubtotal": "50.0"
}
},
"usageLimit": 100,
"appliesOncePerCustomer": true
}
},
},
});
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 CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
title
startsAt
endsAt
customerSelection {
... on DiscountCustomers {
customers {
id
}
}
}
customerGets {
value {
... on DiscountPercentage {
percentage
}
}
}
}
}
}
userErrors {
field
message
}
}
}
QUERY
variables = {
"basicCodeDiscount": {
"title": "10% off selected items",
"code": "10FORYOU",
"startsAt": "2025-01-01T00:00:00Z",
"endsAt": "2025-12-31T23:59:59Z",
"customerSelection": {
"customers": {
"add": ["gid://shopify/Customer/624407574"]
}
},
"customerGets": {
"value": {
"percentage": 0.1
},
"items": {
"all": true
}
},
"minimumRequirement": {
"subtotal": {
"greaterThanOrEqualToSubtotal": "50.0"
}
},
"usageLimit": 100,
"appliesOncePerCustomer": true
}
}
response = client.query(query: query, variables: variables)
Input variables
JSON1{2 "basicCodeDiscount": {3 "title": "10% off selected items",4 "code": "10FORYOU",5 "startsAt": "2025-01-01T00:00:00Z",6 "endsAt": "2025-12-31T23:59:59Z",7 "customerSelection": {8 "customers": {9 "add": [10 "gid://shopify/Customer/624407574"11 ]12 }13 },14 "customerGets": {15 "value": {16 "percentage": 0.117 },18 "items": {19 "all": true20 }21 },22 "minimumRequirement": {23 "subtotal": {24 "greaterThanOrEqualToSubtotal": "50.0"25 }26 },27 "usageLimit": 100,28 "appliesOncePerCustomer": true29 }30}
Response
JSON1{2 "discountCodeBasicCreate": {3 "codeDiscountNode": {4 "id": "gid://shopify/DiscountCodeNode/1057371591",5 "codeDiscount": {6 "title": "10% off selected items",7 "startsAt": "2025-01-01T00:00:00Z",8 "endsAt": "2025-12-31T23:59:59Z",9 "customerSelection": {10 "customers": [11 {12 "id": "gid://shopify/Customer/624407574"13 }14 ]15 },16 "customerGets": {17 "value": {18 "percentage": 0.119 }20 }21 }22 },23 "userErrors": []24 }25}