r/excel•
David2875

Excel VBA to retrieve json using API key issues

Waiting on OP

I've got a current situation where I cannot seem to figure out how to use the correct syntax in VBA to use an API key to get json data. The curl command would be:

curl --header "api-key: abc123" -X GET "https://api-hazards.atcouncil.org/public/v1/wind.json?lat=33.996&lng=-84.006"

I'm using the following but I'm sure calling the API key isn't correct and my Googling is failing me.

With CreateObject("WinHttp.WinHttpRequest.5.1")

.Open "GET", "https://api-hazards.atcouncil.org/public/v1/wind.json?lat=33.996&lng=-84.006", False

.setRequestHeader "User-Agent: curl/7.33.0"

.setRequestHeader "Authorization: Basic " & "api-key: abc123"

.setRequestHeader "Content-Type: application/json"

.send

Debug.Print "API Response: " & .responseText

Debug.Print "API Status: " & .Status

End With

Can anyone please help me with the syntax to convert the curl command to a way to pass it through the WinHttpRequest? Thank you!

Posted Nov 1, 2019, 1:28 AM UTC

12
Eightstream32•

Is there a specific reason why you're making this call via VBA instead of M/Power Query? The latter is much easier.

2
David2875OP•

Thanks for your input and I’ll check out Power Query. My concern is the url will end up dynamic based on input. This is also a minimal part of a larger workbook so I need to make sure it doesn’t try to auto update when opening / changes cells due to limited api calls per month.

1