Node.js SDK Migration Guide

Version 40.0.0 of the Node.js SDK represents a full rewrite of the SDK with a number of breaking changes, outlined in the following sections. When upgrading to this version or later versions, take note of the changes in the SDK, including client construction and parameter names. If necessary, you can use the legacy version of the SDK along with the latest version.

SDK versions 39.1.0 and earlier continue to function as expected, but you should plan to update your integration as soon as possible to ensure continued support.

Link to section

Client construction

The new Client class has some breaking changes and is constructed in a slightly different way than the legacy version of the SDK.

LegacyNewAdditional information
ClientSquareClientThe main client class for interacting with Square APIs.
EnvironmentSquareEnvironmentAn enum for specifying the environment (Sandbox or Production).
bearerAuthCredentials.accessTokentokenThe access token used for authentication.
customUrlbaseUrlThe base URL for API requests.
squareVersionversionThe Square API version to use.
N/AfetcherProvide your own fetch function for making HTTP requests.
timeoutRemovedTimeout can be set at individual methods.
additionalHeadersRemovedAdditional headers can be set at individual methods.
httpClientOptionsRemovedHTTP options can be set at individual methods.
unstable_httpClientOptionsRemovedThis option has been removed.
userAgentDetailRemovedThis option has been removed.
// LEGACY import { Client, Environment } from "square/legacy"; const client = new Client({ bearerAuthCredentials: { accessToken: process.env.SQUARE_ACCESS_TOKEN, }, environment: Environment.Sandbox, }); // NEW import { SquareClient, SquareEnvironment } from "square"; const client = new SquareClient({ token: process.env.SQUARE_ACCESS_TOKEN, environment: SquareEnvironment.Sandbox, });
Link to section

Use the legacy and new SDK side by side

While the new SDK has many improvements, we understand that it takes time to upgrade when there are breaking changes. To make the migration easier, the new SDK exports the legacy SDK as square/legacy. The following example shows how to use the new and legacy SDKs inside a single file:

Square recommends migrating to the new SDK using the following steps:

  1. Upgrade the NPM module to ^40.0.0.

  2. Search and replace all requires and imports from "square" to "square/legacy".

    • For required, replace require("square") with require("square/legacy").
    • For imports, replace from "square" with from "square/legacy".
    • For dynamic imports, replace import("square") with import("square/legacy").
  3. Gradually introduce methods from the new SDK by importing them from "square".

Note

If you're using TypeScript, make sure that the moduleResolution setting in your tsconfig.json is equal to node16, nodenext, or bundler to consume the legacy SDK.

Link to section

Named parameters

The previous version of the SDK relied on many positional parameters, which made it difficult for readers to understand the meaning of the parameter. For example:

// LEGACY await client.customersApi.listCustomers(undefined, 10, undefined, "DESC");

It's not immediately clear what undefined, 10, undefined, and DESC mean. In the new version of the SDK, you specify the parameters by their name and you're not required to pass undefined parameters. The same ListCustomers request looks like the following:

// NEW await client.customers.list({ limit: 10, sortOrder: "DESC", });
Link to section

API classes and method name changes

Previously, API classes were named after the resource you're interacting with and suffixed with Api. To interact with customer resources, you used the client.customersApi class.

The method was named based on the action you want to perform and the same resource name specified earlier. To list customers, you call client.customersApi.listCustomers.

// LEGACY await client.customersApi.listCustomers();

The new API classes are named by the resource without the Api suffix, and the methods now name the action without mentioning the resource a second time.

// NEW await client.customers.list();

When interacting with a nested resource, you can chain the resources using dot-notation.

// NEW await client.customers.groups.list();

To find all classes and method names that might have changed, see reference.md.

Link to section

Auto-pagination

Paginated endpoints now support auto-pagination with an async iterator. Callers don’t need to manually fetch the next page at all. You can now iterate over the entire list and the client automatically fetches the next page behind the scenes.

The following example shows the same code, with the legacy and new SDK:

If you need to paginate page by page, you can still do so with a lot less code:

  • Use customerPager.data to get the customers of the current page.
  • Use customerPage.hasNextPage() to check whether there's another page after the current one.
  • Use customerPage.getNextPage() to retrieve the next page of customers.
Link to section

Next steps

For more sample code and examples of using the new Node.js SDK, see Common Square API Patterns.