# Account credits

Source: https://developers.swell.is/backend-api/account-credits

Account credits are a form of currency that is stored on a customer's account. This balance can be awarded through actions such as refunds, or it can additionally be increased by the redemption of gift cards. Customers can apply their account credit balance towards purchases during checkout.

## The account credit model

### Fields

- `id` (objectId): Unique identifier for the credit.
- `amount` (currency, required): Credit amount denominated in currency. Minimum of 0.01.
- `parent_id` (objectId, required): The ID of the parent customer.
- `credit` (Credit memo): Expandable list of the customer’s credit transactions.
- `credit_id` (objectId): ID of the credit applied to the customer's account.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `date_created` (date, auto): Date and time the credit was created.
- `date_updated` (date, auto): Date and time the credit was last updated.
- `giftcard_id` (objectId): ID of the gift card used to make the payment, if applicable.
- `giftcard` (Gift Card): Expandable link to the gift card that generated the credit, if applicable.
- `invoice` (Invoice): Expandable link to the invoice the credit was applied to, if applicable.
- `invoice_id` (objectId): ID of the invoice the credit was applied to, if applicable.
- `number` (string, auto): Unique incremental number assigned automatically each time an account credit is created.
- `parent` (Account): Expandable link to the parent customer.
- `payment` (Payment): Expandable link to the payment.
- `payment_id` (objectId): Unique identifier for the payment.
- `reason` (string): The reason for the refund.
- `reason_message` (string): A brief message describing the reason for the refund.
- `refund` (Refund): Expandable list of refunds that apply to the credit.
- `refund_id` (objectId): ID of the refund the credit was applied to.

### Example request

**The account credit model**

### Example response

```json
{
  "parent_id": "5ffd413300b58e2a4a017831",
  "payment_id": "61e78cbed7e71014da0f1b2c",
  "amount": 100,
  "reason": "promo",
  "reason_message": "Found a bag of gold in a tree stump."
  "currency": "USD",
  "date_created": "2022-01-19T03:59:58.816Z",
  "number": "101082",
  "id": "61e78cbed7e71014da0f1b87"
}
```


## Create an account credit

Create a new customer account credit.

### Arguments

- `amount` (currency, required): Account credit value. Must be greater than zero.
- `parent_id` (objectId, required): Unique identifier for the parent account.
- `id` (objectId): Unique identifier for the account credit.
- `number` (string): Automatically generated field that gets incremented each time an account credit is created.
- `credit` (Account credit): Expandable list of account credits.
- `credit_id` (objectId): ID of the credit applied to the customer's account.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `date_created` (date, auto): Date and time the credit was created.
- `date_updated` (date, auto): Date and time the credit was last updated.
- `giftcard_id` (objectId): ID of the gift card used to make the payment, if applicable.
- `invoice` (Invoice): Expandable link to the invoice the credit was applied to, if applicable.
- `invoice_id` (objectId): ID of the invoice the credit was applied to, if applicable.
- `parent` (Account): Expandable link to the parent account.
- `payment` (Payment): Expandable link to the payment.
- `payment_id` (objectId): Unique identifier for the payment.
- `reason` (string): The reason for the refund.
- `reason_message` (string): A brief message describing the reason for the refund.
- `refund` (Refund): Expandable list of refunds that apply to the credit.
- `refund_id` (objectId): ID of the refund the credit was applied to.

### Example request

`POST /accounts:credits`

**cURL**

```sh
$ curl https://api.swell.store/accounts:credits \
  -u store-id:secret-key \
  -d parent_id=621e53a6213eaf013d1fa6b2 \
  -d amount=17 \
  -d reason=refund
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.post('/accounts:credits', {
  parent_id: '621e53a6213eaf013d1fa6b2',
  amount: 17,
  reason: 'refund'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->post('/accounts:credits', [
  'parent_id' => '621e53a6213eaf013d1fa6b2',
  'amount' => 17,
  'reason' => 'refund'
]);
```

### Example response

```json
{
  "parent_id": "621e53a6213eaf013d1fa6b2",
  "amount": 17,
  "reason": "refund",
  "currency": "USD",
  "date_created": "2022-07-08T20:27:40.199Z",
  "number": "100003",
  "id": "62c8933c5f4ffe00136039f2"
}
```


## Retrieve an account credit

Retrieve an existing account credit using the ID that was returned when created.

### Arguments

- `id` (objectId, required): Unique identifier for the credit.

### Example request

`GET /accounts:credits/:id`

**cURL**

```sh
$ curl https://api.swell.store/accounts:credits/61e78cbed7e71014da0f1b87 \
  -u store-id:secret-key
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/accounts:credits/{id}', {
  id: '61e78cbed7e71014da0f1b87'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/accounts:credits/{id}', [
  'id' => '61e78cbed7e71014da0f1b87'
]);
```

### Example response

```json
{
  "parent_id": "5ffd413300b58e2a4a017831",
  "payment_id": "61e78cbed7e71014da0f1b2c",
  "amount": -19.55,
  "currency": "USD",
  "date_created": "2022-01-19T03:59:58.816Z",
  "number": "101082",
  "id": "61e78cbed7e71014da0f1b87"
}
```


## Update an account credit

Update an existing account credit using the ID that was returned when created.

### Arguments

- `id` (objectId, required): Unique identifier for the credit.
- `amount` (currency, required): Credit amount denominated in currency. Minimum of 0.01.
- `parent_id` (objectId, required): The ID of the parent customer.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `credit_id` (objectId): ID of the credit applied to the customer's account.
- `number` (string, auto): Unique incremental number assigned automatically each time an account credit is created.

### Example request

`PUT /accounts:credits/:id`

**cURL**

```sh
$ curl https://api.swell.store/accounts:credits/62c8933c5f4ffe00136039f2 \
  -u store-id:secret-key \
  -d amount=25 \
  -X PUT
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.put('/accounts:credits/{id}', {
  id: '62c8933c5f4ffe00136039f2',
  amount: 25
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->put('/accounts:credits/{id}', [
  'id' => '62c8933c5f4ffe00136039f2',
  'amount' => 25
]);
```

### Example response

```json
{
  "parent_id": "621e53a6213eaf013d1fa6b2",
  "amount": 25,
  "reason": "refund",
  "currency": "USD",
  "date_created": "2022-07-08T20:27:40.199Z",
  "number": "100003",
  "date_updated": "2022-07-12T16:02:14.965Z",
  "id": "62c8933c5f4ffe00136039f2"
}
```


## List all account credits

Return a list of customer account credits.

### Arguments

- `expand` (string): Expand link fields and child collections by using the expand argument.

  - For example, `expand=account` would return a related customer account if one exists.

  When the field represents a collection, you can specify the query limit.

  - For example, `expand=variants:10` would return up to 10 records of the variants collection.

  See [expanding](https://developers.swell.is/backend-api/querying/expanding) for more details.
- `fields` (string): Returns only the specified fields in the result.

  - For example `fields=name,slug` would return only the fields `name` and `slug` in the response.

  Supports nested object and array fields using dot-notation.

  - For example, `items.product_id`. The product `id` is always returned.
- `include` (object): Include one or more arbitrary queries in the response which are potentially related to the main query.

  See [including](https://developers.swell.is/backend-api/querying/including) for more details.
- `limit` (int): Limit the number of records returned, ranging between `1` and `1000`. Defaults to `15`. Default: `15`.
- `page` (int): The page number of results to return given the specified or default `limit`.
- `search` (string): A text search is performed using the search argument. Searchable fields are defined by the model.

  - For example, `search=red` would return records containing the word "red" anywhere in the defined text fields.

  See [searching](https://developers.swell.is/backend-api/querying/searching) for more details.
- `sort` (string): Expression to sort results by using a format similar to a SQL sort statement.

  - For example, `sort=name asc` would return records sorted by name ascending.

  See [sorting](https://developers.swell.is/backend-api/querying/sorting) for more details.
- `where` (object): An object with criteria to filter the result.

  - For example, `active=true` would return records containing a field `active` with the value `true`.

  It's also possible to use query operators, for example, `$eq`, `$ne`, `$gt`, and more.

  See [querying](https://developers.swell.is/backend-api/querying) for more details.

### Example request

`GET /accounts:credits`

**cURL**

```sh
$ curl https://api.swell.store/accounts:credits/ \
  -u store-id:secret-key \
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/accounts:credits/', {
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/accounts:credits/', [
]);
```

### Example response

```json
{
  "count": 3,
  "results": [
    {
      "parent_id": "621e53a6213eaf013d1fa6b2",
      "amount": 17,
      "reason": "refund",
      "currency": "USD",
      "date_created": "2022-07-08T20:27:40.199Z",
      "number": "100003",
      "date_updated": "2022-07-12T16:02:14.965Z",
      "id": "62c8933c5f4ffe00136039f2"
    },
    {
      "parent_id": "621506e39c5e9e013dac45a0",
      "amount": 15,
      "giftcard_id": "62166e0072c71f013d0b3216",
      "currency": "USD",
      "date_created": "2022-02-23T17:25:25.101Z",
      "number": "100002",
      "id": "62166e056050eb01329b8cce"
    },
    {
      "parent_id": "621506e39c5e9e013dac45a0",
      "amount": 10,
      "giftcard_id": "6206da29178ab8013d86c6aa",
      "currency": "USD",
      "date_created": "2022-02-23T17:24:28.605Z",
      "number": "100001",
      "id": "62166dcc6050eb01329a4359"
    }
  ],
  "page": 1,
  "page_count": 1,
  "limit": 15
}
```


## Delete an account credit

Delete a customer account credit permanently.

### Arguments

- `id` (objectId, required): Unique identifier of the account credit.

### Example request

`DELETE /accounts:credits/:id`

**cURL**

```sh
$ curl https://api.swell.store/accounts:credits/62c8933c5f4ffe00136039f2 \
  -u store-id:secret-key \
  -X DELETE
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.delete('/accounts:credits/{id}', {
  id: '62c8933c5f4ffe00136039f2'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->delete('/accounts:credits/{id}', [
  'id' => '62c8933c5f4ffe00136039f2'
]);
```

### Example response

```json
{
  "parent_id": "621e53a6213eaf013d1fa6b2",
  "amount": 17,
  "reason": "refund",
  "currency": "USD",
  "date_created": "2022-07-08T20:27:40.199Z",
  "number": "100003",
  "date_updated": "2022-07-12T16:02:14.965Z",
  "id": "62c8933c5f4ffe00136039f2"
}
```

