Retrieve Activities for a Single Lead using REST API

February 11, 2016 | by

Here is a question that we are repeatedly asked by our developer community:

“How do I get a list of past activities for an individual lead?”

Up until recently, there was no straightforward way to accomplish this using the REST API.  But now there is!

The Winter 2016 release of our REST API contains a nice little enhancement. Get Lead Activities now accepts the leadIds parameter that can be used to specify a lead id.  When the leadIds parameter is specified, only activities for that lead id will be returned.  You can think of this as a lead id filter.

Note that the leadIds parameter can take a comma separated list of lead ids in case you’d like to filter results on more than one lead (up to 30).  This might come in handy for example when limiting activities to leads for a particular company.

Example

Below is a sample request to Get Lead Activities that contains the leadIds parameter.  I have specified a value of “50” for the leadIds parameter, which corresponds to an arbitrary lead in my Marketo instance.  I have specified a value of “129” for the activityTypeIds parameter, which corresponds to the “Mobile App Session” activity on my Marketo instance.

Below is an excerpt of the response from that request above.  As you can see, it contains only result objects with “leadId”: 50 and “activityTypeId”: 129.

Fun With Shell Scripts

While working on this post, I wrote the following shell script to automate lead activity retrieval.  If I’ve written it correctly, it should be self explanatory 😉  Feel free to replace the constants at the top of the script with values from your instance and give it a try.

Note that this script makes use of a command line tool “jq”.  I’ve found that jq and jq play are indispensable tools when slicing and dicing JSON response bodies.  Enjoy!