# Create an invoice for unbilled charges

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


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

[Asynchronous](/docs/api/async_response/async-response-object)

Use this API to bill the [unbilled charges](https://www.chargebee.com/docs/unbilled-charges.html). Available Credits and Excess Payments will automatically be applied while creating the invoice.

If the _Auto Collection_ is turned on for the particular customer, the invoice will be created in payment\_due state and the payment collection will be scheduled immediately.

During invoice creation, the PO number for the line items will be filled from the subscription's current PO number, if available.

If no recurring item is present in the created invoice, the invoice will be marked as recurring=false.

If consolidated invoicing is enabled and the parameter 'customer\_id' is passed, multiple invoices can be created based on the following factors.

-   Currency
-   PO number if 'Group by PO number' is enabled
-   Shipping address
-   Auto Collection
-   Payment method

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/unbilled_charges/invoice_unbilled_charges \
     -u {site_api_key}:\
     -d subscription_id="__test__8aszcSOcqxg162"
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = UnbilledCharge.InvoiceUnbilledCharges()
		.SubscriptionId("__test__8aszcSOcqxg162")
		.Request();

List<Invoice> invoices = result.Invoices;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    unbilledchargeAction "github.com/chargebee/chargebee-go/v3/actions/unbilledcharge"
    "github.com/chargebee/chargebee-go/v3/models/unbilledcharge"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := unbilledchargeAction.InvoiceUnbilledCharges(&unbilledcharge.InvoiceUnbilledChargesRequestParams{
        SubscriptionId : "__test__8aszcSOcqxg162",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Invoices := res.Invoices
    }
}
```

#### 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.UnbilledChargeInvoiceUnbilledChargesRequest{
    SubscriptionId : "__test__8aszcSOcqxg162",
}
  res, err := client.UnbilledCharge.InvoiceUnbilledCharges(req)
      if err != nil {
        fmt.Println(err)
    } else {
        Invoices := res.Invoices
    }
}
```

#### Java

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

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = UnbilledCharge.invoiceUnbilledCharges()
            .subscriptionId("__test__8aszcSOcqxg162")
            .request();

        List<Invoice> invoices = result.invoices();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.invoice.Invoice;
import com.chargebee.v4.models.unbilledCharge.params.InvoiceUnbilledChargesParams;
import com.chargebee.v4.models.unbilledCharge.responses.InvoiceUnbilledChargesResponse;
import java.util.List;

public class InvoiceUnbilledCharges {

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

        InvoiceUnbilledChargesParams params = InvoiceUnbilledChargesParams.builder()
            .subscriptionId("__test__8aszcSOcqxg162")
            .build();

        InvoiceUnbilledChargesResponse response = client.unbilledCharges().invoiceUnbilledCharges(params);

        List<Invoice> invoices = response.getInvoices();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.unbilledCharge.invoiceUnbilledCharges({
        subscription_id: "__test__8aszcSOcqxg162"
    });

    console.log(result);
    const invoices = result.invoices;
} 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->unbilledCharge()->invoiceUnbilledCharges([
    "subscription_id" => "__test__8aszcSOcqxg162"
]);
$invoices = $result->invoices;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.UnbilledCharge.invoice_unbilled_charges(
    cb_client.UnbilledCharge.InvoiceUnbilledChargesParams(
        subscription_id="__test__8aszcSOcqxg162"
    )
)
invoices = response.invoices
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::UnbilledCharge.invoice_unbilled_charges({
  :subscription_id => "__test__8aszcSOcqxg162"
})

invoices = result.invoices
```

## Sample Response

```json
{
  "invoices": [
    {
      "adjustment_credit_notes": {},
      "amount_adjusted": 0,
      "amount_due": 1100,
      "amount_paid": 0,
      "amount_to_collect": 1100,
      "applied_credits": {},
      "base_currency_code": "USD",
      "billing_address": {
        "first_name": "John",
        "last_name": "Doe",
        "object": "billing_address",
        "validation_status": "not_validated"
      },
      "credits_applied": 0,
      "currency_code": "USD",
      "customer_id": "__test__8aszcSOcqxbQ5z",
      "date": 1517495908,
      "deleted": false,
      "due_date": 1517495908,
      "dunning_attempts": {},
      "exchange_rate": 1,
      "first_invoice": true,
      "has_advance_charges": false,
      "id": "__demo_inv__1",
      "is_gifted": false,
      "issued_credit_notes": {},
      "line_items": [
        {
          "amount": 1000,
          "customer_id": "__test__8aszcSOcqxbQ5z",
          "date_from": 1517495907,
          "date_to": 1519915107,
          "description": "basic USD",
          "discount_amount": 0,
          "entity_id": "basic-USD",
          "entity_type": "plan_item_price",
          "id": "li___test__8aszcSOcqxjI64",
          "is_taxed": false,
          "item_level_discount_amount": 0,
          "object": "line_item",
          "pricing_model": "per_unit",
          "quantity": 1,
          "subscription_id": "__test__8aszcSOcqxg162",
          "tax_amount": 0,
          "tax_exempt_reason": "tax_not_configured",
          "unit_amount": 1000
        },
        {..}
      ],
      "linked_orders": {},
      "linked_payments": {},
      "net_term_days": 0,
      "new_sales_amount": 1100,
      "object": "invoice",
      "price_type": "tax_exclusive",
      "recurring": true,
      "resource_version": 1517495908473,
      "round_off_amount": 0,
      "status": "payment_due",
      "sub_total": 1100,
      "subscription_id": "__test__8aszcSOcqxg162",
      "tax": 0,
      "term_finalized": true,
      "total": 1100,
      "updated_at": 1517495908,
      "write_off_amount": 0
    },
    {..}
  ]
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/unbilled_charges/invoice_unbilled_charges

## Input Parameters

- `subscription_id` (optional, string, max chars=50)
  Identifier of the subscription for which this invoice needs to be created. Should be specified if 'customer\_id' is not specified.

- `customer_id` (optional, string, max chars=50)
  Identifier of the customer for whom this invoice needs to be created. Should be specified if 'subscription\_id' is not specified. Applicable only if the consolidated invoicing is enabled. .

## Returns

- `invoices` (always returned)
  Resource object representing invoice
