Descuentos y Promociones

Crea y gestiona códigos de descuento y campañas promocionales.

Descripción General

La API de Discounts te permite crear, validar y gestionar códigos de descuento. Construye campañas promocionales, recompensas de lealtad o sistemas de descuento automatizados.

Listar Descuentos

curl -X GET https://api.whatalo.com/v1/discounts \
  -H "X-API-Key: wk_live_your_key_here"

Scope requerido: read:discounts

Crear un Descuento

curl -X POST https://api.whatalo.com/v1/discounts \
  -H "X-API-Key: wk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "SAVE20",
    "type": "percentage",
    "value": 20,
    "min_order_amount": 5000,
    "max_uses": 100,
    "starts_at": "2026-03-15T00:00:00Z",
    "expires_at": "2026-04-15T23:59:59Z"
  }'

Scope requerido: write:discounts

Tipos de descuento: percentage | fixed_amount

Actualizar un Descuento

curl -X PATCH https://api.whatalo.com/v1/discounts/disc_abc123 \
  -H "X-API-Key: wk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "max_uses": 200
  }'

Eliminar un Descuento

curl -X DELETE https://api.whatalo.com/v1/discounts/disc_abc123 \
  -H "X-API-Key: wk_live_your_key_here"

Validar un Descuento

Verifica si un código de descuento es válido antes de aplicarlo:

curl -X POST https://api.whatalo.com/v1/discounts/validate \
  -H "X-API-Key: wk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "SAVE20",
    "order_total": 7500
  }'

Respuesta (válido):

{
  "data": {
    "valid": true,
    "discount_amount": 1500
  }
}

Respuesta (inválido):

{
  "data": {
    "valid": false,
    "discount_amount": 0,
    "reason": "Minimum order amount not reached"
  }
}

Patrones Comunes

Venta Relámpago por Tiempo Limitado

async function createFlashSale(hours, discountPercent) {
  const now = new Date();
  const expires = new Date(now.getTime() + hours * 60 * 60 * 1000);

  const res = await fetch('https://api.whatalo.com/v1/discounts', {
    method: 'POST',
    headers: {
      'X-API-Key': API_KEY,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      code: `FLASH${discountPercent}`,
      type: 'percentage',
      value: discountPercent,
      starts_at: now.toISOString(),
      expires_at: expires.toISOString(),
      max_uses: 50
    })
  });

  if (!res.ok) {
    throw new Error(`Failed to create flash sale: ${res.status}`);
  }

  return res.json();
}

Recompensa de Lealtad de Un Solo Uso

async function createLoyaltyCode(customerId, rewardAmount) {
  const code = `LOYALTY-${customerId}-${Date.now()}`;

  const res = await fetch('https://api.whatalo.com/v1/discounts', {
    method: 'POST',
    headers: {
      'X-API-Key': API_KEY,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      code,
      type: 'fixed_amount',
      value: rewardAmount,
      max_uses: 1
    })
  });

  if (!res.ok) {
    throw new Error(`Failed to create loyalty code: ${res.status}`);
  }

  return res.json();
}

On this page