BE SMART, hand over your headache to Rootways and FOCUS ON YOUR BUSINESS.

Magento 2 Get REST API token

Posted in: Magento REST API

Magento 2 Get REST API token

This Tutorial instructs you that how to get REST API token in Magento 2
This Token will be use as a mediator between 3rd party and Magento.
By default, this token is valid for 4 hours. You can set this value from the magento admin. Check the Magento admin path as below,
Magento Admin -> Stores -> Settings -> Configuration -> Services -> OAuth -> Access Token Expiration -> Admin Token Lifetime (hours).
There are Two ways to create an integration Token code.

  1. Magento Admin
  2. Curl script

    1) Magento Admin
  • Go to Magento Admin -> System -> Integration
    Here you can see the "Add New Integration" on it and add new token.

    2) Curl Script
  • You can get token using Magento admin username and password. Below is the curl script of how you can get admin token.
    Curl Script usually executing in three parts.
    1. Endpoint: 'YOUR_MAGENTO_BASE_URL/rest/V1/integration/admin/token';
    2. Headers: 'Content-Type: application/json'
    3. Payload: { "username": "your_magento_admin_username", "password": "your_magento_admin_password" }

    Please refer the Whole Curl Script as below.
    $data = array("username" => "your_magento_admin_username", "password" => "your_magento_admin_password");
    $requestParameters = json_encode($data);
    $ch = curl_init();
    $ch = curl_init($tokenPostUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParameters);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
           'Content-Type: application/json',
           'Content-Length: ' . strlen($requestParameters))
    $token = curl_exec($ch);
    $token = json_decode($token);
    echo 'Token = ' . $token;

Are you looking for a Magento REST API developer? Contact us and we will help you into that.

January 27, 2021
Did you like this post?