Part 1-My First Successful API Callout

I'm learning about HTTP Callouts, and wanted to share what I've figured out so far.  My goal was to use API to update a contact record based on the contact's zip code.  This is a beta feature that's available in all sandboxes and orgs that have "rolled forward" to spring '23.

 

I started with an API called Zippoptam.us.  Here's how the API works: if you write a zip code in the URL, it will send you information about that zip code.  For example: http://api.zippopotam.us/us/20011 gives you this:


My first step was to create a named credential.  One mistake I made in my first few tries was that I did not allow merge fields in my HTTP headers.  This is important because I want to pull the zip code from contact records and use them to change the values of the URL.
 named credential.png  Once I've done that, I've made a new HTTP callout from actions in flow.  Notice that I use the named credential I just made:

Create external.png

Next, I created an invocable action.  I wrote "/US/{ZIP}".  This mirrors the format of the url I showed above--the zip in the curly brackets shows the value that I want to set based on the value of an account field.  Once I typed in the url, the "Key" ZIP appeared below.  I set ZIP as string because a zip code is a text string.

 

My next step was to click on the sample response button and add a sample.  To get the sample response, I went back to by browser (firefox) and selected raw data.  Then I copied the text below.

 

Raw data.pngAnd I pasted it to Sample Response.  Note that it shows the information that I get back (after clicking review).  Note that it lists the data "type" of the responses.  I was right when I guessed that zip code (aka post code) is a string:

 

Sample response.png

Now, I select the action that I made in the flow and set $record.MailingPostalCode as the source for zip (the key I created above).  My flow, by the way, is a scheduled flow that runs on contact records where the zip is not null:

Edit action.pngAfter debugging the flow, you can see it got information about 20011 (I could have picked another zip code, but I wasn't creative)

 

debug.png

Sadly, I was not able to pull all of this data into my flow (most notably, I wasn't able to get the state).  When I created an update record element, I was able to select the outputs:


  

But only these were available.


Thankfully, I was able to select country and use it to update the contry field on the contact that triggered the flow. 

Update records.png

It proves that I "did the thing," but isn't practically useful in this case.  I, after all knew that all the inputs were going to be from the us, because I'd selected: http://api.zippopotam.us/us/ as my url of choice.  

I was hoping to access the state or city, but they are part of an array, basically a list of strings.  I know that you can use apex to access items within the array, but given that I'm hoping not to use apex, this isn't what I'm hoping for.  At the very least, it means I have the wrong API if I want to really use this flow with my current skill level.

 As a bonus thought, it looks like you can access the array in the update record item (even if clicking in the value box suggests otherwise).  When I added ".places" (the name of the method that the array is associated with) as shown below and assign it to MailingCity:

 I get the following:

 
This basically let me know that adding ".places" gives me the response values associated with places.  Now, I just need to figure out how to do something meaningful with those values.

The most useful video I've seen so far on this is produced by Salesforce Break.

I hope to share my learning with other admins as I continue.
 
 

Comments