# Add a charge-item to a pending invoice

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

This endpoint is used when [metered billing](https://www.chargebee.com/docs/billing/2.0/usage-based-billing/metered_billing) is enabled and it adds a [charge-item price](/docs/api/item_prices) to a `pending` invoice. To collect the accumulated charges by closing the invoice, call [Close a pending invoice](/docs/api/invoices/close-a-pending-invoice).

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/invoices/__demo_inv__2/add_charge_item \
     -u {site_api_key}:\
     -d "item_price[item_price_id]"="ssl-charge-USD" \
     -d "item_price[unit_price]"=495
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Invoice.AddChargeItem("__demo_inv__2")
		.ItemPriceItemPriceId("ssl-charge-USD")
		.ItemPriceUnitPrice(495)
		.Request();

Invoice invoice = result.Invoice;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    invoiceAction "github.com/chargebee/chargebee-go/v3/actions/invoice"
    "github.com/chargebee/chargebee-go/v3/models/invoice"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := invoiceAction.AddChargeItem("__demo_inv__2", &invoice.AddChargeItemRequestParams{
        ItemPrice : &invoice.AddChargeItemItemPriceParams{
            ItemPriceId : "ssl-charge-USD",
            UnitPrice : chargebee.Int64(495),
        },
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.InvoiceAddChargeItemRequest{
    ItemPrice : &chargebee.InvoiceAddChargeItemItemPrice{
        ItemPriceId : "ssl-charge-USD",
        UnitPrice : chargebee.Int64(495),
    },
}
  res, err := client.Invoice.AddChargeItem("__demo_inv__2", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Invoice.addChargeItem("__demo_inv__2")
            .itemPriceItemPriceId("ssl-charge-USD")
            .itemPriceUnitPrice(495L)
            .request();

        Invoice invoice = result.invoice();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.invoice.Invoice;
import com.chargebee.v4.models.invoice.params.InvoiceAddChargeItemParams;
import com.chargebee.v4.models.invoice.responses.InvoiceAddChargeItemResponse;

public class InvoiceAddChargeItem {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        InvoiceAddChargeItemParams.ItemPriceParams itemPriceParams =
            InvoiceAddChargeItemParams.ItemPriceParams.builder()
                .itemPriceId("ssl-charge-USD")
                .unitPrice(495L)
                .build();

        InvoiceAddChargeItemParams params = InvoiceAddChargeItemParams.builder()
            .itemPrice(itemPriceParams)
            .build();

        InvoiceAddChargeItemResponse response = client
            .invoices()
            .addChargeItem("__demo_inv__2", params);

        Invoice invoice = response.getInvoice();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.invoice.addChargeItem("__demo_inv__2", {
        item_price: {
            item_price_id: "ssl-charge-USD",
            unit_price: 495
        }
    });

    console.log(result);
    const invoice = result.invoice;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->invoice()->addChargeItem("__demo_inv__2", [
    "item_price" => [
        "item_price_id" => "ssl-charge-USD",
        "unit_price" => 495
    ]
]);
$invoice = $result->invoice;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Invoice.add_charge_item("__demo_inv__2",
    cb_client.Invoice.AddChargeItemParams(
        item_price=cb_client.Invoice.AddChargeItemItemPriceParams(
            item_price_id="ssl-charge-USD",
            unit_price=495
        )
    )
)
invoice = response.invoice
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::Invoice.add_charge_item("__demo_inv__2",{
  :item_price => {
    :item_price_id => "ssl-charge-USD",
    :unit_price => 495
  }
})

invoice = result.invoice
```

## Sample Response

```json
{
  "invoice": {
    "adjustment_credit_notes": {},
    "amount_adjusted": 0,
    "amount_due": 1001,
    "amount_paid": 0,
    "amount_to_collect": 1001,
    "applied_credits": {},
    "base_currency_code": "USD",
    "credits_applied": 0,
    "currency_code": "USD",
    "customer_id": "__test__KyVk7hRy2QAqMx",
    "date": 1517502930,
    "deleted": false,
    "dunning_attempts": {},
    "exchange_rate": 1,
    "first_invoice": false,
    "has_advance_charges": false,
    "id": "__demo_inv__2",
    "is_gifted": false,
    "issued_credit_notes": {},
    "line_items": [
      {
        "amount": 1000,
        "customer_id": "__test__KyVk7hRy2QAqMx",
        "date_from": 1517502930,
        "date_to": 1519922130,
        "description": "basic USD",
        "discount_amount": 0,
        "entity_id": "basic-USD",
        "entity_type": "plan_item_price",
        "id": "li___test__KyVkDeRy2QDX18",
        "is_taxed": false,
        "item_level_discount_amount": 0,
        "object": "line_item",
        "pricing_model": "per_unit",
        "quantity": 1,
        "subscription_id": "__test__KyVk7hRy2QAqMx",
        "tax_amount": 0,
        "unit_amount": 1000
      },
      {..}
    ],
    "linked_orders": {},
    "linked_payments": {},
    "net_term_days": 0,
    "object": "invoice",
    "price_type": "tax_exclusive",
    "recurring": true,
    "resource_version": 1517502935000,
    "round_off_amount": 0,
    "status": "pending",
    "sub_total": 1001,
    "subscription_id": "__test__KyVk7hRy2QAqMx",
    "tax": 0,
    "term_finalized": true,
    "total": 1001,
    "updated_at": 1517502935,
    "write_off_amount": 0
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/invoices/{invoice-id}/add_charge_item

## Input Parameters

- `comment` (optional, string, max chars=300)
  An internal [comment](/docs/api/comments) to be added for this operation, to the invoice. This comment is displayed on the Chargebee UI. It is not displayed on any customer-facing [Hosted Page](/docs/api/hosted_pages) or any document such as the [Invoice PDF](/docs/api/invoices/retrieve-invoice-as-pdf) .

- `subscription_id` (optional, string, max chars=50)
  Identifier of the subscription for which this addon needs to be created. Applicable for consolidated invoice.

- `item_price` (optional, string)
  Parameters for item\_price
  - `item_price_id` (required, string, max chars=100)
    A unique ID for your system to identify the item price.
  - `quantity` (optional, integer, min=1)
    Item price quantity
  - `quantity_in_decimal` (optional, string, max chars=33)
    The decimal representation of the quantity of the item purchased. Can be provided for quantity-based item prices and only when [multi-decimal pricing](/docs/api/currencies) is enabled.
  - `unit_price` (optional, in cents, min=0)
    The price or per-unit-price of the item price. By default, it is the [value set](/docs/api/item_prices/item_price-object#price) for the `item_price`. This is only applicable when the `pricing_model` of the `item_price` is `flat_fee` or `per_unit`. The value depends on the [type of currency](/docs/api/currencies) .
  - `unit_price_in_decimal` (optional, string, max chars=39)
    The decimal representation of the price or per-unit price of the plan. The value is in major units of the currency. Always returned when [multi-decimal pricing](/docs/api/currencies) is enabled.
  - `date_from` (optional, timestamp(UTC) in seconds)
    The time when the service period for the item starts.
  - `date_to` (optional, timestamp(UTC) in seconds)
    The time when the service period for the item ends.

- `item_tiers` (optional, array)
  Parameters for item\_tiers
  - `starting_unit` (optional, integer)
    The lowest value in the quantity tier.
  - `ending_unit` (optional, integer)
    The highest value in the quantity tier.
  - `price` (optional, in cents)
    The per-unit price for the tier when the `pricing_model` is `tiered` or `volume`. The total cost for the item price when the `pricing_model` is `stairstep`. The value is in the minor unit of the currency.
  - `starting_unit_in_decimal` (optional, string, max chars=33)
    The decimal representation of the lowest value of quantity in this tier. This is zero for the lowest tier. For all other tiers, it is the same as `ending_unit_in_decimal` of the next lower tier. Returned only when the pricing\_model is `tiered` , `volume` or `stairstep` and [multi-decimal pricing](/docs/api/currencies) is enabled.
  - `ending_unit_in_decimal` (optional, string, max chars=33)
    The decimal representation of the highest value of quantity in this tier. This attribute is not applicable for the highest tier. For all other tiers, it must be equal to the `starting_unit_in_decimal` of the next higher tier. Returned only when the pricing\_model is `tiered` , `volume` or `stairstep` and [multi-decimal pricing](/docs/api/currencies) is enabled.
  - `price_in_decimal` (optional, string, max chars=39)
    The decimal representation of the per-unit price for the tier when the `pricing_model` is `tiered` or `volume`. When the `pricing_model` is `stairstep` , it is the decimal representation of the total price for the item. The value is in major units of the currency. Returned when the plan is quantity-based and [multi-decimal pricing](/docs/api/invoices/add-a-charge-item-to-a-pending-invoice) is enabled.
  - `pricing_type` (optional, enumerated string)
    Pricing type for the tier.
    Possible enum values:
      - `per_unit`
        Indicates that the tier pricing is based on individual units. Customers are charged a fixed price per unit. For example, if the price per unit is $2 and the customer consumes 150 units, they will be charged $300 (150 × $2).
      - `flat_fee`
        Indicates that the tier pricing is a flat fee, applied to the entire tier regardless of the number of units consumed. For the **stairstep** pricing model, `pricing_type` will be set to `flat_fee` by default. For example, if the flat fee for a tier is $100, the customer pays $100 whether they consume 1 unit or the maximum number of units within that tier.
      - `package`
        Indicates that the tier pricing is based on a package of units. Customers are charged for each block or package of units. For example, if the package size is 100 units and the cost per block is $20 consuming 400 units will result in a charge of $80 (4 × $20).
  - `package_size` (optional, integer)
    Package size for the tier when pricing type is `package`. Specify the number of units that make up one package. For example, if 1000 API hits are grouped into a single package, set the package size to 1000.

## Returns

- `invoice` (Invoice object)
  Resource object representing invoice
