# Sync usages

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


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

Updates the [`quantity`](/docs/api/invoices/invoice-object#line_items_quantity) for `metered` [`line_items`](/docs/api/invoices/invoice-object#line_items) of an invoice to reflect the latest [usage](/docs/api/usages) data. **Note:** This operation is done automatically while [closing](/docs/api/invoices/close-a-pending-invoice) the invoice.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/invoices/draft_inv_AwSNuHT1ebuqgOu/sync_usages \
     -X POST  \
     -u {site_api_key}:
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Invoice.SyncUsages("draft_inv_AwSNuHT1ebuqgOu").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"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := invoiceAction.SyncUsages("draft_inv_AwSNuHT1ebuqgOu").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.InvoiceSyncUsagesRequest{
    Id : "draft_inv_AwSNuHT1ebuqgOu",
}
  res, err := client.Invoice.SyncUsages(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.syncUsages("draft_inv_AwSNuHT1ebuqgOu").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.InvoiceSyncUsagesParams;
import com.chargebee.v4.models.invoice.responses.InvoiceSyncUsagesResponse;

public class InvoiceSyncUsages {

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

        InvoiceSyncUsagesResponse response = client.invoices().syncUsages("draft_inv_AwSNuHT1ebuqgOu");

        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.syncUsages("draft_inv_AwSNuHT1ebuqgOu");

    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()->syncUsages("draft_inv_AwSNuHT1ebuqgOu");
$invoice = $result->invoice;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Invoice.sync_usages("draft_inv_AwSNuHT1ebuqgOu")
invoice = response.invoice
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::Invoice.sync_usages("draft_inv_AwSNuHT1ebuqgOu")

invoice = result.invoice
```

## Sample Response

```json
{
  "invoice": {
    "id": "draft_inv_AwSNuHT1ebuqgOu",
    "customer_id": "AwSNuHT1eblJbOe",
    "subscription_id": "AwSNuHT1ebupIOr",
    "recurring": true,
    "status": "pending",
    "price_type": "tax_exclusive",
    "date": 1630866600,
    "net_term_days": 0,
    "exchange_rate": 1,
    "total": 10000,
    "amount_paid": 0,
    "amount_adjusted": 0,
    "write_off_amount": 0,
    "credits_applied": 0,
    "amount_due": 10000,
    "updated_at": 1630919754,
    "resource_version": 1630919754875,
    "deleted": false,
    "object": "invoice",
    "first_invoice": false,
    "amount_to_collect": 10000,
    "round_off_amount": 0,
    "has_advance_charges": false,
    "currency_code": "USD",
    "base_currency_code": "USD",
    "generated_at": 1630866600,
    "is_gifted": false,
    "term_finalized": true,
    "channel": "web",
    "tax": 0,
    "line_items": [
      {
        "id": "li_AwSNuHT1ebuq3Ot",
        "date_from": 1628188200,
        "date_to": 1630866599,
        "unit_amount": 10000,
        "quantity": 1,
        "amount": 10000,
        "pricing_model": "per_unit",
        "is_taxed": false,
        "tax_amount": 0,
        "object": "line_item",
        "subscription_id": "AwSNuHT1ebupIOr",
        "customer_id": "AwSNuHT1eblJbOe",
        "description": "metered-plan",
        "entity_type": "plan_item_price",
        "entity_id": "metered-plan-USD-Monthly",
        "metered": true,
        "tax_exempt_reason": "export",
        "discount_amount": 0,
        "item_level_discount_amount": 0
      },
      {..}
    ],
    "sub_total": 10000,
    "linked_payments": {},
    "applied_credits": {},
    "adjustment_credit_notes": {},
    "issued_credit_notes": {},
    "linked_orders": {},
    "dunning_attempts": {}
  }
}
```

## URL Format

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

## Returns

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