Featured image for blog post on how you can start calling the Marketo API with Python

Start Making Calls With Python

I have done a lot of posts and videos about how to use the Marketo API with Postman but now I want to talk about Python to take it to the next level. Jep Castelein does maintain a Marketo repository on GitHub but my examples below are from how I taught myself how to do it.

Libraries to import

You will need to import two libraries for the Marketo authentication scripts: json which is a standard library and is already included with Python, and Requests which will need to be installed. 

Create a Launchpoint Service

You should set up a Launchpoint service in Marketo to use with Python. If you have roles available, I would also suggest creating your own Python API user. Once you have this, make note of the Munchkin Account ID for your instance as well as the Client Id and Client Secret from your Launchpoint service.

Creating Instance Variables

The first thing we need to do is to import the libraries noted above and create variables for our Munchkin Account ID, Client Id and Client Secret. I use mmc (Marketo Munchkin Code) mci (Marketo Client ID) and mcs (Marketo Client Secret) respectively, but you can use whatever makes sense to you. Enter your specific values between the quotes.

import json, requests

#credentials

mmc = 'MUNCHKIN ID xxx-xxx-xxx'

mci = 'MARKETO CLIENT ID'

mcs = 'MARKETO SECRET'

Calling the Authentication Endpoint

In this step we are going to get our access token and store it in a variable as it will be needed for all subsequent calls. We are going to use the Requests library that we import to make the GET call. Notice that we are use the variables above in the URL and have added the grant_type parameter with a value or “client _credentials”.

#get token

mkto_access = requests.get(f'https://{mmc}.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id={mci}&client_secret={mcs}')

Now we need to get the json response which stores the access token.

mkto_auth = (mkto_access.json())

The JSON response will have four key-value pairs:

  • access_token: The value of the token granted from Marketo to authenticate other calls
  • token_type: Will be ‘bearer’
  • expires_in: How much more time in seconds that the token will be valid for (expires after 60 minutes)
  • scope: The name of the Launchpoint service used to get the token

We want to store the access_token value in a variable to make for future calls.

mat = (mkto_auth['access_token'])

Calling Other Marketo Endpoints

Now that we have our token we can call the other Marketo API endpoints. In this example, we are going to use the Get Form By Id Endpoint. You can see that we are once again using the Requests library to call the endpoint and query parameter of “access_token” is using the “mat” variable we created in the previous step. Then we get the JSON response and print it out

get_forms = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/form/2244.json?acces_token=mat')

get_forms_json = get_forms.json()

print(get_forms_json)

Now the whole script will look like this

import json, requests

#credentials
mmc = 'MUNCHKIN ID xxx-xxx-xxx'
mci = 'MARKETO CLIENT ID'
mcs = 'MARKETO SECRET'

#get token
mkto_access = requests.get(f'https://{mmc}.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id={mci}&client_secret={mcs}')
mkto_auth = (mkto_access.json())
print(mkto_auth)
#print('Token expires in: '+str(round(mkto_auth['expires_in']/60))+' minutes')
mat = (mkto_auth['access_token'])

#get forms
get_forms = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/form/2244.json?acces_token=mat')
get_forms_json = get_forms.json()
print(get_forms_json)

Similar Posts