# Credit Grant routes: apply, issue, void

URL: /code/api/credit-grant/verbs · Id: `page:code/api/credit-grant/verbs`

> The Credit Grant Verbs apply, issue, void as api.sb routes, with inputs, result, refusals and an example.

Reference for [Credit Grant](/business/nouns/credit-grant), one group of [Credit Grant routes](/code/api/credit-grant).

### CreditGrant.apply
Apply spends Credit Grants against a finalizing Invoice, promotional first, then by priority and earliest expiry, and settles them through Stripe credit grants. [Verb page](/business/nouns/credit-grant/apply?interface=api).

```text
POST /{namespace}/credit-grants/apply
```

Body, as JSON: the inputs. Emits [`CreditGrant.applied`](/business/events/CreditGrant.applied).

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `invoice` | [Invoice](/business/nouns/invoice) | Yes | The Invoice being finalized. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `applied` | `money` | Yes | The total the grants paid toward the Invoice. |
| `creditGrants` | list of [Credit Grant](/business/nouns/credit-grant) | Yes | Each grant drawn, with its new remaining balance. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/credit-grants/apply \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"invoice":"inv_9sfvgeRT7L"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "applied": {
    "amount": 29,
    "currency": "USD"
  },
  "creditGrants": [
    {
      "$id": "https://api.sb/leaseline.app/credit-grants/credgr_PduV9IB1HF",
      "$type": "CreditGrant",
      "amount": {
        "amount": 29,
        "currency": "USD"
      },
      "category": "paid"
    }
  ],
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "CreditGrant.apply",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```

### CreditGrant.issue
Issue gives a Customer a paid or promotional Credit Grant that its metered calls draw down before anything is invoiced. [Verb page](/business/nouns/credit-grant/issue?interface=api).

```text
POST /{namespace}/credit-grants/issue
```

Body, as JSON: the inputs. Emits [`CreditGrant.issued`](/business/events/CreditGrant.issued).

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `customer` | [Customer](/business/nouns/customer) | Yes | The Customer who receives it. |
| `amount` | `money` | Yes | The balance granted. |
| `category` | `string` | Yes | `paid` for a balance the Customer bought; `promotional` for one given, which is never cash. |
| `appliesTo` | [Offer](/business/nouns/offer) | No | The metered Offer it pays for; every metered Offer of the Startup when absent. |
| `expiresAt` | `datetime` | No | When any remaining balance lapses; never, when absent. |
| `priority` | `integer` | No | Order of use among grants; a lower number spends first. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `creditGrant` | [Credit Grant](/business/nouns/credit-grant) | Yes | The new Credit Grant, its remaining balance equal to its amount. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/credit-grants/issue \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customer":"cus_K1BROJhqzZ","amount":{"amount":29,"currency":"USD"},"category":"paid"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "creditGrant": {
    "$id": "https://api.sb/leaseline.app/credit-grants/credgr_PduV9IB1HF",
    "$type": "CreditGrant",
    "amount": {
      "amount": 29,
      "currency": "USD"
    },
    "category": "paid"
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "CreditGrant.issue",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```

### CreditGrant.void
Void ends a promotional Credit Grant early and forfeits its remaining balance, keeping the record. [Verb page](/business/nouns/credit-grant/void?interface=api).

```text
POST /{namespace}/credit-grants/{creditGrant}/void
```

Path: `creditGrant`. Body, as JSON: the other inputs. Emits [`CreditGrant.voided`](/business/events/CreditGrant.voided).

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `creditGrant` | [Credit Grant](/business/nouns/credit-grant) | Yes | The grant to end. |
| `reason` | `text` | Yes | Why it ends, shown to the Customer. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `creditGrant` | [Credit Grant](/business/nouns/credit-grant) | Yes | The voided grant, its remaining balance zero. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/credit-grants/credgr_PduV9IB1HF/void \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reason":"Example reason"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "creditGrant": {
    "$id": "https://api.sb/leaseline.app/credit-grants/credgr_PduV9IB1HF",
    "$type": "CreditGrant",
    "amount": {
      "amount": 29,
      "currency": "USD"
    },
    "category": "paid"
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "CreditGrant.void",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```