Is it possible to programmatically add a path alias to a programmatically created node?
To create the alias, I first check if the alias was set in the user interface, and if it is empty then I set it manually using the label().

use Drupal\Core\Entity\EntityInterface;

/**
 * hook_entity_insert
 */
function mymodule_entity_insert(EntityInterface $entity) {
    // Convert to lowercase, trim spaces, replace spaces with underscore and then truncate to 60 chars.
    $pathTitle = substr(str_replace(" ","_",trim(strtolower($entity-&gt;label()))),0,60);
    if ($entity-&gt;toArray()['path'][0]['alias'] == "") {
        $path = \Drupal::service('path.alias_storage')-&gt;save("/node/" . $entity-&gt;id(), "/mynewpath/" . $pathTitle, "en");
    }
}


https://www.drupal.org/node/1853148 for more information