Search code examples
marketo

Get all leads programmatically in Marketo v1


I would like to get all the leads that a customer has in Marketo.

I understand that you can Get Multiple Leads by Filter Type REST API endpoint.

If I do not have access to their Marketo UI, how should I get all the leads?

I was thinking about querying 300 ids at a time until there were no more results. But I am unsure about how to handle if all leads in a 300 batch are deleted, but there are leads after that deleted batch. Are deleted leads returned?


Solution

  • I'll describe a workaround you can use to determine the last lead that was created in Marketo using the REST API. You can use this lead as the upper bound for lead id, and then query leads 300 at a time until you reach this upper bound as you described.

    The workaround is to use the Get Lead Activities API to return activities for most recent leads that were created. By calling this API, you can determine the last lead that was created in Marketo, and then use it as your upper bound.

    Here are some tips for calling the Get Lead Activities API:

    • Specify an activityTypeIds=12 parameter to return activities for new leads.
    • Include a paging token parameter as the start date to look for the most recent leads that were created. To generate a paging token, you will need to use the Get Paging Token API.
    • To optimize this, start with a time range that is close to the current date. For example, first query the Get Lead Activities API for leads created in the past hour. Then if there are no results, query for the past day, and so on.
    • Iterate through the results from the Get Lead Activities API until the moreResult attribute in the response is false. The last lead returned will be the upper bound for leads ids.

    For example, a call to the Get Lead Activities API will look like:

    /rest/v1/activities.json?nextPageToken=GIYDAOBNGEYS2MBWKQYDAORQGA5DAMBOGAYDAKZQGAYDALBQ&activityTypeIds=12