I am trying to use VBA to get tracking information from the TrackingMore API. I am using the WinHttpRequest object in VBA. My API calls seem to be successful as I get a response with a status code of 200, but the data array in the response is empty. Here is my code:
Sub GetTrackingInfo_2()
Dim http As Object
Dim url As String
Dim apiKey As String
Dim carrierCodes As Variant
Dim trackingNumbers As Variant
Dim response As String
Dim i As Integer
Dim ws As Worksheet
Dim json As Object
apiKey = "" 'Please use your key if possible
carrierCodes = Array("bluedart", "delhivery", "dtdc", "bluedart")
trackingNumbers = Array("78051987016", "21025821925200", "I45618896", "80545687143")
Set ws = ThisWorkbook.Sheets("Sheet1")
'Set http = CreateObject("MSXML2.XMLHTTP")
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
For i = LBound(carrierCodes) To UBound(carrierCodes)
url = "https://api.trackingmore.com/v4/trackings/" & carrierCodes(i) & "/" & trackingNumbers(i)
With http
.Open "GET", url, False
.setRequestHeader "Tracking-Api-Key", apiKey
.setRequestHeader "Content-Type", "application/json"
.send
response = .responseText
End With
Debug.Print response
Next i
Set http = Nothing
End Sub
I am getting the following response for each request:
{"meta":{"code":200,"type":"Success","message":"The request was successful."},"data":[]}
{"meta":{"code":200,"type":"Success","message":"The request was successful."},"data":[]}
{"meta":{"code":200,"type":"Success","message":"The request was successful."},"data":[]}
{"meta":{"code":200,"type":"Success","message":"The request was successful."},"data":[]}
Steps already Taken:
- Verified that the API key is correct.
- Checked that the carrier codes and tracking numbers are valid and correctly formatted.
- Ensured the Content-Type and Tracking-Api-Key headers are set correctly.
Questions:
Why is the data array in the response always empty despite the successful request?
3
Based on the TrackingMore documentation, you need to pass the carrier/courier number and the tracking number as URL parameters instead of including them in the URL path.
CURL equivalence:
curl --request GET
--url 'https://api.trackingmore.com/v4/trackings/get?tracking_numbers=123&courier_code=123'
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--header 'Tracking-Api-Key: 123'
In VBA, you’d need to replace the line where you define the URL with something like this:
url = "https://api.trackingmore.com/v4/trackings/get?courier_code=" & carrierCodes(i) & "&tracking_numbers=" & trackingNumbers(i)