# Create using Chargebee token

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


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

Storing card after successful 3DS completion is not supported in this API. Use [create using Payment Intent API](/docs/api/payment_sources/create-using-payment-intent) under Payment source to store the card after successful 3DS flow completion.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/payment_sources/create_using_token \
     -u {site_api_key}:\
     -d customer_id="__test__XpbTXGTSRp4REKES" \
     -d token_id="cb_tok___test__XpbTXGTSRp4RaBEV"
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = PaymentSource.CreateUsingToken()
		.CustomerId("__test__XpbTXGTSRp4REKES")
		.TokenId("cb_tok___test__XpbTXGTSRp4RaBEV")
		.Request();

Customer customer = result.Customer;
PaymentSource paymentSource = result.PaymentSource;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    paymentsourceAction "github.com/chargebee/chargebee-go/v3/actions/paymentsource"
    "github.com/chargebee/chargebee-go/v3/models/paymentsource"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := paymentsourceAction.CreateUsingToken(&paymentsource.CreateUsingTokenRequestParams{
        CustomerId : "__test__XpbTXGTSRp4REKES",
        TokenId : "cb_tok___test__XpbTXGTSRp4RaBEV",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        PaymentSource := res.PaymentSource
    }
}
```

#### 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.PaymentSourceCreateUsingTokenRequest{
    CustomerId : "__test__XpbTXGTSRp4REKES",
    TokenId : "cb_tok___test__XpbTXGTSRp4RaBEV",
}
  res, err := client.PaymentSource.CreateUsingToken(req)
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        PaymentSource := res.PaymentSource
    }
}
```

#### 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 = PaymentSource.createUsingToken()
            .customerId("__test__XpbTXGTSRp4REKES")
            .tokenId("cb_tok___test__XpbTXGTSRp4RaBEV")
            .request();

        Customer customer = result.customer();
        PaymentSource paymentSource = result.paymentSource();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.customer.Customer;
import com.chargebee.v4.models.paymentSource.PaymentSource;
import com.chargebee.v4.models.paymentSource.params.PaymentSourceCreateUsingTokenParams;
import com.chargebee.v4.models.paymentSource.responses.PaymentSourceCreateUsingTokenResponse;

public class PaymentSourceCreateUsingToken {

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

        PaymentSourceCreateUsingTokenParams params = PaymentSourceCreateUsingTokenParams.builder()
            .customerId("__test__XpbTXGTSRp4REKES")
            .tokenId("cb_tok___test__XpbTXGTSRp4RaBEV")
            .build();

        PaymentSourceCreateUsingTokenResponse response = client.paymentSources().createUsingToken(params);

        Customer customer = response.getCustomer();
        PaymentSource paymentSource = response.getPaymentSource();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.paymentSource.createUsingToken({
        customer_id: "__test__XpbTXGTSRp4REKES",
        token_id: "cb_tok___test__XpbTXGTSRp4RaBEV"
    });

    console.log(result);
    const customer = result.customer;
    const paymentSource = result.payment_source;
} 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->paymentSource()->createUsingToken([
    "customer_id" => "__test__XpbTXGTSRp4REKES",
    "token_id" => "cb_tok___test__XpbTXGTSRp4RaBEV"
]);
$customer = $result->customer;
$paymentSource = $result->payment_source;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.PaymentSource.create_using_token(
    cb_client.PaymentSource.CreateUsingTokenParams(
        customer_id="__test__XpbTXGTSRp4REKES",
        token_id="cb_tok___test__XpbTXGTSRp4RaBEV"
    )
)
customer = response.customer
payment_source = response.payment_source
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::PaymentSource.create_using_token({
  :customer_id => "__test__XpbTXGTSRp4REKES",
  :token_id => "cb_tok___test__XpbTXGTSRp4RaBEV"
})

customer = result.customer
payment_source = result.payment_source
```

## Sample Response

```json
{
  "customer": {
    "allow_direct_debit": false,
    "auto_collection": "on",
    "card_status": "valid",
    "created_at": 1517487233,
    "deleted": false,
    "excess_payments": 0,
    "first_name": "Mark",
    "id": "__test__XpbTXGTSRp4REKES",
    "last_name": "Henry",
    "net_term_days": 0,
    "object": "customer",
    "payment_method": {
      "gateway": "stripe",
      "gateway_account_id": "gw___test__5SK2lMpwSRp4Mx02v",
      "object": "payment_method",
      "reference_id": "cus_J7rVlNvtSznu6p/card_1IVbmYJv9j0DyntJOIWtQCkk",
      "status": "valid",
      "type": "card"
    },
    "pii_cleared": "active",
    "preferred_currency_code": "USD",
    "primary_payment_source_id": "pm___test__XpbTXGTSRp4RqgEX",
    "promotional_credits": 0,
    "refundable_credits": 0,
    "resource_version": 1517487236333,
    "taxability": "taxable",
    "unbilled_charges": 0,
    "updated_at": 1517487236
  },
  "payment_source": {
    "card": {
      "brand": "visa",
      "expiry_month": 5,
      "expiry_year": 2022,
      "first_name": "MyCard",
      "funding_type": "credit",
      "iin": "******",
      "last4": "4242",
      "last_name": "testing",
      "masked_number": "************4242",
      "object": "card"
    },
    "created_at": 1517487236,
    "customer_id": "__test__XpbTXGTSRp4REKES",
    "deleted": false,
    "gateway": "stripe",
    "gateway_account_id": "gw___test__5SK2lMpwSRp4Mx02v",
    "id": "pm___test__XpbTXGTSRp4RqgEX",
    "issuing_country": "US",
    "object": "payment_source",
    "reference_id": "cus_J7rVlNvtSznu6p/card_1IVbmYJv9j0DyntJOIWtQCkk",
    "resource_version": 1517487236331,
    "status": "valid",
    "type": "card",
    "updated_at": 1517487236
  }
}
```

## URL Format

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

## Input Parameters

- `customer_id` (required, string, max chars=50)
  Identifier of the customer with whom this payment source is associated.

- `brand_id` (optional, string, max chars=50)
  The unique ID of the [brand](/docs/api/brands) this payment source should be linked to. Applicable only when multiple brands have been created for the site. This need not match the brand of the `customer_id`; when the two differ, the value provided here is used for the payment source. An alternative way of passing this parameter is by means of the `chargebee-brand-id` custom HTTP header; when both are provided, they must specify the same brand.
  
  **Default behavior**
  
  -   When not provided, the payment source is linked to the brand of the customer it is created for.

- `replace_primary_payment_source` (optional, boolean, default=false)
  Indicates whether the primary payment source should be replaced with this payment source. In case of Create Subscription for Customer endpoint, the default value is True. Otherwise, the default value is False.

- `token_id` (required, string, max chars=40)
  Token generated by Chargebee.js representing payment method details.

## Returns

- `customer` (Customer object)
  Resource object representing customer

- `payment_source` (Payment source object)
  Resource object representing payment\_source
