# Tokenize Card Credentials

This guide explains how to generate authentication tokens using the `AuthRepository`. Tokens allow your application to securely authenticate with third-party services or backend systems on behalf of the merchant, without handling sensitive card data directly.

## Prerequisites

* The TPV must be initialized via `InitializationRepository`.
* You must have access to the `AuthRepository`.

## Generating a Token

To retrieve a token, you use the `getToken` function. This function communicates with the **Get Smart SDK Payment Service** to issue a secure token usable by a third party.

### Steps

1. **Call `getToken`**: Invoke the suspend function on your `AuthRepository` instance.
2. **Handle the Result**: The function returns a `RepositoryResult<AuthResult>`. You must handle the `Success` state which contains the `AuthResult` sealed interface.

### Code Example

```kotlin
suspend fun retrieveToken() {
    // 1. Call getToken
    val result = authRepository.getToken()

    // 2. Handle the result
    when (result) {
        is RepositoryResult.Success -> {
            // The request was successful, check the specific AuthResult
            when (val authOutcome = result.data) {
                is AuthResult.AuthInfo -> {
                    println("✅ Token Retrieved Successfully")
                    println("Token: ${authOutcome.token}")
                    println("Merchant ID: ${authOutcome.merchantId}")
                    println("FUC: ${authOutcome.fuc}")
                    println("Terminal: ${authOutcome.terminal}") // Available in TpvInfo, context dependent
                }
                is AuthResult.TokenNotFound -> {
                    println("❌ Token Not Found: ${authOutcome.message}")
                }
            }
        }
        is RepositoryResult.ConnectionError -> {
            println("❌ Connection Error: Ensure the service is running.")
        }
        is RepositoryResult.ProtocolError -> {
            println("❌ Protocol Error: ${result.type}")
        }
        is RepositoryResult.Cancelled -> {
            println("❌ Operation Cancelled: ${result.message}")
        }
    }
}
```

## Understanding the Response

The AuthResult sealed interface provides two possible outcomes:

| Outcome     | Class         | Description                                                                               |
| :---------- | :------------ | :---------------------------------------------------------------------------------------- |
| **Success** | AuthInfo      | Contains the token, fuc, merchantId, merchantName, merchantCountry, and merchantCurrency. |
| **Failure** | TokenNotFound | Indicates that a valid token could not be generated or found. Contains a message string.  |

## Troubleshooting

* **ConnectionError**: If you receive this error, verify that the **Get Smart SDK Payment Service** is installed and running on the device.
* **TokenNotFound**: This may indicate a configuration issue with the merchant parameters on the host.