0

I want to use different functionality depending on the Magento version. Is it possible to get version via REST API?

5

By default Magento there is no API endpoints to get the version.

Based on your needs, you can create new API class then inject ProductMetadataInterface or inject ProductMetadataInterface in existing class wherever needs.

public function __construct(
    \Magento\Framework\App\ProductMetadataInterface $productMetadata
) { 
    $this->productMetadata = $productMetadata; 
}

/**
 * Get Product version
 *
 * @return string
 */ 
public function getMagentoVersion() { 
    return $this->productMetadata->getVersion(); 
}

I hope you got solution to achieve your goal.

  • Are you confirming there is no default API endpoint for this purpose? – Dmitri Portenko Sep 20 at 14:42
  • @DmitriPortenko as my knowledge, by default there is no endpoints. – Sathish Sep 20 at 14:53
  • 2
    @DmitriPortenko Yeah, In Magento there is no default API endpoint for to get the version. And also you can easily check the version like this way www.website.com/magento_version. – Renga Sep 20 at 14:55
  • @DmitriPortenko Please note that we also have an option to disable this for some security reasons. bin/magento module:disable Magento_Version The following modules have been disabled: - Magento_Version – Renga Sep 20 at 15:41
  • thanks for the note, but I think any module can be disabled this way :) – Dmitri Portenko Sep 20 at 15:44
2

Summary of the proposed solutions.
1) From @sathish answer - I've created my own REST API. You can check the following commit or simply use extension.

2) From @renga comment, it is possible to get Magento version from www.website.com/magento_version. So I used the following code to get the Magento version in my React Native app.

const getMagentoVersion = async () => {
  try {
    const url = 'http://magento.base.url/';
    const response = await fetch(`${url}magento_version`);
    const text = await response.text();
    const number = parseNumber(text);
    console.log(text, number);
  } catch (error) {
    console.log(error);
  }
};

const parseNumber = (strg) => {
  strg = strg || '';
  let decimal = '.';
  strg = strg.replace(/[^0-9$.,]/g, '');
  if (strg.indexOf(',') > strg.indexOf('.')) decimal = ',';
  if ((strg.match(new RegExp(`\\${decimal}`, 'g')) || []).length > 1) decimal = '';
  if (decimal != '' && (strg.length - strg.indexOf(decimal) - 1 == 3) && strg.indexOf(`0${decimal}`) !== 0) decimal = '';
  strg = strg.replace(new RegExp(`[^0-9$${decimal}]`, 'g'), '');
  strg = strg.replace(',', '.');
  return parseFloat(strg);
};

getMagentoVersion();

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.