How to Check Website code is valid or not Magento 2?

When you are working for the process of Import product in Magento 2 and You are not aware about website code is exists or not. You can check it from admin panel or programmatic way to check code availability.

You can check the website code from the database table store_website also.

Database Table persist the Website code:

Optional: You can also create a proxy of StoreManagerInterface in your di.xml file because the interface is resource hungry.

namespace Jesadiya\Website\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoreManagerInterface;

class WebsiteValid
     * @var StoreManagerInterface
    private $storeManager;

     * @var LoggerInterface
    private $logger;

    public function __construct(
        LoggerInterface $logger,
        WebsiteRepositoryInterface $websiteRepository
    ) {
        $this->logger = $logger;
        $this->countryFactory = $countryFactory;

     * Verify website by code
     * @param string $code
     * @return string|null
    public function isWebsiteCodeIsValid(string $code): ?string
        $websiteCode = null;
        try {
            $websiteData = $this->storeManager->getWebsite($code);
            $websiteCode = $websiteData->getId();
        } catch (LocalizedException $localizedException) {

        return $websiteCode;

Now, Call the method from the Model file,

$code = “us_website”;
echo $isWebsiteCodeValid = $this->isWebsiteCodeIsValid($code);

If Website code is valid return website id.
If website code is invalid return null.