# Import contract term

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


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

Imports an active or historical [contract term](/docs/api/contract_terms) for a subscription.

Use this operation to import contract terms when migrating subscriptions from another billing system, or to add historical contract term data for existing subscriptions. You can import both active contract terms (currently running) and historical contract terms (completed, canceled, or terminated).

### Prerequisites & Constraints

-   The [Contract Terms](https://www.chargebee.com/docs/billing/2.0/subscriptions/contract-terms) feature must be enabled on the site.
-   The [Multi-Frequency Billing](https://www.chargebee.com/docs/billing/2.0/subscriptions/subscriptions#multi-frequency-billing) feature must not be enabled for the site.
-   The subscription must have a fixed billing cycle (the [`remaining_billing_cycles`](/docs/api/subscriptions#remaining_billing_cycles) must be set).
-   The contract term period must not overlap with any existing contract terms for the subscription.

### Impacts

**

Contract term

**

-   A new `contract_term` resource is created and [associated](/docs/api/subscriptions#contract_term) with the subscription.
-   For active contract terms:
-   the `total_contract_value` is calculated as the sum of the contract estimate and the `total_amount_raised` parameter.
-   The `contract_end` date is calculated based on the `contract_start` date and the `billing_cycle` parameter.

### Implementation Notes

-   Check the subscription's `remaining_billing_cycles` attribute. If it is not set, the subscription is set to forever renewal. [Update the subscription](subscriptions#update_subscription_for_items_billing_cycles) to a fixed billing cycle before importing a contract term.
-   [Check for existing contract terms](subscriptions#list_contract_terms_for_a_subscription) for the subscription. Ensure that the the `contract_start` and `contract_end` dates don't overlap with any existing contract term for the subscription.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/subscriptions/__test__8asukSOXdvMNOv/import_contract_term \
     -u {site_api_key}:\
     -d "contract_term[action_at_term_end]"="CANCEL" \
     -d "contract_term[billing_cycle]"=5 \
     -d "contract_term[contract_start]"=1483245610 \
     -d "contract_term[contract_end]"=1493613610 \
     -d "contract_term[status]"="TERMINATED" \
     -d "contract_term[total_contract_value]"=1000
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Subscription.ImportContractTerm("__test__8asukSOXdvMNOv")
		.ContractTermActionAtTermEnd(Subscription.SubscriptionContractTerm.ActionAtTermEndEnum.Cancel)
		.ContractTermBillingCycle(5)
		.ContractTermContractStart(1483245610)
		.ContractTermContractEnd(1493613610)
		.ContractTermStatus(Subscription.SubscriptionContractTerm.StatusEnum.Terminated)
		.ContractTermTotalContractValue(1000)
		.Request();

ContractTerm contractTerm = result.ContractTerm;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    subscriptionAction "github.com/chargebee/chargebee-go/v3/actions/subscription"
    "github.com/chargebee/chargebee-go/v3/models/subscription"
    subscriptionEnum "github.com/chargebee/chargebee-go/v3/models/subscription/enum"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := subscriptionAction.ImportContractTerm("__test__8asukSOXdvMNOv", &subscription.ImportContractTermRequestParams{
        ContractTerm : &subscription.ImportContractTermContractTermParams{
            ActionAtTermEnd : subscriptionEnum.ContractTermActionAtTermEndCancel,
            BillingCycle : chargebee.Int32(5),
            ContractStart : chargebee.Int64(1483245610),
            ContractEnd : chargebee.Int64(1493613610),
            Status : subscriptionEnum.ContractTermStatusTerminated,
            TotalContractValue : chargebee.Int64(1000),
        },
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        ContractTerm := res.ContractTerm
    }
}
```

#### 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.SubscriptionImportContractTermRequest{
    ContractTerm : &chargebee.SubscriptionImportContractTermContractTerm{
        ActionAtTermEnd : chargebee.ContractTermActionAtTermEndCancel,
        BillingCycle : chargebee.Int32(5),
        ContractStart : chargebee.Int64(1483245610),
        ContractEnd : chargebee.Int64(1493613610),
        Status : chargebee.ContractTermStatusTerminated,
        TotalContractValue : chargebee.Int64(1000),
    },
}
  res, err := client.Subscription.ImportContractTerm("__test__8asukSOXdvMNOv", req)
      if err != nil {
        fmt.Println(err)
    } else {
        ContractTerm := res.ContractTerm
    }
}
```

#### Java

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

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Subscription.importContractTerm("__test__8asukSOXdvMNOv")
            .contractTermActionAtTermEnd(Subscription.ContractTerm.ActionAtTermEnd.CANCEL)
            .contractTermBillingCycle(5)
            .contractTermContractStart(new Timestamp(1483245610L * 1000))
            .contractTermContractEnd(new Timestamp(1493613610L * 1000))
            .contractTermStatus(Subscription.ContractTerm.Status.TERMINATED)
            .contractTermTotalContractValue(1000L)
            .request();

        ContractTerm contractTerm = result.contractTerm();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.contractTerm.ContractTerm;
import com.chargebee.v4.models.subscription.params.SubscriptionImportContractTermParams;
import com.chargebee.v4.models.subscription.responses.SubscriptionImportContractTermResponse;
import java.sql.Timestamp;

public class SubscriptionImportContractTerm {

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

        SubscriptionImportContractTermParams.ContractTermParams contractTermParams =
            SubscriptionImportContractTermParams.ContractTermParams.builder()
                .actionAtTermEnd(SubscriptionImportContractTermParams.ContractTermParams.ActionAtTermEnd.CANCEL)
                .billingCycle(5)
                .contractStart(new Timestamp(1483245610L * 1000))
                .contractEnd(new Timestamp(1493613610L * 1000))
                .status(SubscriptionImportContractTermParams.ContractTermParams.Status.TERMINATED)
                .totalContractValue(1000L)
                .build();

        SubscriptionImportContractTermParams params = SubscriptionImportContractTermParams.builder()
            .contractTerm(contractTermParams)
            .build();

        SubscriptionImportContractTermResponse response = client
            .subscriptions()
            .importContractTerm("__test__8asukSOXdvMNOv", params);

        ContractTerm contractTerm = response.getContractTerm();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.subscription.importContractTerm("__test__8asukSOXdvMNOv", {
        contract_term: {
            action_at_term_end: "cancel",
            billing_cycle: 5,
            contract_start: 1483245610,
            contract_end: 1493613610,
            status: "terminated",
            total_contract_value: 1000
        }
    });

    console.log(result);
    const contractTerm = result.contract_term;
} 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->subscription()->importContractTerm("__test__8asukSOXdvMNOv", [
    "contract_term" => [
        "action_at_term_end" => "cancel",
        "billing_cycle" => 5,
        "contract_start" => 1483245610,
        "contract_end" => 1493613610,
        "status" => "terminated",
        "total_contract_value" => 1000
    ]
]);
$contractTerm = $result->contract_term;
```

#### Python

```python
import chargebee
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Subscription.import_contract_term("__test__8asukSOXdvMNOv",
    cb_client.Subscription.ImportContractTermParams(
        contract_term=cb_client.Subscription.ImportContractTermContractTermParams(
            action_at_term_end=chargebee.Subscription.ContractTermActionAtTermEnd.CANCEL,
            billing_cycle=5,
            contract_start=1483245610,
            contract_end=1493613610,
            status=chargebee.Subscription.ContractTermStatus.TERMINATED,
            total_contract_value=1000
        )
    )
)
contract_term = response.contract_term
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::Subscription.import_contract_term("__test__8asukSOXdvMNOv",{
  :contract_term => {
    :action_at_term_end => "CANCEL",
    :billing_cycle => 5,
    :contract_start => 1483245610,
    :contract_end => 1493613610,
    :status => "TERMINATED",
    :total_contract_value => 1000
  }
})

contract_term = result.contract_term
```

## Sample Response

```json
{
  "contract_term": {
    "action_at_term_end": "cancel",
    "billing_cycle": 5,
    "cancellation_cutoff_period": 0,
    "contract_end": 1493613610,
    "contract_start": 1483245610,
    "created_at": 1483245610,
    "id": "__test__8asukSOXdvULP3",
    "object": "contract_term",
    "status": "terminated",
    "total_contract_value": 1000
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/subscriptions/{subscription-id}/import_contract_term

## Input Parameters

- `contract_term_billing_cycle_on_renewal` (optional, integer, min=1, max=100)
  The number of billing cycles the new contract term should run for when the contract term renews. This value is used when `action_at_term_end` is `renew` or `renew_once`.
  
  **Constraints**
  
  -   Should not be sent when `action_at_term_end` is `cancel` or `evergreen`.
  
  **Default value**
  
  -   Defaults to the value of `billing_cycle` or a custom value depending on the [site configuration](https://www.chargebee.com/docs/billing/2.0/subscriptions/contract-terms#configuring-contract-terms).

- `contract_term` (optional, string)
  Parameters for contract\_term
  - `id` (optional, string, max chars=50)
    Unique identifier for the contract term in the site.
    
    **Default value**
    
    -   If not provided, a unique identifier is automatically generated.
  - `created_at` (optional, timestamp(UTC) in seconds)
    The date when the contract term was created.
    
    **Required if**
    
    -   The contract term `status` is `active`.
    
    **Constraints**
    
    -   For active contract terms, cannot be a future date unless the subscription `status` is `future`.
    -   Must be less than or equal to `contract_start`.
    
    **Default value**
    
    -   For historical contract terms, defaults to `contract_start` if not provided.
  - `contract_start` (optional, timestamp(UTC) in seconds)
    The start date of the contract term.
    
    **Constraints**
    
    -   Must be less than `contract_end`.
    -   For active contract terms, cannot be a future date unless the subscription `status` is `future`.
    -   For historical contract terms, must be a past date.
    -   Cannot be less than `created_at`.
    -   Must not overlap with existing contract terms for the subscription.
  - `contract_end` (optional, timestamp(UTC) in seconds)
    The end date of the contract term.
    
    **Required if**
    
    -   The contract term `status` is not `active` (for historical contract terms).
    
    **Constraints**
    
    -   Should not be sent when the contract term `status` is `active` (it is calculated automatically).
    -   Must be greater than `contract_start`.
    -   Must be a past date.
    -   Must not overlap with existing contract terms for the subscription.
  - `status` (optional, enumerated string)
    Current status of the contract term. Use `active` for currently running contract terms, or `completed`, `cancelled`, or `terminated` for historical contract terms.
    Possible enum values:
      - `active`
        An actively running contract term.
        
        **Prerequisite**
        
        -   The subscription `status` must be `future`, `in_trial`, `active`, or `non-renewing`.
      - `completed`
        The contract term has run its full duration.
      - `cancelled`
        The contract term was ended because a change in the subscription caused a [subscription term reset](/docs/api/subscriptions/update-subscription-for-items#force_term_reset), or the subscription was canceled due to non-payment.
      - `terminated`
        The contract term was terminated ahead of completion.
  - `total_amount_raised` (optional, in cents, default=0, min=0)
    The amount raised for the contract term up to the time of importing the subscription. This amount is added to the contract estimate to calculate the [`total_contract_value`](/docs/api/contract_terms#total_contract_value) for active contract terms.
    
    **Required if**
    
    -   The contract term `status` is `active`.
    
    **Constraints**
    
    -   Should not be sent when the contract term `status` is not `active`.
  - `total_amount_raised_before_tax` (optional, in cents, default=0, min=0)
    The amount raised for the contract term up to the time of importing the subscription, excluding tax. This amount is added to the contract estimate to calculate the [`total_contract_value_before_tax`](contract_terms#contract_term_total_contract_value_before_tax) for active contract terms.
    
    **Required if**
    
    -   The contract term `status` is `active` and [pre-tax TCV](https://www.chargebee.com/docs/contract-terms.html) is enabled on the site.
    
    **Constraints**
    
    -   Should not be sent when the contract term `status` is not `active`.
  - `total_contract_value` (optional, in cents, default=0, min=0)
    The sum of the [totals](/docs/api/invoices#total) of all invoices raised as part of the contract term. For active contract terms, this is a predicted value calculated as the sum of the contract estimate and `total_amount_raised`.
    
    **Required if**
    
    -   The contract term `status` is not `active` (for historical contract terms).
    
    **Constraints**
    
    -   Should not be sent when the contract term `status` is `active` (it is calculated automatically).
  - `total_contract_value_before_tax` (optional, in cents, default=0, min=0)
    The total amount of revenue expected to be generated from the contract term, calculated as the sum of all invoices raised during the term, excluding taxes. For active contract terms, this is calculated as the sum of the contract estimate (before tax) and `total_amount_raised_before_tax`.
    
    **Required if**
    
    -   The contract term `status` is not `active` and [pre-tax TCV](https://www.chargebee.com/docs/contract-terms.html) is enabled on the site (for historical contract terms).
    
    **Constraints**
    
    -   Should not be sent when the contract term `status` is `active` (it is calculated automatically).
  - `billing_cycle` (optional, integer, min=0)
    The number of billing cycles of the subscription that the contract term covers.
    
    **Required if**
    
    -   The contract term `status` is `active`.
    
    **Constraints**
    
    -   For active contract terms, must be greater than the subscription's `remaining_billing_cycles` when the subscription `status` is `active` or `non-renewing`.
    
    **Default value**
    
    -   For historical contract terms, defaults to `1` if not provided.
  - `action_at_term_end` (optional, enumerated string, default=renew)
    Action to be taken when the contract term completes.
    Possible enum values:
      - `renew`
        The contract term completes and a new contract term is started for the number of billing cycles specified in `contract_term_billing_cycle_on_renewal`. The `action_at_term_end` for the new contract term is set to `renew`.
      - `evergreen`
        The contract term completes and the subscription continues to renew without a new contract term.
      - `cancel`
        The contract term completes and the subscription is canceled.
      - `renew_once`
        The contract term completes and a new contract term is started for the number of billing cycles specified in `contract_term_billing_cycle_on_renewal`. The `action_at_term_end` for the new contract term is set to `cancel`, so the subscription is canceled when the new contract term completes.
  - `cancellation_cutoff_period` (optional, integer)
    The number of days before `contract_end` during which the customer is barred from canceling the contract term. The customer can cancel the contract term via the [Self-Serve Portal](https://www.chargebee.com/docs/self-serve-portal.html) only before this period. This allows you to have sufficient time for processing the contract term closure.
    
    **Required if**
    
    -   The `action_at_term_end` is `renew` or `renew_once`.
    
    **Constraints**
    
    -   Must be less than the duration of the contract term (in days).
    -   Should not be sent when `action_at_term_end` is `cancel` or `evergreen`.

## Returns

- `contract_term` (Contract term object)
  Resource object representing contract\_term
