I Have generated a token for API calls by using following code :

curl -X POST "http://magento2.ce/rest/V1/integration/customer/token"\
-H "Content-Type:application/json"\
-d '{"username":"john@change.me", "password":"abc123"}'

-How to set expiry time for this token?
-Can we generate token by using username? with out password.

  • Is it possible we will set token lifetime to minutes..? Sep 25 '19 at 5:52

Install Magento from github.com/magento/magento2.git and then use the steps from documentation page about Token-based authentication

On Github, you should look on branch '2.2.0-preview' or 'develop' or any other that uses the codebase of 2.2.0 or higher (develop is active branch by default).

This feature probably will be backported on Magento 2.1.9 and other versions.

Here is how it should look like: Access Token Expiration

  • Thank You Tomaw...But i am using Magento2.1.6 Version. I want to disable token expiry feature in this version. In 2.1.6 version we have "Expiration Period" option.If i am giving empty value for this field, then also token expiring after some time. Oct 3 '17 at 11:46

For anyone using the Web API, you may only need to allow anonymous access for simple tasks such as fetching products for your custom frontend/client - no need for an access token. Admin > Stores > Configuration > Services > Magento Web API enter image description here


Yes, you can generate tokens by using the only username.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$tokenModelFactory = $objectManager->get('Magento\Integration\Model\Oauth\TokenFactory');

$this->tokenModelFactory = $tokenModelFactory;    
$customerToken = $this->tokenModelFactory->create();
$tokenKey = $customerToken->createCustomerToken($customerId)->getToken();

return $tokenKey;

Get the $customerId from customer collection and place it in the code at

$tokenKey = $customerToken->createCustomerToken($customerId)->getToken();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.