{"id":1754,"date":"2021-05-06T05:57:29","date_gmt":"2021-05-06T05:57:29","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/fetch-bearer-token-in-magento-2\/"},"modified":"2025-06-09T09:09:22","modified_gmt":"2025-06-09T03:39:22","slug":"fetch-bearer-token-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/fetch-bearer-token-in-magento-2\/","title":{"rendered":"How to Fetch Bearer Token in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 supports&nbsp;<a href=\"https:\/\/developer.adobe.com\/commerce\/webapi\/rest\/\" target=\"_blank\" rel=\"noreferrer noopener\">REST<\/a>&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-api-integration-service.html\" target=\"_blank\" rel=\"noreferrer noopener\">API Integration<\/a>, which lets you speed up getting, sending, and processing data. It also transfers it to the third-party system. Postman is one of the recommended REST clients which connects with&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>(Application Programming Interface).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Postman is an API testing tool, an API development environment, and a handy HTTP client for testing websites that simplifies API workflow in testing and development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The bearer token is a cryptic string, usually generated by the server in response to a login request. The&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-admin-token\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento access token<\/a>&nbsp;must be given on the line to allow a web API request from a client. As a result, the token serves as an electronic key that enables you to enter&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have to pass an authorized token in the postman while sending API to Magento requested from the postman for security purposes. What if you have the requirement to validate the condition, like if the token is not there, then the condition must be false?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, when we forget the password- we use the facility of \u2018forget password\u2019 and reset our old password with the new one. In the backend of this process, a temporary token gets generated and passed in the whole process.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such kind of scenarios, we\u2019ve to use the below method to\u00a0fetch bearer token in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Fetch Bearer Token in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public function getAuthToken(){\n\n    $token = false;\n\n    $headers = []; \n    foreach ($_SERVER as $name => $value) \n    { \n        if (substr($name, 0, 5) == 'HTTP_') \n        { \n            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; \n        } \n    }\n    $authorizationBearer = '';\n\n    if(isset($headers['Authorization'])) {\n        $authorizationBearer = $headers['Authorization'];\n    } else if(isset($headers['authorization'])) {\n        $authorizationBearer = $headers['authorization'];\n    } else {\n        $authorizationBearer = \"\";\n    }\n\n    $authorizationBearerArr = explode(' ', $authorizationBearer);\n    if(\n        isset($authorizationBearerArr[0]) &amp;&amp; \n        trim($authorizationBearerArr[0]) == 'Bearer' &amp;&amp; \n        isset($authorizationBearerArr[1])\n    ){\n        $token = $authorizationBearerArr[1];\n    }\n\n    return $token;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note: You can use this method for any REST API , postman is used here for example.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ve created a preference and called model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ve used the above code in the model file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can call the file according to your requirement according the path you\u2019ve passed in the xml file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more details you can refer the post:&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-custom-rest-api-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Custom Rest API in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 supports&nbsp;REST&nbsp;API Integration, which lets you speed up getting, sending, and processing data. It also transfers it to the third-party system. Postman is one&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1754","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1754"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1754\/revisions"}],"predecessor-version":[{"id":16500,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1754\/revisions\/16500"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}