How get session object in a non request context

Topic summary

A developer working with Shopify webhooks needed to access a session object outside of a typical request context—specifically within a webhook handler class.

The Problem:

  • When handling webhook events (like PRODUCTS_CREATE), there’s no direct request context to retrieve a session from
  • The goal was to perform Shopify API calls from within the webhook handler

The Solution:

  • Since the $shop variable is available in the webhook handler, the developer can load an offline session using:
$session = Utils::loadOfflineSession($shop);
  • This allows creating a REST client and making API calls without needing an active request context

Status: Resolved—the developer found a working solution using Shopify’s offline session loading utility.

Summarized with AI on November 18. AI used: claude-sonnet-4-5-20250929.

let say we are in a Handler class of a webhook

class ProductsHandler implements Handler
 
{
public function handle(string $topic, string $shop, array $body): void{
 switch ($topic) { 
case Topics::PRODUCTS_CREATE: {
// how to get a session instance here so we can perform shopify api call
//
//  $session = ?
//$client = new Rest($session->getShop(), $session->getAccessToken());
 
 
}
 }
 }
}

i found a solution

as we already have the $shop variable so we can do this

$session = Utils::loadOfflineSession($shop);