Start Making Calls With Python
This guide introduces using Python with the Marketo API. While Jep Castelein maintains a Marketo repository on GitHub, these examples are based on my approach to learning the integration.
Libraries to Import
You’ll need the json and requests libraries. The former is included with Python, while the latter can be installed separately.
Create a Launchpoint Service
In Marketo, set up a Launchpoint service for your Python script. Document your Munchkin Account ID, Client ID, and Client Secret, as they’ll be used in the code.
Creating Instance Variables
Start by importing the libraries and creating variables for your account credentials. Substitute your 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
Use the authentication endpoint to retrieve an access token. The following example uses the previously defined variables:
#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())
mat = (mkto_auth['access_token'])
Calling Other Marketo Endpoints
With your access token stored, you can call other endpoints, such as the Get Form By Id endpoint. Here’s an example:
get_forms = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/form/2244.json?access_token={mat}')
get_forms_json = get_forms.json()
print(get_forms_json)
Complete Script
Here’s the full script to initiate a call and fetch form data from Marketo:
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())
mat = (mkto_auth['access_token'])
#get forms
get_forms = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/form/2244.json?access_token={mat}')
get_forms_json = get_forms.json()
print(get_forms_json)