Queries LibraryCreate thousands of redemption codes for AppSumo in LemonSqueezy
Create thousands of redemption codes for AppSumo in LemonSqueezy
This query connects to the LemonSqueezy API and creates a hundred 100% discount codes at one go.
Execute this query multiple times to create the 10.000 codes required to run an AppSumo campaign.
You must provide:
- The access token to connect to the Lemon Squeezy API, via variable
$lemonSqueezyAccessToken
- The Lemon Squeezy store, via variable
$storeID
- The product(s) to be redeemed by that code, via variable
$variantIDs
The generated discount code will be a random string. You can prepend a string to the code (via variable $codePrefix
), indicate how long the code will be (via variable $codeLength
), and customize the name of the discount code (via variables $discountNamePrefix
and $firstRecordNumber
) to search for it in the Lemon Squeezy dashboard.
Collect all the newly-created discount codes by providing a $postId
, then all codes will be appended at the end of that post.
query ExportLemonSqueezyAPIData(
$lemonSqueezyAccessToken: String!,
$variantIDs: [String!]!,
$postId: ID,
) {
bearerToken: _sprintf(
string: "Bearer %s",
values: [$lemonSqueezyAccessToken]
)
@remove
lemonSqueezyAPIHeaders: _echo(value: [
{
name: "Accept",
value: "application/vnd.api+json"
},
{
name: "Content-Type",
value: "application/vnd.api+json"
},
{
name: "Authorization",
value: $__bearerToken
}
])
@export(as: "lemonSqueezyAPIHeaders")
@remove
variants: _echo(value: $variantIDs)
@underEachArrayItem(
passValueOnwardsAs: "variantID"
)
@applyField(
name: "_echo",
arguments: {
value: {
type: "variants",
id: $variantID,
}
}
setResultInResponse: true
)
@export(as: "variants")
hasPostId: _notEmpty(value: $postId)
@export(as: "hasPostId")
}
query CreateMutationInputs(
$storeID: String!,
$discountNamePrefix: String! = "AppSumo campaign",
$codePrefix: String! = "",
$numberCodes: Int! = 100,
$codeLength: Int! = 16,
$firstRecordNumber: Int! = 1,
$isTestMode: Boolean! = false,
)
@depends(on: "ExportLemonSqueezyAPIData")
{
mutationInputs: _arrayPad(array: [], length: $numberCodes, value: null)
@underEachArrayItem(
passIndexOnwardsAs: "key"
affectDirectivesUnderPos: [1, 2, 3, 4, 5]
)
@applyField(
name: "_generateRandomString",
arguments: {
length: $codeLength,
characters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
},
passOnwardsAs: "randomCode"
)
@applyField(
name: "_strAppend",
arguments: {
after: $codePrefix,
append: $randomCode,
},
passOnwardsAs: "discountCode"
)
@applyField(
name: "_intAdd",
arguments: {
add: $key,
to: $firstRecordNumber,
},
passOnwardsAs: "recordNumber"
)
@applyField(
name: "_sprintf",
arguments: {
string: "%s #%s",
values: [$discountNamePrefix, $recordNumber],
},
passOnwardsAs: "discountName"
)
@applyField(
name: "_echo",
arguments: {
value: {
url: "https://api.lemonsqueezy.com/v1/discounts",
method: POST,
options: {
headers: $lemonSqueezyAPIHeaders
json: {
data: {
type: "discounts",
attributes: {
name: $discountName,
code: $discountCode,
amount: 100,
amount_type: "percent",
is_limited_to_products: true,
is_limited_redemptions: true,
max_redemptions: 1,
test_mode: $isTestMode,
},
relationships: {
store: {
data: {
type: "stores",
id: $storeID,
}
},
variants: {
data: $variants
}
}
}
}
}
}
},
setResultInResponse: true
)
@export(as: "mutationInputs")
@remove
}
query CreateDiscountCodesInLemonSqueezy
@depends(on: "CreateMutationInputs")
{
createDiscountCodesInLemonSqueezy: _sendJSONObjectItemHTTPRequests(inputs: $mutationInputs)
@underEachArrayItem
@underJSONObjectProperty(by: { path: "data.attributes.code" })
@export(as: "discountCodes")
}
query PrintDiscountCodesFromLemonSqueezy
@depends(on: "CreateDiscountCodesInLemonSqueezy")
{
discountCodes: _echo(value: $discountCodes)
}
query GetPostWithDiscountCodes($postId: ID)
@depends(on: "CreateDiscountCodesInLemonSqueezy")
@include(if: $hasPostId)
{
post(by: { id: $postId }, status: any) {
title
postContent: rawContent
discountCodesAsContent: _arrayJoin(
array: $discountCodes,
separator: "\n"
)
updatedPostContent: _sprintf(
string: "%s\n%s"
values: [$__postContent, $__discountCodesAsContent]
)
@export(as: "updatedPostContent")
}
}
mutation UpdatePostWithDiscountCodes($postId: ID)
@depends(on: "GetPostWithDiscountCodes")
@include(if: $hasPostId)
{
updatePost(input: {
id: $postId,
contentAs: { html: $updatedPostContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
title
rawContent
}
}
}