Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REST API - Shopify 3

Status
Not open for further replies.

Swi

Programmer
Feb 4, 2002
1,963
US
Hi,

I have a legacy app that connects to an online store database to pull orders and integrate into a MIS system. The client is migrating away from their current site and going to Shopify. I would like to just swap out the module that reads the DB and add an API call to pull orders from the Shopify REST API. However, I am not seeing many examples of this on the web. Has anyone successfully done this in VB6 that may be a starting point? Thanks.

Swi
 
I don't have a Shopify site to test it agfainst, and SHopify don't give out free test sites as far as I can tell, but the following should retrieve all orders from a shop

Code:
[COLOR=blue]Private Sub RestExample()
    Dim APICall As String
    Dim Query As String
    Dim JSONResponse As String
    
    APICall = "[URL unfurl="true"]https://your-development-store.myshopify.com/admin/api/2023-04/orders.json"[/URL]
    Query = "?status=any" [COLOR=green]' all orders[/color]
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", APICall & Query, False
        .setRequestHeader "X-Shopify-Access-Token", "<your_access_token_goes_here>"
        .send
        JSONResponse = .responseText[COLOR=green] ' Now all you need to do is figure out how to use VB6 to decode complex JSON ...[/color]
    End With
    [COLOR=green]' Do something with JSONRFesponse[/color]
End Sub[/color]

Note that teh response is JSON. Shopify used to also provide XML responses, but I am not sure that they do any more. Loads more detail of parameters etc is available e.g. here
 
Hi,

I set up a test environment and tried the code.

Code:
    APICall = "[URL unfurl="true"]https://quick-start-619c55ce.myshopify.com/admin/api/2023-04/orders.json"[/URL]
    Query = "?status=any" ' all orders
    Set oxmlhttp2 = New MSXML2.ServerXMLHTTP60
    With oxmlhttp2
        .open "GET", APICall & Query, False
        .setRequestHeader "X-Shopify-Access-Token", "XXXXXXXXXXXXXXXXXXXXXX"
        .send
        Set oXMLDoc2 = New MSXML2.DOMDocument60
        oXMLDoc2.async = False
        If oxmlhttp2.readyState = 4 Or oxmlhttp2.Status = 200 Then
            JSONResponse = .responseText
        Else
            MsgBox "Error Connecting!", vbCritical + vbOKOnly
            JSONResponse = ""
        End If
    End With
    MsgBox JSONResponse

I had the proper token from the site. Is there somewhere I need to put additional credentials? I have the following error.

{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

I can provide the API token if you want as this is a test site.

Thanks.

Swi
 
In the Headers section, try to add second header with Key of Content-Type and Value of application/json:

[pre].setRequestHeader "Content-Type", "application/json"[/pre]
 
Doers your access token have the correct Shopify scope to read orders?

Try

Code:
APICall="[URL unfurl="true"]https://your-development-store.myshopify.com/admin/oauth/access_scopes.json"[/URL] 
Query = "" '[COLOR=green] no specific query[/color]

and examine the result
 
Ah, I found the solution. I needed the Admin API access token.

There was also a Storefront API access token and API key and secret key.

Thanks! Going to work on this now. It seems to now return the orders.

Just need to parse them and then work on selecting orders on a timed basis but not pick up ones I already imported.

Additionally, once I import into our MIS system and get a tracking number from UPS, FedEX or USPS I will need to look at the API to update Shopify.

I really appreciate the help strongm.

Swi
 
@Swi (Programmer)(OP)

Just searched Google and found this post. I am working on the same problem and not finding any workable solution. It is great to know that you have already done it. Can you share the working solution (only the generalized section) that successfully connects to Shopify and downloads the Orders?
It will be a great help, and thank you in advance.
 
Sorry, just saw this. When I am near a PC I will paste the code here probably tomorrow. Thanks.

Swi
 
Here you go. This pulls all orders in a JSON format that you will then need to parse. This will pull all orders for the current day that have a status of open, unfulfilled and are paid. I hope this helps. Thanks.

Code:
    ' Build API call based on criteria and the current date
    APICall = "[URL unfurl="true"]https://quick-start-XXXXXXXXX.myshopify.com/admin/api/2023-04/orders.json"[/URL]
    Query = "?status=open;fulfillment_status=unfulfilled;financial_status=paid;updated_at_min=" & FormattedDateStart & "T00:00:00;updated_at_max=" & FormattedDateEnd & "T23:59:59"
    Set oxmlhttp2 = New MSXML2.ServerXMLHTTP60
    With oxmlhttp2
        .open "GET", APICall & Query, False
        .setRequestHeader "X-Shopify-Access-Token", "XXXXXXXXXXX"
        .setRequestHeader "Content-Type", "application/json"
        .send
        Set oXMLDoc2 = New MSXML2.DOMDocument60
        oXMLDoc2.async = False
        If oxmlhttp2.readyState = 4 Or oxmlhttp2.Status = 200 Then
            JSONResponse = .responseText
        Else
            SendEmail "NOTIFICATION - Error Connecting", FailureEmailGroup, "", "", "Order Administrator <xxxxx@xxxx.com>", _
            "<B>Summary:</B><BR>" & "Unable to connect to Shopify API!", "", "Yes"
            End
            JSONResponse = ""
        End If
    End With



Swi
 
BTW, strongm had most if not all of this code previously in the post. Basically my version filters the results down a bit and has a bit of error handling. Just wanted to give credit where credit was due. Thanks.

Swi
 
@Swi (Programmer)(OP)
Thanks for your kind support, and providing the Code.
I will try this code, and let you know if any trouble comes.
Anyhow, thanks once again for your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top