How to get customer access token by id for REST api Magento 2?

Get Customer token for the REST API using customer id in Magento 2 used to call API.

If you have customer id, retrieve the token by the Token factory class of the Integration module.

Instantiate the Magento\Integration\Model\Oauth\TokenFactory in your class constructor method.

namespace Jesadiya\CustomerToken\Model;

use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory;

class Token
     * Token Model
     * @var TokenModelFactory
    private $tokenModelFactory;

    public function __construct(
        TokenModelFactory $tokenModelFactory
    ) {
        $this->tokenModelFactory = $tokenModelFactory;

     * Retrieve customer token
     * @param int $customerId
     * @return string
     public function createToken(int $customerId)
        $token = $this->tokenModelFactory->create()->createCustomerToken($customerId)->getToken();
        return $token;

Call method by required input,

$customerId = 1;
$result = $this->createToken($customerId);