0

For context, I am requesting via node-fetch the Shopify Admin REST API orders.json with the queries shown in my code. For some reason, this only returns the latest order regardless of if I add status=any or limit=250 (which is the standard limit for orders). Before I go too much into detail let me show my code so you can understand the query.

const User = require('../models/User.model');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

const getReportsPartner = async (req, res, next) => {
    const user = await User.findById(req.user.id)
    const url = 'https://astore.com/admin/orders.json?fields=total_price_usd,line_items';
    const headers = {
        'X-Shopify-Access-Token': 'anaccesstoken',
        'Content-type': 'application/json',
        'Authorization': 'anauthkey'
    }

    fetch(url, { method: 'GET', headers: headers })
        .then(res => res.json())
        .then(json => {
            const orders = json.orders[0]
            console.log(orders)
            const partner = orders.line_items[0].title
            const commission = (orders.total_price_usd * .125).toFixed(2)
            res.render('reports/partnerReport', { partner: partner, commission: commission })
        })
}

module.exports = { getReportsPartner }

I have attempted modifying the URL in a few ways other than status and limit. I will list some of them below: https://astore.com/admin/orders.json?fields=total_price_usd,line_items.title=something https://astore.com/admin/orders.json?fields=total_price_usd,line_items&line_items.title=something

I have also tried line_items[0].title=something in the url as well, however this seems to invalidate the entire request. I appreciate any help you can give. If you need more in terms of context let me know. Ultimately I plan to get recent orders by title and graph the orders and commissions in a report format. For future orders I will be using a webhook, so at the moment I am only worried about past.

Matt Nyce
  • 57
  • 9
  • Are you building a private app or a public app? One thing I can clearly see is your authorization header is wrong. Second, there is no query or filter to fetch only `line_items.title`. By adding this the query is invalidated and you are receiving the default response for the API endpoint. – HymnZzy Mar 09 '22 at 17:35
  • Hello, I've decided to go with a different approach since then. Unfortunately I have found there to be no query to grab just the line_items title as you stated. This will be a private application. My headers are wrong to not post sensitive data. – Matt Nyce Mar 09 '22 at 21:37

1 Answers1

1

As HymnZzy mention in the comments, it is not possible to query for only line_items title. What I have decided to do is make requests for the last 250 orders or from a certain date range. Here is an example url with query.

`https://${yourshop}.com/admin/api/2022-01/orders.json?limit=250&status=any&financial_status=paid&fields=line_items,total_price_usd`

Then I loop through the results and do something with each order with a certain title.

for (let i=0; i<json.orders.length; i++) {
              try {
              let title = json.orders[i].line_items[0].title
              let price = json.orders[i].total_price_usd
              /* Do Something with title and price here */
            } catch (err) {
              console.log(err)
            }
          } 

My only issue is the speed of this method, however it seems Shopify has limited us to something like this if we would like to find orders by line_items title via the REST Admin API.

Matt Nyce
  • 57
  • 9