API Client

Discounts

Create and manage discount codes using client.discounts — list, get, create, update, and delete discount records.

client.discounts lets your plugin manage discount codes in the merchant's store. This is useful for loyalty plugins, referral systems, or promotional campaign tools.

Required Scopes

OperationRequired Scope
list, getread:discounts
create, update, deletewrite:discounts

HTTP Mapping

MethodHTTP VerbPath
list(params?)GET/discounts
get(id)GET/discounts/:id
create(data)POST/discounts
update(id, data)PATCH/discounts/:id
delete(id)DELETE/discounts/:id

Methods

list

Retrieve a paginated list of all discount codes.

const { data, pagination } = await client.discounts.list();

data.forEach((discount) => {
  console.log(`${discount.code} — ${discount.type}: ${discount.value}`);
});

get

Retrieve a single discount by its ID.

const discount = await client.discounts.get("disc_123");

console.log(discount.code);   // e.g., "SAVE20"
console.log(discount.type);   // "percentage" | "fixed"
console.log(discount.value);  // e.g., 20 (percent) or 10.00 (fixed amount)

create

Create a new discount code.

// Percentage discount
const percentDiscount = await client.discounts.create({
  code: "SAVE20",
  type: "percentage",
  value: 20,
});

// Fixed-amount discount
const fixedDiscount = await client.discounts.create({
  code: "10OFF",
  type: "fixed",
  value: 10.0,
});

console.log(percentDiscount.id);

update

Partially update an existing discount. Only the fields you pass are changed.

await client.discounts.update("disc_123", {
  value: 25, // Increase the discount from 20% to 25%
});

delete

Permanently remove a discount code. Active uses of the code are not affected.

await client.discounts.delete("disc_123");

Loyalty Plugin Example

import { WhataloClient } from "@whatalo/plugin-sdk";

const client = new WhataloClient({ apiKey: process.env.WHATALO_API_KEY! });

/**
 * Generate a unique loyalty discount for a customer after
 * they reach a reward threshold.
 */
async function issueLoyaltyReward(customerId: string): Promise<string> {
  const code = `LOYAL-${customerId.slice(-6).toUpperCase()}`;

  const discount = await client.discounts.create({
    code,
    type: "percentage",
    value: 15,
  });

  return discount.code; // Return the code to display to the customer
}

Error Handling

import { NotFoundError, ValidationError } from "@whatalo/plugin-sdk";

try {
  await client.discounts.create({
    code: "SAVE20", // Duplicate code — store already has this
    type: "percentage",
    value: 20,
  });
} catch (error) {
  if (error instanceof ValidationError) {
    error.fieldErrors.forEach((e) => console.log(`${e.field}: ${e.message}`));
  } else {
    throw error;
  }
}

See Errors & Rate Limits for the full error class reference.

On this page