Ordering Results of Order API

Solved

Ordering Results of Order API

michaelm
Shopify Partner
10 0 8

Hi!

 

I'm currently building a product requires syncing a Shopify customer's orders to our database. In order to do this, I need to pull all orders using the Order API, and then every day get all the new orders that were added or updated.

 

In order to do this, I need to order the results of the Order API by "updated_at asc" and then use the "since_id" to make sure I'm pulling exactly all orders after a certain ID. I saw on an old answer on this forum that you can use the "order" argument to order the results. It seems to work, but when I use it with "since_id" it gives incorrect results.

 

A few questions:

  • What is the recommended way to order results?
  • The "since_id" description is "Show orders after the specified ID." — how do you define "after"? Is it by the "order" field? Is it by sorting the ID number?
Accepted Solution (1)
KarlOffenberger
Shopify Partner
1873 184 901

This is an accepted solution.

So pull via since_id and sort on your end in app code once all orders have been fetched. You'd anyway want results in default sort order by ID so you have the last items' ID for the next request.

View solution in original post

Replies 11 (11)

KarlOffenberger
Shopify Partner
1873 184 901

Hi,

 

Why do you need the orders sorted by date? If it is just a matter of getting newly created or updated orders per day, you can query orders by date range for updated_at like so

GET /admin/orders.json?status=any&updated_at_min=2019-03-07&updated_at_max=2019-03-08

But if you still insist on sorting by date, you can as mentioned in the referenced post

/admin/orders.json?status=any&updated_at_min=2019-02-27&updated_at_max=2019-02-28&order=updated_at asc
michaelm
Shopify Partner
10 0 8

@KarlOffenberger The reason that I need to fetch orders sorted by date is because I only want to pull orders that have been created/updated since last I pulled. The issue arises when I combine the "order" query argument with the "since_id" query argument.

 

The specific reason I need to use "since_id" is because there could be 250 (the maximum result amount) orders with the same "updated_at" value. In order to pull all of them, and make sure I don't miss any orders, I'll need to include a since_id to paginate through the results. Any ideas on how to solve this problem?

KarlOffenberger
Shopify Partner
1873 184 901

This is an accepted solution.

So pull via since_id and sort on your end in app code once all orders have been fetched. You'd anyway want results in default sort order by ID so you have the last items' ID for the next request.

michaelm
Shopify Partner
10 0 8

@KarlOffenberger you're right, that solution makes a lot of sense. I'll look into it.

 

Do you happen to know if the "order" and "page" arguments will be officially supported by Shopify? I noticed that they seem to work when I send them, but they're not listed on the API page.

KarlOffenberger
Shopify Partner
1873 184 901

Unfortunately not - I do not have any insights regarding their roadmap. Your best bet would be to post a new question asking that and a staff member might offer some insight.

rbnali
Shopify Partner
24 1 12

@KarlOffenberger The order argument is still not listed in the official Order API page. Do you have an update on whether or not this will be officially supported (or deprecated) in the near future?

Adding some context on my specific case:

We're building a pipeline to ingest Shopify Orders via the API. Every hour, we look at the max(updated_at) in our shopify database and use it as our updated_at_min argument to call the Orders API. Between each call (pagination), we append the data to our database and remove duplicates before going to the next page. When running the script, we do not store orders from previous pages in memory to use as little memory as possible and avoid out-of-memory issues.

By default, the API gives orders ordered by updated_at desc. If our ingestion script breaks in the middle of its run, there will be a missing entries (with updated_at < new max(updated_at)) that we will ignore in the next job run.

Of course, we could avoid this situation by keeping in memory the orders from all the pages on our machine before sending them to the database, but that is not memory efficient. I'm quite sure I'm not the only one building such ingestion pipelines from the Orders API and the `order` argument is very useful here to have an optimised ingestion.

rbnali
Shopify Partner
24 1 12

@michaelm Have you posted the question for the roadmap? If yes, can you link me to the thread?

kumbagiri9
Visitor
2 0 1

I have rest APi client  to get API data .My company is authorized  to shopify . Can someone  advise me rest API url   to get daily orders as batch . Thanks a lot 

ashishmishra
Visitor
1 0 0

Hi

Can you send me the request URL to retrieve the order details from shopify? I need to know the complete request URL and what authentication method need to be used at the time of sending the request?

kumbagiri9
Visitor
2 0 1

Thanks  a lot for the response . Sure I will send you  today .

pankajjaisu
Shopify Partner
1 0 0

by default on which basis sorting for Shopify API is done  specially Order API is it ascending order of created_at or something else?