How do I make a call to that URL that captures the User ID, using Magento customer Token in magento 2 rest API?

How Magento 2 makes calls in-between for the APIs ?

  • you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter> Sep 17 '18 at 15:04
  • 1
    it worked!! gives me customer id based on token. Thanks! Sep 17 '18 at 15:14
  • You can mark as right if it's working fine for you, it may help future readers. Sep 17 '18 at 15:15

How magento 2 make calls in-between for the APIs ?

Magento gets the customer data with passing anything except token value in API call.

In between magento calls some API related controllers for the same


<route url="/V1/customers/me" method="GET">
        <service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
            <resource ref="self"/>
            <parameter name="customerId" force="true">%customer_id%</parameter>

in this API call magento get customer data based on token.

Based on API url url="/V1/customers/me" magento calls function from

Magento calls In-between call of dispatch function.


 public function dispatch(\Magento\Framework\App\RequestInterface $request)
     /** In between code **/
     // In the same file

 protected function processApiRequest()
      $inputParams = $this->getInputParamsResolver()->resolve();

And this resolve() function calls override() function


 public function resolve()
      $inputData = $this->paramsOverrider->override($inputData, $route->getParameters());

and overide() function calls getOverriddenValue() function


 public function override(array $inputData, array $parameters)
      $value = $this->paramOverriders[$paramValue]->getOverriddenValue();

getOverriddenValue() calls ParamOverriderInterface


  • To Override parameter values
  • Parameters in the webapi.xml can be forced. This ensures that on specific routes, a specific value is always used.
  • For instance, if there is a ".../me/..." route, the route should use only user information specific to the
  • currently logged in user. More specifically, if there was a "/customers/me/addresses" route, the service method
  • invoked could have a signature of "getAddresses($customerId)", but in the webapi.xml, the $customerId parameter
  • would be forced to be the customer id of the current authenticated user.
  • The forced override parameter configuration is in the webapi.

     <parameter name="customer.id" force="true">%customer_id%</parameter>

and for business logic of this function is in file -


    * {@inheritDoc}
   public function getOverriddenValue()
       if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
           return $this->userContext->getUserId();
   return null;

So this is how Magento get Customer Id between API calls !!

Another solution

$ch = curl_init('dev.magento2.com/rest/V1/customers/me');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

//execute post
$result = curl_exec($ch);

//close connection

$json = json_decode($result);
echo $json->id;

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.