2

I'm using the 'official' Shopify node adapter (https://github.com/MONEI/Shopify-api-node) and trying to create a draft order.

Sending the payload of

{
  "draft_order": {
    "line_items": [
      {
        "title": "Custom Tee",
        "price": "20.00",
        "quantity": 2
      }
    ]
  }
}

works via Postman, but is returning 'bad request' (400) from the api.

The full function/call via wrapper is as follows:

import Shopify from 'shopify-api-node';

makeDraftOrder: function(shop_name) {
    console.log('trying to connect with ', shop_name);
    const shop = Shops.findOne({'shopName': shop_name});

    const shopify_data = new Shopify({
      shopName: shop.shopName,
      accessToken: shop.accessToken
    });

    let newOrder = JSON.stringify({
      "draft_order": {
        "line_items": [
          {
            "title": "Custom Tee",
            "price": "20.00",
            "quantity": 2
          }
        ]
      }
    });

    shopify_data.draftOrder.create(newOrder).then(data => {
      console.log('draft order', data);
    }).catch(err => console.error('wawawoowa', err));
  }

Making a call to draftOrder.list() works fine, but the above fails. Any help much appreciated.

aroundtheworld
  • 731
  • 1
  • 5
  • 15

1 Answers1

3

Don't wrap the order with "draft_order": { ... }. You also don't need to stringify the object.

let newOrder= {
  "line_items": [
    {
      "title": "Custom Tee",
      "price": "20.00",
      "quantity": 2
    }
  ]
};

shopify_data.draftOrder.create(newOrder)
Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • nice, thank you. can you tell me how you knew to remove both of those? – aroundtheworld Oct 13 '17 at 05:17
  • I had the same problem some time ago. I figured it out from the source code of `shopify-api-node`. Here you can see a line that wraps your order with `"draft_order": { ... }` internally: https://github.com/MONEI/Shopify-api-node/blob/2.9.0/index.js#L116. The `key` is set here: https://github.com/MONEI/Shopify-api-node/blob/2.9.0/resources/draft-order.js#L18 – Lukasz Wiktor Oct 13 '17 at 06:41
  • As of `JSON.stringify`, `shopify-api-node` uses `got` module to make requests. It has a [json](https://www.npmjs.com/package/got#json) mode option which is set to true here: https://github.com/MONEI/Shopify-api-node/blob/2.9.0/index.js#L101 so you have to pass a plain object and it gets stringified to JSON under the hood. – Lukasz Wiktor Oct 13 '17 at 06:44
  • @LukaszWiktor Can you please answer this https://stackoverflow.com/questions/47432209/shopify-get-shop-domain-inside-a-app –  Nov 22 '17 at 14:42