How to add a menu link to a menu programmatically?
I was using the wrong namespace.
Use the following namespace

use Drupal\menu_link_content\Entity\MenuLinkContent;


and then the following code will work.

MenuLinkContent::create([
      'title' =&gt; 'Example link',
      'link' =&gt; ['uri' =&gt; 'internal:/node'],
      'menu_name' =&gt; 'main',
    ])-&gt;save();