How to retrieve Masked id from the cart id Magento 2?

Retrieve Masked id (guest cart hash) from the cart id in Magento 2, You required the cart quote id.

masked_id is stored in the quote_id_mask table, it’s the randomly generated string for the guest customer quote in Magento 2.

You can retrieve masked id string by using an interface,

namespace Jesadiya\QuoteId\Model;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;

class MaskedHash
     * @var QuoteIdToMaskedQuoteIdInterface
    private $quoteIdToMaskedQuoteId;

    public function __construct(
        QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
    ) {
        $this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;

     * get Masked id by Quote Id
     * @return string|null
     * @throws LocalizedException
    public function getQuoteMaskId($quoteId)
        $maskedId = null;
        try {
            $maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('Current user does not have an active cart.'));

        return $maskedId;

Call method,

$quoteId = 1;
echo $maskedId = $this->getQuoteMaskId($quoteId);

If cart id has available masked id it will return the masked hash string as a result.

On Error,

The cart has no masked hash, It will throw an error, the current user does not have an active cart.