# เปลี่ยนวิธีการเก็บ Access Token

โดยปกติแล้ว, sdk ตัวนี้จะคอยเก็บ access token และ ดึง access token ล่าสุดมาจาก temp system ให้เองจากการสร้าง access token oauth2

ถ้าท่านต้องการเปลี่ยนวิธีการเก็บให้, ท่านสามารถแก้ไขได้โดยการ implement `Farzai\KApi\Contracts\AccessTokenRepositoryInterface` class ได้เลย

ยกตัวอย่างเช่น

```php
<?php

namespace App\Repositories;

use Farzai\KApi\Contracts\AccessTokenRepositoryInterface;

class DatabaseAccessTokenRepository implements AccessTokenRepositoryInterface
{

    /**
     * Create a new storage instance.
     */
    public function __construct()
    {
        // Connect database
    }

    /**
     * Get the access token.
     */
    public function retrieve(): ?AccessToken
    {
        // Load access token from your database
        $data = [
            'access_token' => '',
            
            // int The expiration time for the access token. Expressed in seconds.
            'expires_in' => '', 
            
            // The time the access token was issued. (Y-m-d H:i:s\.u)
            'issued_at' => '',
            
            // The scope (if any) associated with the access token.
            'scope' => '',
            
            // The status of the access token (e.g., approved or revoked).
            'status' => '',
            
            // It is a required parameter which is assigned by the KBank Open API and specifies the type of token.
            'token_type' => '',
        ];

        return new AccessToken($data);
    }

    /**
     * Store the access token.
     */
    public function store(AccessToken $token): void
    {
        // Encode the access token.
        $data = json_encode($token->toArray());

        // Store the access token.
        // ....
    }

    /**
     * Forget the access token.
     */
    public function forget(): void
    {
        // Remove the access token from your database
        // 
    }
}
```

เพิ่มเข้าไปยัง `ClientBuilder`

```php
$accessTokenRepository = new DatabaseAccessTokenRepository();

$client = ClientBuilder::make()
    ->setConsumer("<YOUR_CONSUMER_ID>", "<YOUR_CONSUMER_SECRET>")
    ->asSandbox()
    ->setTokenRepository($accessTokenRepository) // <-- เพิ่มเข้าไปยัง Client Builder
    ->build();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://farzai.gitbook.io/kbank-kapi-php/undefined/access-token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
