# Create a credit unit

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


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

Creates a new credit unit in the `active` status.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/credit_units \
     -u {site_api_key}:\
     -d id="ai_tokens_001" \
     -d name="AI tokens" \
     -d external_name="AI tokens" \
     -d is_unlimited="false" \
     -d overdraft_amount="1000"
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = CreditUnit.Create()
		.Id("ai_tokens_001")
		.Name("AI tokens")
		.ExternalName("AI tokens")
		.IsUnlimited(false)
		.OverdraftAmount("1000")
		.Request();

CreditUnit creditUnit = result.CreditUnit;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    creditunitAction "github.com/chargebee/chargebee-go/v3/actions/creditunit"
    "github.com/chargebee/chargebee-go/v3/models/creditunit"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := creditunitAction.Create(&creditunit.CreateRequestParams{
        Id : "ai_tokens_001",
        Name : "AI tokens",
        ExternalName : "AI tokens",
        IsUnlimited : chargebee.Bool(false),
        OverdraftAmount : "1000",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        CreditUnit := res.CreditUnit
    }
}
```

#### 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.CreditUnitCreateRequest{
    Id : "ai_tokens_001",
    Name : "AI tokens",
    ExternalName : "AI tokens",
    IsUnlimited : chargebee.Bool(false),
    OverdraftAmount : "1000",
}
  res, err := client.CreditUnit.Create(req)
      if err != nil {
        fmt.Println(err)
    } else {
        CreditUnit := res.CreditUnit
    }
}
```

#### 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 = CreditUnit.create()
            .id("ai_tokens_001")
            .name("AI tokens")
            .externalName("AI tokens")
            .isUnlimited(false)
            .overdraftAmount("1000")
            .request();

        CreditUnit creditUnit = result.creditUnit();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.creditUnit.CreditUnit;
import com.chargebee.v4.models.creditUnit.params.CreditUnitCreateParams;
import com.chargebee.v4.models.creditUnit.responses.CreditUnitCreateResponse;

public class CreditUnitCreate {

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

        CreditUnitCreateParams params = CreditUnitCreateParams.builder()
            .id("ai_tokens_001")
            .name("AI tokens")
            .externalName("AI tokens")
            .isUnlimited(false)
            .overdraftAmount("1000")
            .build();

        CreditUnitCreateResponse response = client.creditUnits().create(params);

        CreditUnit creditUnit = response.getCreditUnit();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.creditUnit.create({
        id: "ai_tokens_001",
        name: "AI tokens",
        external_name: "AI tokens",
        is_unlimited: false,
        overdraft_amount: "1000"
    });

    console.log(result);
    const creditUnit = result.credit_unit;
} 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->creditUnit()->create([
    "id" => "ai_tokens_001",
    "name" => "AI tokens",
    "external_name" => "AI tokens",
    "is_unlimited" => false,
    "overdraft_amount" => "1000"
]);
$creditUnit = $result->credit_unit;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.CreditUnit.create(
    cb_client.CreditUnit.CreateParams(
        id="ai_tokens_001",
        name="AI tokens",
        external_name="AI tokens",
        is_unlimited=False,
        overdraft_amount="1000"
    )
)
credit_unit = response.credit_unit
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::CreditUnit.create({
  :id => "ai_tokens_001",
  :name => "AI tokens",
  :external_name => "AI tokens",
  :is_unlimited => "false",
  :overdraft_amount => "1000"
})

credit_unit = result.credit_unit
```

## Sample Response

```json
{
  "credit_unit": {
    "id": "ai_tokens_001",
    "name": "AI tokens",
    "external_name": "AI tokens",
    "status": "active",
    "resource_version": 1784633586000,
    "is_unlimited": false,
    "overdraft_amount": "1000",
    "created_at": 1784633586,
    "created_by": "sample.user@chargebee.com",
    "updated_at": 1784633586,
    "updated_by": "sample.user@chargebee.com"
  }
}
```

## URL Format

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

## Input Parameters

- `id` (required, string, max chars=50)
  Unique identifier for the credit unit. Must be unique across the site.

- `name` (required, string, max chars=50)
  Internal display name for the credit unit. Must be unique across the site.

- `is_unlimited` (required, boolean)
  Indicates whether the credit unit allows unlimited overdraft consumption. When `true`, grace consumption continues without a cap after the allocated grants are exhausted. When `false`, provide `overdraft_amount` to cap the grace consumption.

- `overdraft_amount` (optional, string, max chars=50)
  The amount up to which grace consumption is allowed after the allocated grants are exhausted. A positive decimal value that applies only when `is_unlimited` is `false`.

- `external_name` (optional, string, max chars=50)
  Customer-facing display name for the credit unit. Must be unique across the site. Defaults to `name` when omitted.

## Returns

- `credit_unit` (Credit unit object)
  Resource object representing credit unit
