Powershell: Use the SmartyStreets API

SmartyStreets is an address validator for U.S. postal addresses.  Feed SmartyStreets an address, like “11 Church Street, Burlington VT” and, if the address is matchable with the official U.S. postal service address, it will be returned, including the 9 digit zip code.  The SmartyStreets API has some of the best API documentation. Here is the PowerShell  code to validate a single address.

<#Powershell Code to query SmartyStreets API 
Provide address validation for a single U.S. address submitted to the API
LK 11/12/2014 
#>

$Output=""

$Uri="https://api.smartystreets.com/street-address?"+
"street=11+Church+Street&"+
"city=Burlington&"+
"state=VT&"+
"auth-id=myauthid"+
"auth-token=myauth-token"

$Output=Invoke-RestMethod -Uri $Uri -ContentType application/json -Method Get 

$Output.delivery_line_1
$Output.last_line
$Output.metadata

Athe auth-id and auth-token are values that you obtain from the Smartystreets site, which validate your account.

The result of the code is placed in the variable $Output.

Running this program provides the following output, the two validated address lines, and a slew of meta-data related to the address, including the county, gps coordinates, etc.

11 Church St
 Burlington VT 05401-4417
record_type : S
 zip_type : Standard
 county_fips : 50007
 county_name : Chittenden
 carrier_route : C009
 congressional_district : AL
 rdi : Commercial
 elot_sequence : 0196
 elot_sort : A
 latitude : 44.47953
 longitude : -73.21282
 precision : Zip9
 time_zone : Eastern
 utc_offset : -5
 dst : True

You can see the returned fields by piping $Output to Get-Member  The delivery_line_1, and last_line contain the validated address with nine-digit zip code.

PS >$Output | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name                   MemberType   Definition
 ----                   ----------   ----------
 Equals                 Method       bool Equals(System.Object obj)
 GetHashCode            Method       int GetHashCode()
 GetType                Method       type GetType()
 ToString               Method       string ToString()
 analysis               NoteProperty System.Management.Automation.PSCustomObject analysis=@{dpv_match_code=Y; dpv_footnotes=AA...
 candidate_index        NoteProperty System.Int32 candidate_index=0
 components             NoteProperty System.Management.Automation.PSCustomObject components=@{primary_number=11; street_name=C...
 delivery_line_1        NoteProperty System.String delivery_line_1=11 Church St
 delivery_point_barcode NoteProperty System.String delivery_point_barcode=054014417112
 input_index            NoteProperty System.Int32 input_index=0
 last_line              NoteProperty System.String last_line=Burlington VT 05401-4417
 metadata               NoteProperty System.Management.Automation.PSCustomObject metadata=@{record_type=S; zip_type=Standard; ...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s