Can you do an API call in Excel using VBA?

Elliottj2121

Board Regular
Joined
Apr 15, 2021
Messages
56
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

Is there a way to do an API call in excel using VBA? or is there another non-macro way to do it? I am trying to use the United States Post Office's API to get the full nine digit zip code for addresses. The API requires a username and password which I have. I have linked to the technical information on the USPS website and included an example sheet. Any help would be greatly appreciated.

USPS TECH SPEC SHEET Specific section is 3.0 ZIP Code Lookup API

Book1
ABCD
1VARIABLEDATARESULTDATA
2ADDRESS1600 PENNSYLVANIA AVE NWZIP+420500-0005
3CITY WASHINGTON
4STATEDC
5ZIP20500
6
7
Sheet1
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I got this close but the requestData seems to fail on the call.



VBA Code:
Sub GetUSPSAddressInfo()
    Dim apiURL As String
    Dim address As String
    Dim requestData As String
    Dim responseText As String
    
    apiURL = "https://secure.shippingapis.com/ShippingAPI.dll/API/Verify"
    
    address = "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"
    
    ' this is where it fails
    requestData = "<AddressValidateRequest USERID=''><Address ID='0'><Address1></Address1><Address2><![CDATA[" & address & "]]></Address2><City></City><State></State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>"
    
    responseText = GetUSPSAPIResponse(apiURL, requestData)
    
    Debug.Print "XML Response:"
    Debug.Print responseText
End Sub

Function GetUSPSAPIResponse(apiURL As String, requestData As String) As String
    Dim xmlHTTP As Object
    Set xmlHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    With xmlHTTP
        .Open "POST", apiURL, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send "API=Verify&XML=" & requestData
        If .Status = 200 Then
            GetUSPSAPIResponse = .responseText
        Else
            GetUSPSAPIResponse = "Error " & .Status & ": " & .responseText
        End If
    End With
    
    Set xmlHTTP = Nothing
End Function
 
Upvote 0
USPS TECH SPEC SHEET Specific section is 3.0 ZIP Code Lookup API

See if this example code works for you - change the userID string. According to the specs a password isn't needed.

VBA Code:
Public Sub Zip_Code_Lookup()

    Dim httpReq As Object
    Set httpReq = CreateObject("MSXML2.XMLHTTP")
    
    Dim URL As String
    Dim userID As String
    Dim XML As String
    
    userID = "user123"
    
    XML = "<ZipCodeLookupRequest USERID='" & userID & "'>" & _
          "<Address ID='" & "1" & "'>" & _
          "<Address1></Address1>" & _
          "<Address2>1600 PENNSYLVANIA AVE NW</Address2>" & _
          "<City>WASHINGTON</City>" & _
          "<State>DC</State>" & _
          "<Zip5>20500</Zip5>" & _
          "<Zip4></Zip4>" & _
          "</Address>" & _
          "</ZipCodeLookupRequest>"
       
    URL = "https://secure.shippingapis.com/ShippingAPI.dll?API=ZipCodeLookup&XML=" & XML
          
    With httpReq
        .Open "GET", URL, False
        .send
        Debug.Print .Status, .statusText
        Debug.Print .responseText
    End With

End Sub
Your code would have to build the XML string according to the cell values. Since the response is XML format, it's probably better to use DOMDocument instead of XMLHTTP to send the request and then the response can be easily parsed.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top