Authenticating and Retrieving Lead Data from Marketo with the REST API

July 10, 2015 | by

Note: In the Java examples below, we’ll be using the minimal-json package to handle JSON representations in our code.  You can read more about this project here: https://github.com/ralfstx/minimal-json

One of the most common requirements when integrating with Marketo is the retrieval of lead data.  Most, if not all integrations will require either the retrieval or submission of lead data from a Marketo subscription, so today we will take a look at a basic lead information retrieval task, by authenticating with a subscription and then retrieving lead data from it.  If you haven’t yet provisioned an API user and a Custom Service, check out our Quick Start Guide for some quick configuration instructions.

To retrieve our leads, first we need to authenticate with the target Marketo instance using OAuth 2.0.   There are three pieces of information we need to authenticate with Marketo, the client ID, client secret, and the host of the Marketo instance.  Here’s the class we’ll be using to authenticate:

This code allows you to create an instance of Auth with our client ID, client secret and host (as marketoInstance) from Admin -> Launchpoint (ID and Secret) and Admin -> Web Services (Host).  It exposes the getToken method which tests whether the currently stored token is null or expired, and then returns either the existing token, or makes a new authentication request then returns the new token from the “access_token” member of the JSON response.

Now that you can authenticate with your Marketo instance, the next step is to retrieve our leads.  We’ll be using this class:

This class has a single constructor which accepts an Auth object, and then exposes several setters for both optional and required parameters.  In this case, we really only need to concern ourselves with setting the filterType and filterValues to perform get leads by email address.  So we’ll use setFilterType for “email”, and addFilterValue for the email address that we need to retrieve an ID for.  When you’ve set your parameters, you can use the getData method to retrieve a JsonObject from the leads endpoint, containing a results array which has a JSON representation of the lead records which were retrieved.

Putting it together

Now that we’ve gone through the sample code that we’ll be using let’s take a look at a simple example to retrieve leads matching a test email address, testlead@marketo.com.  For this we’ll need to use setFilterType for “email”, and addFilterValue for the email address that we need to retrieve information for.  When you’ve set your parameters, you can use the getData method to retrieve a JsonObject from the leads endpoint, containing a results array which has a JSON representation of the lead records which were retrieved.

In this main method example, we create an instance of Auth, and then pass this to a new Leads constructor.  Using setFilterType, and addFilterValue, we configure our instance of Leads to retrieve just leads matching the email address “testlead@marketo.com.”  This example prints this out to the console:

Now we have lead data which we can process in whatever way that we need.  Thanks for reading, and please leave any feedback you have in the comments.