What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: 400 error when attempting to fulfill line item via API

400 error when attempting to fulfill line item via API

epodietz
Excursionist
13 2 1
In the node.js test script below, I am attempting to fulfill the second line item in an order via the Shopify API.
In debugger I am able to see that the order is successfully downloaded, but when
the fulfillment is attempted, I get a 400 error. Ideas?
===========================================================================================

Error: Response code 400 (Bad Request)

    at Request.<anonymous> (c:\nodejs\node_modules\got\dist\source\as-promise\index.js:118:42)

    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {name: 'HTTPError', code: 'ERR_NON_2XX_3XX_RESPONSE', timings: {…}, stack: 'HTTPError: Response code 400 (Bad Request)

  …ions (node:internal/process/task_queues:95:5)', message: 'Response code 400 (Bad Request)', …}

 ==========================================================================================
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
    shopName: 'xxxx.myshopify.com',
    accessToken:'xxxx'
});
async function fulfillSecondLineItem(orderId) {
  try {
    const order = await shopify.order.get(orderId);
    // Check if the order has at least two line items
    if (order.line_items && order.line_items.length >= 2) {
      const secondLineItem = order.line_items[1]; // Get the second line item
      // Create a fulfillment for the second line item
      const fulfillment = {
        order_id: orderId,
        line_items: [{ id: secondLineItem.id }] // Fulfill only the second line item
      };
      // Post the fulfillment to Shopify
      const response = await shopify.fulfillment.create(orderId, fulfillment);
      console.log('Fulfillment created:', response);
    } else {
      console.log('Order does not have enough line items.');
    }
  } catch (error) {
    console.error('Error:', error);
  }
}

fulfillSecondLineItem(orderId);
Replies 7 (7)

SomeUsernameHe
Shopify Partner
517 57 110

Adjust your logic SLIGHTLY and let me know: 

const fulfillment = {
  order_id: orderId,
  line_items: [{ 
    id: secondLineItem.id, 
    quantity: 1 // Assuming you want to fulfill 1 quantity of the second line item
  }]
};


Make sure to replace quantity: 1 with the appropriate quantity if you need to fulfill more than one unit of the product. Also, double-check that your orderId variable is correctly set and passed to the fulfillSecondLineItem function.

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
epodietz
Excursionist
13 2 1

Thanks for your suggestion. I tried it and still got a 400. I have verified that the order id is correct. I have to assume the line item id is correct since it was pulled from the line items downloaded with the order. Is the Shopify API supposed to supply any kind of error code or message that could serve as a clue? I burrowed through the error response but could not find anything meaningful (to me, at least).

const fulfillment = {

  "order_id": "5847711088977",

  "line_items": [

    {

      "id": 15197089562997,

      "quantity": 1

    }

  ]

}

 

Error: HTTPError: Response code 400 (Bad Request)

    at Request.<anonymous> (c:\nodejs\node_modules\got\dist\source\as-promise\index.js:118:42)

    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

{name: 'HTTPError', code: 'ERR_NON_2XX_3XX_RESPONSE', timings: {…}, stack: 'HTTPError: Response code 400 (Bad Request)

  …ions (node:internal/process/task_queues:95:5)', message: 'Response code 400 (Bad Request)', …}

SomeUsernameHe
Shopify Partner
517 57 110

To better understand the cause of the error, you can modify your error handling to log the complete error response from Shopify. This should include more detailed information about what's specifically wrong with your request. Here's how you can modify your catch block to log the complete error response:

catch (error) {
  if (error.response) {
    console.error('Error Response:', error.response.body); // Log the full error response
  } else {
    console.error('Error:', error);
  }
}

 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
epodietz
Excursionist
13 2 1

That was very helpful! The error message I got back was "location_id must be specified when creating fulfillments."

We only have one location, and in the downloaded order json, location_id was null. I am wondering where I am supposed to specify the fulfillment_id and if I should just specify null, or is there some config flag somewhere which indicates that location_id is not necessary?

 

Thx!

SomeUsernameHe
Shopify Partner
517 57 110

Awesome! I am super glad to hear it!

You can use the GET /admin/api/2023-10/locations.json or /admin/api/2024-01/locations.json (for the latest endpoint) to list all locations for your shop.
The response will include the ID for each location. If you only have one location, this will be the location_id you need.

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
epodietz
Excursionist
13 2 1

I used the GraphQL tool to get the shop location_id and put it in fulfillment, however I still got the same error ("location_id must be specified  when creating fulfillments".

fullfilment: { "order_id": "5847711088933", "line_items": [ { "id": 15197089562917, "quantity": 1, "location_id": "73224487205" } ] }

 

epodietz
Excursionist
13 2 1

Moving location_id from line item level to order level got rid of the "location_id must specified ..." error, but now results in a 404. However, I think I have some more homework to on learning how to do create fulfillments.

fulfillment: { "order_id": "5856379699493",  "location_id": "73224487205", "line_items": [ { "id": 15210794484005, "quantity": 1 }] }