I've written the following function

public function getImg($sku){

    return $image;

as an api method and it returns me the name of the image. how can I get an image to be used and viewed in mobile app by rest api

1 Answer 1


You need to get media url first then concat this before image url. You can get media url from below code

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager

) {
    $this->storeManager = $storeManager;
public function getMediaUrl() {
   $mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Let me know if you need further assistance.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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