How to find the order with the nearest due date in Shopify Admin API?

Hi @Ph_m_Th_Thu_Ha! The orders query doesn’t have a built-in way to sort by payment due dates, the available sort keys cover things like created date and financial status, but not payment terms.

You’ll need to fetch the orders with payment terms and sort client-side by the dueAt field from payment schedules:

query {
  orders(first: 50, query: "financial_status:pending OR financial_status:partially_paid") {
    nodes {
      id
      name
      displayFinancialStatus
      paymentTerms {
        paymentSchedules(first: 5) {
          nodes {
            dueAt
            balanceDue {
              amount
              currencyCode
            }
          }
        }
      }
    }
  }
}

Once you have the results, filter out orders without payment terms and sort by the dueAt timestamp in your application code to find the order with the nearest due date. Keep in mind that not all orders will have payment terms, only those with deferred payment options will have this data.