US address verifier using usps API

US address verifier using usps API

We came across a requirement where one of our customers wanted to verify the address entered. It took a while to get the right API method that would work. Fortunately, USPS offers a good API and documentation on this. We are listing the steps below to achieve the same in your application.

Prerequisites:

  1. A USPS api USER ID, created at https://registration.shippingapis.com/ You will receive an email after the registration with the userid (created automatically by usps)
  2. Knowledge of Zoho Creator’s PostURL and XML parsing and deluge scripting

How It Is Done :

We have created the following fields on the Form

  1. Address fields – Street Address, Apt no, City, State, Zip-
  2. a decision box “Verify Address”, to initiate the address verifier
  3. an Add notes field “address_display”, to display the verified address
  4. a decision box “Update Address”, to update the address fields with the correct address

Script Execution:

On user input of “Verify Address” field triggers the address verifier and updates the address_display notes field and has the following script

if (input.Verify_Address){
xml_string = thisapp.usps.construct_xml(input.Street_Address, input.Apartment_or_Suite_Number, input.City, input.State, input.Zip_Code);
usps_resp = thisapp.usps.verify_address(xml_string);
resp_map = thisapp.usps.beautify_xml(usps_resp);
input.Verified_Address = resp_map.toString();
input.address_display = resp_map.get("address_display_html");
}

1. Construct The XML

string usps.construct_xml(string street_address, string apt_no, string city, string state, string zip)
{
    xml_string = "<AddressValidateRequest USERID=\"your_user_id_from_usps_email\"><IncludeOptionalElements>true<IncludeOptionalElements> <
    ReturnCarrierRoute>true</ReturnCarrierRoute><Address ID=\"0\"><FirmName />";
    xml_string = xml_string + "<Address1>" + ifnull(input.apt_no,"") + "</Address1>";
    xml_string = xml_string + "<Address2>" + ifnull(input.street_address,"") + "</Address2>";
    xml_string = xml_string + "<City>" + ifnull(input.city,"") + "</City>";
    xml_string = xml_string + "<State>" + ifnull(input.state,"") + "</State>";
    xml_string = xml_string + "<Zip5>" + ifnull(input.zip,"") + "</Zip5>";
    xml_string = xml_string + "<Zip4></Zip4>";
    xml_string = xml_string + "</Address></AddressValidateRequest>";
    return xml_string;
}

2. Use The Zoho Creator’s Post Url To Verify Address From USPS Website

string usps.verify_address(string xml_string){
     usps_url = "http://production.shippingapis.com/ShippingAPI.dll";
     params = map();
     params.put("API", "Verify");
     params.put("XML", input.xml_string);
     usps_resp = postUrl(usps_url, params);
     return usps_resp;
}

3. Parse The XML And Store The Response In A Map/Object Variable

map usps.beautify_xml(string xml_string){
    xml_map = map();
    address_display_html =("<style>.list-group-item{list-style: none;color:#fff;}</style><div id='address_display_custom' style='background-color:#5cb85c;border-color:#5cb85c;width:300px;padding:4px;border-radius:4px;border:1px solid rgba(0,0,0,.125);'><ul class='list-group'>");
     street_address_val = input.xml_string.executeXPath("/AddressValidateResponse/Address/Address2/text()");
     xml_map.put("street_address_val", street_address_val);
     address_display_html = address_display_html + "<li class='list-group-item'>" + street_address_val + "</li>";
     apt_no = input.xml_string.executeXPath("/AddressValidateResponse/Address/Address1/text()");
     xml_map.put("apt_no", apt_no);
     address_display_html = address_display_html + "<li class='list-group-item'>" + apt_no + "</li>";
     city_val = input.xml_string.executeXPath("/AddressValidateResponse/Address/City/text()");
     xml_map.put("city_val", city_val);
     address_display_html = address_display_html + "<li class='list-group-item'>" + city_val + "</li>";
     state_val = input.xml_string.executeXPath("/AddressValidateResponse/Address/State/text()");
     xml_map.put("state_val", state_val);
     address_display_html = address_display_html + "<li class='list-group-item'>" + state_val + "</li>";
     zip_val = input.xml_string.executeXPath("/AddressValidateResponse/Address/Zip5/text()");
     xml_map.put("zip_val", zip_val);
     address_display_html = address_display_html + "<li class='list-group-item'>" + zip_val + "</li>";
     address_display_html = address_display_html + "</ul> </div> ";
     xml_map.put("address_display_html", address_display_html);
     return xml_map;
}

To update fields with correct address, the Update Address field’s on user input has the following script

Note: Verified_Address is a hidden multi-line text field to store the map response.

if (input.Update_Address  &&  (input.Verified_Address  !=  "")){
    xml_map = input.Verified_Address.toMap();
    input.Apartment_or_Suite_Number = xml_map.get("apt_no");
    input.Street_Address = xml_map.get("street_address_val");
    input.City = xml_map.get("city_val");
    input.State = xml_map.get("state_val");
    input.Zip_Code = xml_map.get("zip_val");
}

Live Preview