@bates-solutions/squareup API Reference - v3.2.0
    Preparing search index...

    Class SquareOAuthClient

    OAuth client for the application-credential endpoints: exchanging an authorization code, refreshing, and revoking tokens.

    These endpoints authenticate with the application's clientId / clientSecret, not a merchant token — which is why this is separate from SquareClient. To check a merchant token's status, use createSquareClient({ accessToken }).oauth.tokenStatus().

    const oauth = createSquareOAuthClient({
    clientId: process.env.SQUARE_APP_ID!,
    clientSecret: process.env.SQUARE_APP_SECRET!,
    environment: 'production',
    });

    // In the redirect handler, after verifying `state`: require a stored value,
    // delete it (one use), then compare it with the query value. Then handle a
    // deny (`error=access_denied`, no `code`) and require `code` to be a string.
    // See docs/guides/core/oauth.md.
    const tokens = await oauth.obtainToken({ code });
    Index
    • Exchange the authorization code from the OAuth redirect for tokens.

      Parameters

      • options: { code: string; redirectUri?: string }
        • code: string

          code query parameter from the redirect (valid for 5 minutes)

        • OptionalredirectUri?: string

          Required if the authorize URL included one; must match

      Returns Promise<OAuthTokens>

      // After the state check (see docs/guides/core/oauth.md)
      if (typeof req.query.error === 'string') return res.redirect('/settings?connect=denied');
      if (typeof req.query.code !== 'string') throw new Error('OAuth code missing');

      const { accessToken, refreshToken, expiresAt, merchantId } =
      await oauth.obtainToken({ code: req.query.code });
    • Get a new access token using a refresh token. Run this on a schedule well before expiresAt — Square recommends every 7 days or less.

      Parameters

      • options: { refreshToken: string }

      Returns Promise<OAuthTokens>

      const refreshed = await oauth.refreshToken({ refreshToken: stored.refreshToken });
      
    • Revoke this application's access to a merchant. Pass the merchant's merchantId to revoke every token issued to them, or an accessToken to revoke that token (and its merchant's other tokens).

      Parameters

      • options:
            | {
                accessToken?: undefined;
                merchantId: string;
                revokeOnlyAccessToken?: undefined;
            }
            | {
                accessToken: string;
                merchantId?: undefined;
                revokeOnlyAccessToken?: boolean;
            }
        • {
              accessToken?: undefined;
              merchantId: string;
              revokeOnlyAccessToken?: undefined;
          }
          • OptionalaccessToken?: undefined
          • merchantId: string
          • OptionalrevokeOnlyAccessToken?: undefined

            Revoke only accessToken, leaving the refresh token and the authorization intact

        • { accessToken: string; merchantId?: undefined; revokeOnlyAccessToken?: boolean }
          • accessToken: string
          • OptionalmerchantId?: undefined
          • OptionalrevokeOnlyAccessToken?: boolean

            Revoke only accessToken, leaving the refresh token and the authorization intact

      Returns Promise<void>

      await oauth.revokeToken({ merchantId: tenant.squareMerchantId });