How can I load a menu programmatically with an active trail set by a provided URL?
Inject the menu.active_trail dependency along with menu.link_tree:

private $activeTrail;

/**
 * {@inheritdoc}
 */
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $active_trail, EntityTypeManagerInterface $entity_type_manager, Request $current_request) {
  parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);

  $this-&gt;menuTree = $menu_tree;
  $this-&gt;activeTrail = $active_trail;
  $this-&gt;entityTypeManager = $entity_type_manager;
  $this-&gt;request = $current_request;
  $this-&gt;alias = $this-&gt;request-&gt;query-&gt;get('alias');
}

/**
 * {@inheritdoc}
 */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  return new static(
    $configuration,
    $plugin_id,
    $plugin_definition,
    $container-&gt;getParameter('serializer.formats'),
    $container-&gt;get('logger.factory')-&gt;get('rest'),
    $container-&gt;get('menu.link_tree'),
    $container-&gt;get('menu.active_trail'),
    $container-&gt;get('entity_type.manager'),
    $container-&gt;get('request_stack')-&gt;getCurrentRequest()
  );
}


Now instead of:

$tree = $this-&gt;menuTree-&gt;load('menu_id', new MenuTreeParameters());


get the active trail ID's just before building your menu and pass it in as part of the parameters:

// Change this to menu machine name.
$menu_id = 'menu_id';

// This should return an array. 
// Array contains menu link uuid's, keyed by the uuid's iirc. 
// For example something like:
//   'menu_link:abcdefg_123' =&gt; 'menu_link:abcdefg_123'
$activeTrailIds = $this-&gt;activeTrail-&gt;getActiveTrailIds($menu_id); 

$params = new MenuTreeParameters();
$params-&gt;setActiveTrail($activeTrailIds);
$tree = $this-&gt;menuTree-&gt;load($menu_id, $params);


This should return you a menu tree with correct active trails.