---
title: "Expiring offline tokens"
description: "Automatic migration, refresh, and secure persistence of Shopify offline credentials."
canonical_url: "https://shopify.yan.ad/guides/expiring-offline-tokens"
---
# Expiring offline tokens

> Automatic migration, refresh, and secure persistence of Shopify offline credentials.

The module requests **expiring offline tokens by default** in embedded ID-token exchanges and OAuth callbacks. This is not a Remix/React Router `future` flag: issuance and refresh are implemented by this module using `@shopify/shopify-api`.

## Lifecycle

- New offline acquisitions request `expiring: true`.
- Existing non-expiring sessions migrate through the SDK's `migrateToExpiringToken` before Admin API use. Shopify permanently retires the old token; the returned pair must be saved.
- Offline sessions refresh when their access-token expiry is within 60 seconds. The module uses expiry values returned by Shopify, not assumed token lifetimes.
- Both rotated tokens and expiry dates are saved before creating the Admin API client. Refresh does not run `afterAuth` hooks.
- Expired or rejected refresh credentials require reauthentication. Embedded requests can reacquire a pair from their verified ID token; standalone/background callers receive a 401 and must reopen or authorize the app.
- Transport and storage failures are not treated as permission failures and do not clear stored credentials.

Refresh runs before `admin.graphql` calls, including contexts used by Flow and fulfillment requests. `useShopifyUnauthenticatedAdmin` also resolves a current offline session before returning it. Direct SDK clients you create yourself must manage token freshness themselves.

## Session storage requirements

Persist the complete SDK Session, including:

<table>
<thead>
  <tr>
    <th>
      Field
    </th>
    
    <th>
      Storage requirement
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        accessToken
      </code>
    </td>
    
    <td>
      Secret; store securely.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        expires
      </code>
    </td>
    
    <td>
      Restore as a JavaScript <code>
        Date
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        refreshToken
      </code>
    </td>
    
    <td>
      Secret; replace on every successful renewal.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        refreshTokenExpires
      </code>
    </td>
    
    <td>
      Restore as a JavaScript <code>
        Date
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        id
      </code>
      
      , <code>
        shop
      </code>
      
      , <code>
        isOnline
      </code>
      
      , <code>
        scope
      </code>
      
      , <code>
        state
      </code>
    </td>
    
    <td>
      Preserve normal session fields.
    </td>
  </tr>
</tbody>
</table>

Update your database schema and adapter before deploying this migration. `storeSession` must return `true` only after the entire credential pair is durably saved. Protect tokens at rest and never put them in browser runtime config, logs, or API responses. The default memory adapter is suitable for development, not production durability.

## Concurrent servers

The module deduplicates acquisitions, migrations, and refreshes per session **within one process**. Multi-worker/serverless deployments need distributed coordination around the whole load → issue/refresh → persist operation for each shop. A lock only inside `storeSession` is insufficient. Do not run an independent refresh scheduler concurrently with module token acquisition without the same shared lock.

For dormant stores, schedule coordinated renewal before refresh-token expiry if continued background access is required. The module does not create a scheduler automatically.

## Verify the migration

Restart the app, reopen it inside Shopify admin, and retry a minimal Admin API request. A legacy installation should now store `expires`, `refreshToken`, and `refreshTokenExpires`; do not print the credential values to verify them. Check Shopify Dev Dashboard logs for remaining non-expiring offline requests.

See Shopify's [migration guide](https://shopify.dev/docs/apps/build/authentication-authorization/migrate-to-expiring-offline-access-tokens) and [token refresh documentation](https://shopify.dev/docs/apps/build/authentication-authorization/implement-token-exchange#refresh-an-expiring-offline-token).


## Sitemap

See the full [sitemap](https://shopify.yan.ad/sitemap.md) for all pages.
