> ## Documentation Index
> Fetch the complete documentation index at: https://docs.localpayment.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Tokenization

Tokenization with Localpayment provides a secure way to handle card information, reducing your security risk and simplifying future transactions.

## How It Works

The process involves these key steps:

1. **Card Information Collection**: Card information is securely collected through a form within your application and then transmitted to the Localpayment API.
2. **Token Generation**: Localpayment securely stores the card information and generates a unique, irreversible token representing the card details. This token is returned in the API response.
3. **Using the Tokenized Card**: For subsequent card payments, you will send the `card.token` property instead of the customer's actual card details.

***

## Before You Begin

Before tokenizing a card, ensure you have completed the following:

> [card] [API Credentials](https://docs.localpayment.com/docs/api-keys-and-environments.md)
>
> Valid API credentials and access token for authentication.

> [card] [Account Setup](https://docs.localpayment.com/docs/make-your-first-test-payment.md)
>
> Proper account setup for your target country and business verification.

> [card] [IP Whitelisting](https://docs.localpayment.com/docs/dashboard-whitelist-your-ips.md)
>
> Whitelist your server IP addresses for API access.

> [card] [Webhook Endpoint](https://docs.localpayment.com/docs/webhooks.md)
>
> Set up a webhook URL to receive payment notifications.

***

## Tokenization Methods

There are two available methods for tokenizing a card:

* [Endpoint for card tokenization](#option-1-endpoint-for-card-tokenization)
* [Card tokenization during payment](#option-2-card-tokenization-during-payment)

### Option 1: Endpoint for card tokenization

You can securely tokenize card information for future use through a dedicated endpoint.

To tokenize a card, send a `POST` request to the [Tokenize Card](https://docs.localpayment.com/reference/tokenize-card.md) endpoint with the following parameters:

| Parameter | Description | Required |
| - | - | - |
| `name` | Cardholder name. | ✅ |
| `number` | Card number. | ✅ |
| `cvv` | Card Verification Value. | ✅ |
| `expirationMonth` | Card expiration month. | ✅ |
| `expirationYear` | Card expiration year. | ✅ |

#### Example Request

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --request POST \
     --url https://api.stage.localpayment.com/api/card \
     --header 'Authorization: Bearer <your_access_token>' \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '
{
  "name": "John Doe",
  "number": "4000000000000002",
  "cvv": "123",
  "expirationMonth": "11",
  "expirationYear": "2029"
}
'
```

#### Successful Response Example

In response, the API returns the card token:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
  "card": {
    "token": "xsWMaLcYjS3L11aBwxl465%2F2d8J85FXPAaBFIKfr0Fx%3D",
    "bin": "400000",
    "brand": "VISA",
    "country": "US",
    "name": "John Doe",
    "last4": "0002",
    "expirationYear": 2029,
    "expirationMonth": 11
  },
  "errors": []
}
```

#### Key Response Fields

| Field | Description |
| - | - |
| `card.token` | Tokenized card. |

### Option 2: Card tokenization during payment

Localpayment enables merchants to tokenize cards during [payment processing](https://docs.localpayment.com/docs/one-time-payments.md)  by including the `save` parameter in the payment requests. This streamlined approach allows you to both process a transaction and store card details in a single API call.

To tokenize a card, send a `POST` request to the [Create Payin](https://docs.localpayment.com/reference/payins.md) endpoint and include the `card.save` property set to `true` in your Payin request.

#### Example Request

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --request POST \
  --url https://api.stage.localpayment.com/api/payin/ \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer <your_access_token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "paymentMethod": {
        "type": "CreditCard",
        "code": "1600",
        "flow": "DIRECT"
    },
    "externalId": "dffbd2a6-6e65-469b-8bbe-dac269882220",
    "country": "MEX",
    "currency": "MXN",
    "amount": 1000,
    "accountNumber": "{{accountNumber}}",
    "conceptCode": "0001",
    "merchant": {
        "type": "COMPANY",
        "name": "Company",
        "document": {
            "type": "RFC",
            "id": "XXX840510XX1"
        },
        "email": "mail@company.com"
    },
    "payer": {
        "type": "INDIVIDUAL",
        "name": "John",
        "lastname": "Doe",
        "document": {
            "type": "RFC",
            "id": "YYY180222YY6"
        },
        "email": "john.doe@example.com"
    },
    "card": {
        "number": "4000000000000002",
        "cvv": "123",
        "expirationMonth": "12",
        "expirationYear": "2029",
        "save": true
    }
}'
```

#### Successful Response Example

In response, the API returns the card token:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
  "transactionType": "PayIn",
  "externalId": "dffbd2a6-6e65-469b-8bbe-dac269882220",
  "internalId": "22b29c98-92af-44bd-a164-1863ae2f1bb3",
  "paymentMethod": {
    "type": "CreditCard",
    "code": "1000",
    "flow": "DIRECT"
  },
  "country": "MEX",
  "currency": "MXN",
  "amount": 1000,
  "accountNumber": "{{accountNumber}}",
  "confirmed": {
    "currency": "MXN",
    "amount": 1000,
    "fxQuote": 1,
    "exchangeRateToken": null
  },
  "payment": {
    "installment": null,
    "currency": "MXN",
    "fxQuote": 1,
    "financingFee": 0,
    "amount": 1000
  },
  "localTaxes": [],
  "withHoldings": [],
  "fees": {
    "description": "Fee",
    "currency": "MXN",
    "fxSource": 210.8,
    "fxQuote": 1,
    "amount": 13.02,
    "account": "{{accountNumber}}"
  },
  "status": {
    "code": "103",
    "description": "APPROVED",
    "detail": "The payin was confirmed but not credited yet"
  },
  "ticket": null,
  "qr": null,
  "beneficiary": null,
  "merchant": {
    "type": "COMPANY",
    "name": "Company"
  },
  "payer": {
    "type": "INDIVIDUAL",
    "name": "John",
    "lastname": "Doe",
    "document": {
      "id": "YYY180222YY6",
      "type": "RFC"
    },
    "email": "john.doe@example.com",
    "address": null,
    "birthdate": null,
    "nationality": null
  },
  "intermediaries": [],
  "wireInstructions": null,
  "date": {
    "creationDate": "2024-05-19T19:02:44.018+00:00",
    "processedDate": "2024-05-19T19:02:44.799618",
    "expirationDate": "0001-01-01T00:00:00"
  },
  "card": {
    "token": "xsWMaLcYjS3L11aBwxl465%2F2d8J85FXPAaBFIKfr0Fx%3D",
    "bin": "400000",
    "brand": "VISA",
    "country": "RS",
    "name": "John Doe",
    "last4": "0002",
    "expirationYear": 2029,
    "expirationMonth": 12
  },
  "errors": []
}
```

#### Key Response Fields

| Field | Description |
| - | - |
| `card.token` | Tokenized card. |

***

## Using Tokenized Cards

To use a tokenized card for a payment, include the `card.token` in your payment request.

### Example Request With Token

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --request POST \
  --url https://api.stage.localpayment.com/api/payin/ \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer <your_access_token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "paymentMethod": {
        "type": "CreditCard",
        "code": "1600",
        "flow": "DIRECT"
    },
    "externalId": "dffbd2a6-6e65-469b-8bbe-dac269882220",
    "country": "MEX",
    "currency": "MXN",
    "amount": 1000,
    "accountNumber": "{{accountNumber}}",
    "conceptCode": "0001",
    "merchant": {
        "type": "COMPANY",
        "name": "Company",
        "document": {
            "type": "RFC",
            "id": "XXX840510XX1"
        },
        "email": "mail@company.com"
    },
    "payer": {
        "type": "INDIVIDUAL",
        "name": "John",
        "lastname": "Doe",
        "document": {
            "type": "RFC",
            "id": "YYY180222YY6"
        },
        "email": "john.doe@example.com"
    },
    "card": {
        "token": "WleV6CZ111IMxqp53SIrqzOn1JsGKCef8oBh2mbi56M%3D",
        "cvv": "123",
        "installments": "1"
    }
}'
```

***

## Get Card Information

After a card has been tokenized, you can retrieve its details using the Localpayment API. This allows you to display stored payment methods to customers while maintaining PCI compliance.

To obtain card information, send a `GET` request to the [Get Card Details](https://docs.localpayment.com/reference/get-card.md) endpoint, including the card's token as a query parameter.

### Example Request

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --location 
--request GET 'https://api.stage.localpayment.com/api/card/?token={token}' \
--header 'Authorization: Bearer <your_access_token>'
```

### Successful Response Example

In response, the API returns the card details:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
 {
  "card": {
    "token": "WleV6CZ111IMxqp53SIrqzOn1JsGKCef8oBh2mbi56M%3D",
    "bin": "400000",
    "brand": "VISA",
    "country": "US",
    "name": "",
    "last4": "0002",
    "expirationYear": 2029,
    "expirationMonth": 11
  },
  "errors": []
}
```

***

## Delete a Tokenized Card

Permanently remove stored card tokens through a dedicated API endpoint. This irreversible action helps maintain compliance with data privacy regulations and allows for proper card lifecycle management.

To delete a tokenized card, send a `DELETE` request to the [Delete Tokenized Card](https://docs.localpayment.com/reference/delete-card.md) endpoint, including the card's token as a query parameter.

### Example Request

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --location \
--request DELETE 'https://api.stage.localpayment.com/api/card/?token={token}' \
--header 'Authorization: Bearer <your_access_token>'
```

### Successful Response Example

A successful deletion will typically return an empty JSON object:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
  "errors": []
}
```

***

## Next Steps

Now that you've successfully tokenized a card, you can use the generated token for secure processing across Localpayment's payment services:

> [card] [One-Time Payments](https://docs.localpayment.com/docs/one-time-payments.md)
>
> Use the token to process single, immediate payments.

> [card] [Subscriptions](https://docs.localpayment.com/docs/subscriptions-overview.md)
>
> Set up automated recurring billing cycles using the tokenized card.

> [card] [Installments](https://docs.localpayment.com/docs/installments.md)
>
> Offer installment payment options to your customers using the tokenized card.

> [card] [3D Secure](https://docs.localpayment.com/docs/3d-secure.md)
>
> Enhance the security of tokenized card payments with 3D Secure authentication.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.