Queries Library
Queries LibraryCreate thousands of discount codes for AppSumo in FluentCart

Create thousands of discount codes for AppSumo in FluentCart

This query connects to the FluentCart REST 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 username and application password to connect to the FluentCart REST API, via variables $wpUsername and $wpApplicationPassword
  • The FluentCart store's domain, via variable $fluentCartDomain
  • The product variation(s) to be redeemed by that code, via variable $productVariationIDs

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 FluentCart dashboard.

Collect all the newly-created discount codes by providing a $postId, then all codes will be appended at the end of that post.

# Export FluentCart API config and build mutation inputs for creating coupons.
# FluentCart REST API: { fluentCartDomain }/wp-json/fluent-cart/v2
# Auth: WordPress Application Passwords (Basic auth: username + application_password)
# Create Coupon: POST /coupons — https://dev.fluentcart.com/restapi/operations/coupons/create-coupon
 
query ExportFluentCartAPIData(
  $fluentCartDomain: String!,
  $fluentCartBaseURL: String! = "wp-json/fluent-cart/v2",
  $postId: ID,
) {
  fluentCartCouponsURL: _sprintf(
    string: "%s/%s/coupons",
    values: [$fluentCartDomain, $fluentCartBaseURL]
  )
    @export(as: "fluentCartCouponsURL")
    @remove
 
  hasPostId: _notEmpty(value: $postId)
    @export(as: "hasPostId")
}
 
query CreateMutationInputs(
  $wpUsername: String!,
  $wpApplicationPassword: String!,
  $discountNamePrefix: String! = "AppSumo campaign",
  $discountNotes: String! = "",
  $codePrefix: String! = "",
  $numberCodes: Int! = 100,
  $codeLength: Int! = 16,
  $firstRecordNumber: Int! = 1,
  $productVariationIDs: [ID!],
)
  @depends(on: "ExportFluentCartAPIData")
{
  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: $fluentCartCouponsURL,
            method: POST,
            options: {
              auth: {
                username: $wpUsername,
                password: $wpApplicationPassword,
              },
              headers: [
                {
                  name: "Content-Type",
                  value: "application/json",
                },
              ],
              json: {
                title: $discountName,
                code: $discountCode,
                type: "percentage",
                amount: 100,
                status: "active",
                stackable: "no",
                show_on_checkout: "no",
                priority: 0,
                notes: $discountNotes,
                conditions: {
                  max_uses: 1,
                  included_products: $productVariationIDs
                }
              }
            }
          }
        },
        setResultInResponse: true
      )
    @export(as: "mutationInputs")
    @remove
}
 
query CreateCouponsInFluentCart
  @depends(on: "CreateMutationInputs")
{
  createCouponsInFluentCart: _sendJSONObjectItemHTTPRequests(inputs: $mutationInputs)
    @underEachArrayItem
      @underJSONObjectProperty(by: { path: "data.code" })
        @export(as: "discountCodes")
}
 
query PrintDiscountCodesFromFluentCart
  @depends(on: "CreateCouponsInFluentCart")
{
  discountCodes: _echo(value: $discountCodes)
}
 
query GetPostWithDiscountCodes($postId: ID)
  @depends(on: "CreateCouponsInFluentCart")
  @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
    }
  }
}

...passing the following variables:

{
  "fluentCartDomain": "{ fluentCartDomain }",
  "wpUsername": "{ username }",
  "wpApplicationPassword": "{ appPassword }",
  "codeLength": 24,
  "codePrefix": "APS2V1T1",
  "discountNamePrefix": "AppSumo campaign@2",
  "postId": "{ postId }",
  "discountNotes": "AppSumo campaign #2, starting on 26/03/2026",
  "productVariationIDs": [ "{ productVariationID }" ]
}