# addons.buy()

Canonical URL: https://developers.iagenify.com/sdk/ref/cloud/addons-buy/
Language: en

`api.cloud.addons.buy({ service, gb }) → Promise<{ charged, usage }>`

Adds gigabytes to the account, paid in credits for a 30-day period. Full price on the first purchase, prorated for the days left on the next.

Status: POST /api/cloud/addons · charged immediately · renews monthly

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `service` | string | required | `database` — one pool for NoSQL and SQL — or `object`. |
| `gb` | integer | required | Whole gigabytes, 1 or more. 100 at most per add-on, held together. |

## The price {#price}

| service | Covers | Credits per GB per period |
| --- | --- | --- |
| `database` | NoSQL and SQL, as one pool | 100 |
| `object` | Object Storage | 200 |

The live figures are in `usage().addons.pricePerGb`, so an interface can read them rather than carry a copy.

## What is charged, and when {#charged}

| Situation | Charged now |
| --- | --- |
| No add-on yet, or the period has ended | The full monthly price. A new 30-day period starts. |
| A period is running | Prorated over the days left. The period does not move. |
| The add-ons are suspended | The new price plus what was owed, and the pool resumes. |

```javascript
// First purchase — a period opens
const first = await api.cloud.addons.buy({ service: "object", gb: 1 });
// { charged: 200, usage: { … addons: { objectGb: 1, renewsAt: "…", monthlyCredits: 200 } } }

// Ten days later, one more gigabyte: 20 days left of 30
const more = await api.cloud.addons.buy({ service: "object", gb: 1 });
// { charged: 134, usage: { … addons: { objectGb: 2, monthlyCredits: 400 } } }
```

Proration rounds up, and the two gigabytes then renew together on the same date — one period per account, not one per purchase.

## Returns {#returns}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `charged` | integer |  | Credits taken for this purchase. |
| `usage` | object |  | The fresh [usage()](/sdk/ref/cloud/usage) report, ceilings already raised. No second call needed. |

## An add-on is a pool, not a bigger ceiling {#pool}

Each storage keeps its guaranteed ceiling; the add-on sits above them and is drawn on by whichever storage has gone past its own. So a gigabyte bought while one database is full goes where it is needed, and the guarantee of the other one is never touched.

> **database is one pool for two services**
>
> A gigabyte bought as database serves NoSQL and SQL alike. What one takes in overflow, the other cannot — which is the only way the two share anything.

## Renewal, and what suspension does {#renewal}

At the end of a period the monthly price is taken again. If the balance will not cover it, the add-ons are suspended rather than removed: `lapsed` turns true, the ceilings go back to the plan's, and the renewal is retried — so topping up brings the space back without buying again.

```javascript
const { addons } = await api.cloud.usage();

if (addons.lapsed) {
  // The pool is suspended. Ceilings are the plan's until the renewal goes through.
  console.log(`${addons.monthlyCredits} credits are needed to resume.`);
}
```

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| invalid_addon | 400 | `service` is not `database` or `object`. |
| invalid_gb | 400 | Not a whole number of at least 1, or the 100 GB limit for that add-on would be passed. |
| insufficient_credits | 402 | The balance will not cover this purchase. The message says the price and the balance. |
| user_not_found | 404 | The account behind the session is gone. |

```javascript
import { IaGenifyError } from "iagenify";

try {
  await api.cloud.addons.buy({ service: "object", gb: 1 });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "insufficient_credits") {
    // Nothing was charged and nothing was added.
  }
}
```

Credits are taken before the gigabytes are recorded, and a refusal records nothing — a failed purchase leaves the account exactly as it was.

- [usage()](/sdk/ref/cloud/usage): The whole report, field by field.
- [Quotas and plans](/sdk/ref/cloud/quotas): What each plan includes, per storage.
- [Credits](/sdk/concepts/credits): What a credit is, and where the balance is read.

```python
# First purchase — a period opens
first = api.cloud.addons.buy(service="object", gb=1)
# {"charged": 200, "usage": {… "addons": {"objectGb": 1, "renewsAt": "…", "monthlyCredits": 200}}}

# Ten days later, one more gigabyte: 20 days left of 30
more = api.cloud.addons.buy(service="object", gb=1)
# {"charged": 134, "usage": {… "addons": {"objectGb": 2, "monthlyCredits": 400}}}
```

```python
addons = api.cloud.usage()["addons"]

if addons["lapsed"]:
    # The pool is suspended. Ceilings are the plan's until the renewal goes through.
    print(f"{addons['monthlyCredits']} credits are needed to resume.")
```

```python
from iagenify import IaGenifyError

try:
    api.cloud.addons.buy(service="object", gb=1)
except IaGenifyError as error:
    if error.code != "insufficient_credits":
        raise
    # Nothing was charged and nothing was added.
```
