Marketo REST Python Client
MarketoRestPython is a Python client library that wraps the Marketo REST API, providing a convenient interface for interacting with Marketo's marketing automation platform. It handles common concerns such as authentication, error handling, and API rate limiting. The library is actively maintained, with version 0.5.25 being the latest, and receives frequent updates to support new Marketo API features and address bug fixes.
Common errors
-
Exception: unauthorized
cause Incorrect `client_id` or `client_secret` provided during `MarketoClient` initialization.fixVerify that your `client_id` and `client_secret` are correct and have not expired. These can be found in Marketo Admin > LaunchPoint > View Details. Ensure the associated API user has the necessary permissions. -
Exception: Access token invalid (code 601) or Access token expired (code 602)
cause The authentication token used for API calls is either invalid or has expired. While the library attempts to refresh tokens, specific configurations or long-running processes might expose this.fixEnsure your Marketo credentials (`munchkin_id`, `client_id`, `client_secret`) are correct. The library is designed to handle token refreshing, but if you're manually managing tokens or experiencing persistent issues, review Marketo's authentication documentation for best practices on token management. If code 601 or 602 is returned by the API, the library should internally request a new token. -
HTTP Get Exception! Retrying..... or SSLV3_ALERT_HANDSHAKE_FAILURE
cause This error, particularly on macOS, can be related to issues with the `requests` library's SSL configuration or outdated system libraries.fixUpgrade the `requests` library with its security extras: `pip install requests[security]`. Ensure your Python environment and operating system's SSL certificates are up to date. -
Marketo API call returns HTTP 200 OK, but no data or unexpected data is returned (success: false)
cause The Marketo API frequently returns HTTP 200 even for failed operations, with the actual success indicated by a `success: false` field in the JSON response body, accompanied by an `errors` array.fixAlways parse the JSON response and explicitly check `response.get('success')`. If `False`, iterate through `response.get('errors', [])` to understand the specific reason for the failure and handle it appropriately in your code.
Warnings
- breaking The underlying Marketo REST API will remove support for passing `access_token` as a query parameter on July 31, 2026. All API calls must use the `Authorization` HTTP header. While `marketorestpython` handles this correctly by default (passing tokens in headers), custom code or older versions bypassing the client's internal handling will break.
- gotcha Marketo API requests have strict daily quotas (e.g., 50,000 calls/day) and rate limits (e.g., 100 calls/20 seconds). Exceeding the daily quota will result in a fatal error (code `1029` for 'Too many jobs in queue' or 'Export daily quota') and will stop further processing, rather than retrying indefinitely.
- gotcha Marketo API often returns an HTTP 200 OK status even if an operation fails at the Marketo level. Success is indicated by a `success: true` field within the JSON response body. The response may contain an `errors` array with specific error codes and messages if `success` is `false`.
Install
-
pip install marketorestpython
Imports
- MarketoClient
from pythonmarketo.client import MarketoClient
from marketorestpython.client import MarketoClient
Quickstart
import os
from marketorestpython.client import MarketoClient
munchkin_id = os.environ.get('MARKETO_MUNCHKIN_ID', 'YOUR_MUNCHKIN_ID')
client_id = os.environ.get('MARKETO_CLIENT_ID', 'YOUR_CLIENT_ID')
client_secret = os.environ.get('MARKETO_CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
if not all([munchkin_id, client_id, client_secret]):
raise ValueError("Marketo credentials (MUNCHKIN_ID, CLIENT_ID, CLIENT_SECRET) must be set as environment variables or provided directly.")
try:
# Initialize the Marketo client
# api_limit and max_retry_time are optional parameters for fine-tuning
mc = MarketoClient(munchkin_id, client_id, client_secret, api_limit=None, max_retry_time=300)
# Example: Get activity types
print("Fetching Marketo Activity Types...")
activity_types = mc.execute(method='get_activity_types')
if activity_types and activity_types.get('success'):
print(f"Successfully retrieved {len(activity_types['result'])} activity types.")
for activity_type in activity_types['result'][:5]: # Print first 5 for brevity
print(f" ID: {activity_type['id']}, Name: {activity_type['name']}")
elif activity_types and 'errors' in activity_types:
print(f"Error fetching activity types: {activity_types['errors']}")
else:
print("Failed to retrieve activity types with an unknown error.")
except Exception as e:
print(f"An error occurred: {e}")