Passing a variable as BigInt to create a new order

I’m trying to create an order with the new 40.0 API and I keep getting error with the baseMoney.amount field. I am passing a number like 3500 from a cart object and it’s keeps giving me an error. I have tried passing it as a string, a number and BigInt but nothing works. I can it work to in the API explorer no problem.

Even if I hard code the amount like 3500 I get an error. If I pass BigInt(3500) it says “Expected an integer”. I even tried passing amount:3500n

(BigInt.prototype as any).toJSON = function() {
return this.toString();
};

export async function createSquareOrder (cart) {
  
  const result = await orders.create({
    order: {
      locationId: process.env,
      referenceId: cart.id,
      lineItems: [
        {
          uid: cart.ticket_id,
          name: cart.item_name,
          quantity: cart.quantity.toString(),
          variationName: 'Ticket Sale',
          itemType: 'ITEM',
          basePriceMoney: {
            amount: BigInt(cart.price),
            currency: 'USD'
          }
        }
      ],
      pricingOptions: {
        autoApplyTaxes: false
      }
    },
    idempotencyKey: randomUUID()
  });
  return { success: true, orderId: result.order!.id };
};

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Orders API: How It Works
Create Orders
https://developer.squareup.com/docs/orders-api/quick-start/start

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

Hey @brianmaggi! Can you verify if you’ve fully updated the SDK to 40.0.0? I just tested the latest Node.js SDK version with this code copied directly from API Explorer, and the request succeeded as expected:

async function main() {
    const client = new SquareClient({
        token: "ACCESS TOKEN HERE",
    });
    await client.orders.create({
        idempotencyKey: "28ef5446-fd05-4e14-a40e-6fa246e70942",
        order: {
            locationId: "L5AT2F5E5116J",
            referenceId: "Reference123",
            lineItems: [
                {
                    uid: "order_uid_123",
                    name: "Test",
                    quantity: "1",
                    variationName: "Test Variation",
                    itemType: "ITEM",
                    basePriceMoney: {
                        amount: BigInt(3500),
                        currency: "USD",
                    },
                },
            ],
            pricingOptions: {
                autoApplyTaxes: false,
            },
        },
    });
}
main();

If you’re sure that you’re on 40.0.0, is it possible that the value you’re passing in is getting malformed?

Josh, thanks for the quick response I appreciate it. I am running 40.0.0 → in my package.json I have "square": "^40.0.0", I still have the legacy Square package installed.

import { Client } from "square/legacy";
import { SquareClient, SquareEnvironment } from "square";

I even tried hard coding data similar to what you’d submit in the API Explorer. Note amount: BigInt(3500) looks the same. So I don’t think it’s malformed.

I also have included the following function from Square blog (Accept payments with Square using Next.js App Router).

BigInt.prototype.toJSON = function () {
  return this.toString();
};

That function doesn’t work in TypeScript by the way. So I tried this and it didn’t help either.

(BigInt.prototype as any).toJSON = function() {
  return this.toString(); 
};
export async function createSquareOrder () {
  const result = await orders.create({
    order: {
      locationId: env.process,
      referenceId: "ud_123",
      lineItems: [
        {
          uid: "cart.ticket_id",
          name: "cart.item_name",
          quantity: "1",
          variationName: 'Ticket Sale',
          itemType: 'ITEM',
          basePriceMoney: {
            amount: BigInt(3500),
            currency: 'USD'
          }
        }
      ],
      pricingOptions: {
        autoApplyTaxes: false
      }
    },
    idempotencyKey: randomUUID()
  });
  return { success: true, orderId: result.order!.id };
};

Here’s the error I get…

Body: {
  "errors": [
    {
      "code": "EXPECTED_INTEGER",
      "detail": "Expected an integer value (line 1, character 221)",
      "field": "order.line_items[0].base_price_money.amount",
      "category": "INVALID_REQUEST_ERROR"
    }
  ]
}

I haven’t narrowed it down to a single problem. I think it might have something to do with my local server getting 500 errors upstream of the function to Square. So maybe it is malformed data.

Got it, yeah malformed data would fit the symptoms that we’re seeing. I’d also recommend trying to remove the legacy Square package entirely and test with only the non-legacy package imported: import { SquareClient, SquareEnvironment } from "square";

Doesn’t hurt to rule it out entirely in case there’s some wires getting crossed there accidentally!

1 Like

I suspect that the BigInt conversion is the problem here. This bit of code:

(BigInt.prototype as any).toJSON = function() {
  return this.toString(); 
};

With version 40 of the SDK, this should no longer be necessary and I think it is causing some wires to be crossed. Can you try removing that entirely? If you’re curious about the internal here, we essentially have a helper function now that handles roundtripping of bigint values. square-nodejs-sdk/src/core/json.ts at 5fd4e025be72a61d07174c0de4c94acc329969d6 · square/square-nodejs-sdk · GitHub

We’ll need to go through and update our documentation to reflect this!

Josh

I think 3 different things were happening which made it hard to troubleshoot. I had the legacy package still installed, there was a downstream unrelated bug 500 errors, and I had that BigInt function / workaround. All three have been removed and it works great now.

Thanks
Brian

Mike

In my last reply, that was one of the 3 things I took out and now everything is working. I do have to convert the two different numbers to strings first to work. But that’s no big deal.

quantity: BigInt(String(cart_item.price)),
amount: BigInt(String(cart_item.price)),