> ## Documentation Index
> Fetch the complete documentation index at: https://developers.mageloyalty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# redeemFlexible

> Redeem a flexible reward where the customer chooses how much to spend.

Redeem a `flexible` reward, where the customer chooses how much of their balance to spend and the server generates a discount for that amount. This powers "choose your own discount" rewards in points mode, and it is the only redemption path on store-credit shops.

## Usage

```javascript theme={null}
MageSDK.redeemFlexible('reward_abc', 300).then(function(resp) {
  if (resp.success) {
    console.log('Discount code:', resp.data.customerReward.discountCode);
    console.log('Value:', resp.data.discountAmount);
  } else {
    console.log('Error:', resp.error);
  }
});
```

## Parameters

<ParamField body="rewardId" type="string" required>
  The ID of a flexible reward (one whose `redemptionMode` is `"flexible"`). Get reward IDs from [`getShopRewards`](/js-sdk/rewards/get-shop-rewards).
</ParamField>

<ParamField body="pointsToRedeem" type="number" required>
  The amount to spend, in the shop's loyalty unit (points, or minor currency units in store-credit mode). Must fall within the reward's `flexibleConfig.minPoints` and `flexibleConfig.maxPoints`.
</ParamField>

## Response

<ResponseField name="success" type="boolean">
  Whether the redemption was successful.
</ResponseField>

<ResponseField name="data.customerReward" type="object">
  The created reward record.

  <Expandable title="Customer reward properties">
    <ResponseField name="id" type="string">
      The customer reward ID.
    </ResponseField>

    <ResponseField name="discountCode" type="string">
      The generated Shopify discount code.
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="data.discountAmount" type="number">
  The cash value of the generated discount, in major currency units.
</ResponseField>

<ResponseField name="data.pointsRedeemed" type="number">
  The amount deducted from the customer's balance.
</ResponseField>

<ResponseField name="data.updatedPoints" type="number">
  The customer's remaining balance after redemption.
</ResponseField>

## Errors

| Error                   | Cause                                                   |
| ----------------------- | ------------------------------------------------------- |
| `"Customer not found"`  | Customer is not logged in or not enrolled               |
| `"Insufficient points"` | Customer does not have enough balance                   |
| `"Reward not found"`    | The reward ID is invalid, or the reward is not flexible |

## Sample response

```json theme={null}
{
  "success": true,
  "data": {
    "customerReward": {
      "id": "cr_def456",
      "discountCode": "MAGE-FLEX-ABC"
    },
    "discountAmount": 3,
    "pointsRedeemed": 300,
    "updatedPoints": 150
  }
}
```

## Example: Choose-your-own-amount slider

```html theme={null}
<input type="range" id="amount" min="100" max="500" step="100" value="300" />
<span id="preview"></span>
<button id="redeem-flex" data-reward-id="reward_abc">Redeem</button>

<script>
  document.getElementById('redeem-flex').addEventListener('click', function() {
    var rewardId = this.getAttribute('data-reward-id');
    var amount = Number(document.getElementById('amount').value);

    MageSDK.redeemFlexible(rewardId, amount).then(function(resp) {
      if (resp.success) {
        alert('Your discount code: ' + resp.data.customerReward.discountCode);
      } else {
        alert('Error: ' + resp.error);
      }
    });
  });
</script>
```
