Shopify-App-JS Rest Client Session Undefined

Topic summary

Issue: Initializing Shopify Admin REST client throws TypeError: Cannot read properties of undefined (reading ‘accessToken’) inside @shopify/shopify-api/lib/clients/admin/rest/client.js.

Context:

  • A Session is constructed from database data: shop, scope (read_orders), isOnline: false, id, state, accessToken.
  • Console output of the resulting sessionCopy shows expected fields (including accessToken), plus onlineAccessInfo: undefined and expires: undefined.
  • Code causing the error: const restClient = new shopify.clients.Rest({sessionCopy});

Observation:

  • The error indicates the library is attempting to read accessToken from an undefined session object. Despite sessionCopy containing accessToken, the Rest client’s internal code sees session as undefined.
  • The call passes an object with a property named sessionCopy rather than a property named session; this may prevent the client from receiving the expected session object.

Status:

  • No resolution yet. The author asks whether they are creating the session incorrectly and points to client.js where session appears undefined.

Notes:

  • Code snippets are central to understanding the problem.
Summarized with AI on December 24. AI used: gpt-5.
const sessionCopy = new Session(sessionData); // Pulled from database.

// TODO: Getting error: TypeError: Cannot read properties of undefined (reading 'accessToken')
// at (/node_modules/@shopify/shopify-api/dist/cjs/lib/clients/admin/rest/client.js:27:50)

const restClient = new shopify.clients.Rest({sessionCopy});

Here’s the sessionData object:

{
  shop: 'X.myshopify.com',
  scope: 'read_orders',
  isOnline: false,
  id: 'offline_X.myshopify.com',
  state: '358222388575483',
  accessToken: 'shpua_XXXXXXXXXXXXXXXXXXXXXX'
}

And the sessionCopy objects, as logged in console:

{
  shop: 'X.myshopify.com',
  scope: 'read_orders',
  isOnline: false,
  id: 'offline_X.myshopify.com',
  state: '358222388575483',
  accessToken: 'shpua_XXXXXXXXXXXXXXXXXXXXXX',
  onlineAccessInfo: undefined,
  expires: undefined
}

Calling “const restClient = new shopify.clients.Rest({sessionCopy});” triggers the error “Cannot read properties of undefined (reading ‘accessToken’)”. Diving into the library’s “client.js” it appears the session object is undefined.

Am I creating the session wrong?