Orders API Cryptic Error

Hi and thanks for setting up this forum for helping developers.

I seem to be having an issue creating an order with pickup fulfillment and the error I’m getting is, unfortunately, providing no help. Please see below for all the details (in JavaScript/JSON) and thank you for reading.

POST Request:

const squareOrder = {
  fulfillments: [
    {
      pickup_details: {
        recipient: { display_name: name, phone_number },
        schedule_type: "ASAP",
        note: notes,
      },
      state: "COMPLETED",
      type: "PICKUP",
      uid: id,
    },
  ],
  line_items: manifest_items.map((mi) => {
    const { name, quantity, size, additional_data } = mi;
    return {
      quantity: `${quantity}`,
      name,
      note: `${additional_data}`,
      variation_name: size,
    };
  }),
  reference_id: id,
  source: `${provider} ${integration_name}`,
};

const data = {
  idempotency_key: id,
  order: squareOrder,
};

axios.post(
  `https://connect.squareupsandbox.com/v2/locations/${location_id}/orders`,
  data,
  {
    headers: {
      Authorization: `Bearer ${access_token}`,
      "Square-Version": "2020-04-22",
      "Content-Type": "application/json",
    },
  }
)

Error:

[
  {
    code: 'BAD_REQUEST',
    detail: 'Expected "{" (line 1, character 391)',
    category: 'INVALID_REQUEST_ERROR'
  }
]

Hopefully, this code will be formatted correctly in my post and the error will be an instant light switch for someone because I have no idea what it could possibly be. Thank you.

Hi @adar welcome to the forums!

This error is usually due to just incorrectly formatted data. I think in this case, you need to encode it as JSON, rather than an object as you’re currently doing. Could you try doing JSON.stringify(data) instead of just data and see if that works?

I believe axios does convert data objects to strings, but luckily my colleague figured out that it was because my source property was a string instead of an object with a name property. Sorry about that.

1 Like