Smarty Streets- Verify your mailing addresses in dataverse with Power Automate against USPS- Power Platform

Use case: Verify mailing addresses are valid with the Unites States Postal Services before mailing out large quantities of piece mail or check payments.  Thus reducing mailing returns.
In this flow I am doing a bulk address standardization. You would really only need to do that once and then maybe use an "Update" row after that.

First you will need a Smarty Streets account. You can try a trial version to test if this solution is best for you.

https://www.smarty.com/

You will need create an API Key after you create an account> https://www.smarty.com/account/keys

Copy the auth ID and the Auth Token to a notepad for later use.

 

Now that you have your API Key, let's work on the flow.

1. I create a manual trigger flow for testing.  I am using Dataverse for my datasource.

 

2. Get Rows (Dataverse below)    Other options- Get items (SP) - Get Rows (excel online)

I did a couple things that you do not need to do. However, the trial version of Smarty Streets only give you 1000 free "lookups" so I changed my row count to 5 so it would only process 5 addresses at a time.  For testing purposes... I didn't want to run out of freebies quickly. You may change this to what ever you want>
I also used "Sort By" asc by a certain column so I could add an address that I knew was going to fail to the top of the list to test.

3. Now the fun part- Add a "Compose" Action, This will throw it into a "Apply to Each" action.
Use Dynamic Content to get the "StreetAddress" in your datasource. (You may have named this something else)

4. For the API call... we are going to split the address into separate variables/ compose steps.
The split 
The expression:  split(outputs('DataverseStreetAddress'), ' ')  The expression uses a "Split" where there is a space (, ' ') in the Compose step from before ( outputs('DataverseStreetAddress') )

5. Let grab the street number using compose action! 
Since in the US the "street number" is usually always the first part of the address. We can use the "first" expression.
The expression: first(outputs('Split'))

6. Let's start grabbing the rest of the street name using compose actions.
I am grabbing the first three parts after the number because not only will smarty streets verify the addresses but it will also standardize it as well. 
An example of a 3 part address would be: West Main St. or Saint Main St. (This does not include the street number above.)

The expressions: outputs('Split')?[1]  = StreetName1
                             outputs('Split')?[2] = StreetName2
                             outputs('Split')?[3] = StreetName3

7. Add another Compose Action to put all together for the HTTP call
Add your previous compose actions outputs, Make sure to enter a "+" in between each. and add your City and State from your datasource (Mine is still Dataverse)
This will output like this. IE: 234+west+Main+st+Philadelphia+PA  which is what you need for the API Call.

8. Add a HTTP Response "Get" action

URI: https://us-street.api.smarty.com/street-address?street=INSERT YOUR COMPOSE 2 OUTPUT FROM THE DYNAMIC CONTENT&auth-id=YourAUTHID&auth-token=YourAUTHTOKEN

9. Run a Test to make sure your can connect and to get a JSON Sample
Select All and Copy "Body" Seen in green below.

10. OK Let's parse the JSON
In Content, enter the "Body" from the HTTP Action
Click Generate from Sample and paste the JSON in from the step above.

11. "Fake Null" Compose step. 
For my purposes, I need the department that manages students records to be alerted that one of the student does not valid mailing address.
Is JSON, those addresses come back as "[]" which is a NULL. But... The condition action does like when I enter the text "[]" or when I entered the expression for NULL.
This Compose action worked beautifully. :)

12. Let's find the good, the bad and the ugly... mailing addresses.

Add a condition action.
If the Body of the HTTP action does not equal the Fake Null (Compose action)

13. Then let's format it and update the dataverse row with the standardized address in another column. (I created a new column for standardized addresses)

What is super cool is Smarty Streets will spit back a bunch of JSON Values you can use for each address, such as County and the longer zip code.
I used the "delivery_line_1" and "last_line". You will find the values in the Dynamic Content. BTW this will automatically put it into an "Apply_to_each"

Then Update the the record/Row with the standardized mailing address.

 

14. If the mailing address comes back as not Valid, I put an action to send an email.  You can add any action you want here.

 

The Outcome of this flow! 

Valid Address

Not Valid

(I made this address super crazy so there would not be a match for testing purposes)

Blog Type: