Bootstrap and require a method from a separate module I have a script that runs outside of my module, which bootstraps Drupal.

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\smart_ip\SmartIp;

chdir('../..');

$autoloader = require_once 'autoload.php';

$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel-&gt;boot();

$ip = $request-&gt;getClientIP();

$location = SmartIp::query($ip);


My problem is that using the Smart IP query method (SmartIp::query($ip)) throws an error:

TypeError: Argument 1 passed to Drupal\Core\Session\SessionConfiguration::getOptions() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in /var/www/drupalvm/sitelog/core/lib/Drupal/Core/Session/SessionManager.php on line 111 in Drupal\Core\Session\SessionConfiguration-&gt;getOptions() (line 40 of /var/www/drupalvm/sitelog/core/lib/Drupal/Core/Session/SessionConfiguration.php)
#0 /var/www/drupalvm/sitelog/core/lib/Drupal/Core/Session/SessionManager.php(111): Drupal\Core\Session\SessionConfiguration-&gt;getOptions(NULL)
#1 /var/www/drupalvm/sitelog/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php(278): Drupal\Core\Session\SessionManager-&gt;start()
#2 /var/www/drupalvm/sitelog/vendor/symfony/http-foundation/Session/Session.php(87): Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage-&gt;getBag('attributes')
#3 /var/www/drupalvm/sitelog/modules/smart_ip/src/SmartIp.php(100): Symfony\Component\HttpFoundation\Session\Session-&gt;get('smart_ip')
#4 /var/www/drupalvm/sitelog/modules/smart_ip/src/SmartIpLocation.php(167): Drupal\smart_ip\SmartIp::getSession('smart_ip')
#5 /var/www/drupalvm/sitelog/modules/smart_ip/src/SmartIpLocation.php(133): Drupal\smart_ip\SmartIpLocation-&gt;getData(false)
#6 /var/www/drupalvm/sitelog/core/lib/Drupal/Component/DependencyInjection/Container.php(264): Drupal\smart_ip\SmartIpLocation-&gt;__construct()
#7 /var/www/drupalvm/sitelog/core/lib/Drupal/Component/DependencyInjection/Container.php(177): Drupal\Component\DependencyInjection\Container-&gt;createService(Array, 'smart_ip.smart_...')
#8 /var/www/drupalvm/sitelog/core/lib/Drupal/Component/DependencyInjection/Container.php(494): Drupal\Component\DependencyInjection\Container-&gt;get('smart_ip.smart_...', 1)
#9 /var/www/drupalvm/sitelog/core/lib/Drupal/Component/DependencyInjection/Container.php(236): Drupal\Component\DependencyInjection\Container-&gt;resolveServicesAndParameters(Array)
#10 /var/www/drupalvm/sitelog/core/lib/Drupal/Component/DependencyInjection/Container.php(177): Drupal\Component\DependencyInjection\Container-&gt;createService(Array, 'smart_ip.get_lo...')
#11 /var/www/drupalvm/sitelog/core/lib/Drupal.php(158): Drupal\Component\DependencyInjection\Container-&gt;get('smart_ip.get_lo...')
#12 /var/www/drupalvm/sitelog/modules/smart_ip/src/SmartIp.php(36): Drupal::service('smart_ip.get_lo...')
#13 /var/www/drupalvm/sitelog/modules/sitelog/sitelog.php(23): Drupal\smart_ip\SmartIp::query('192.168.88.1')
#14 {main}.


I hoped that by putting use Drupal\smart_ip\SmartIp;, at the top of my script, I would have access to the module's methods, but I don't.

I don't understand what is required to make the Smart IP query function available to my script?
For this case you have to add one additional step to your bootstrap code.

Put $kernel-&gt;preHandle($request); right below the $kernel-&gt;boot();