Featured image for blog post on handling errors and warnings in the Marketo API

Marketo API: Errors, Warnings, & Results

When you call a Marketo API endpoint it will most likely return a success – meaning return a response code of 200 –  to Python, even if the call wasn’t a success with Marketo. This means that if you use a Try/Except block, it won’t pick up an error and run the Except block.This means we have to specifically look for errors or warnings with the JSON response itself.

Success = False: A Smart Campaign That Doesn’t Exist

If you use the get smart campaign by id endpoint but enter an id for a smart campaign that doesn’t exist (you can try something like 123456789) you will notice that you get a value of false for the success key and a list with two values for the errors key: a code of ‘702’ and a message of ‘Smart Campaign not found’.

Success = True But With Warning

Oddly, if you do the same thing with the get landing page by id endpoint, it will return a success of true but it will come with a warning of ‘No assets found for the given search criteria’.

Access Token Expired

When you first get an access token, it will expire in 3600 seconds (1 hour). Each time you use the get oauth token endpoint within the hour that you first got the token, it will tell you how much time is left. After 60 minutes, the token expires and will return a 602 error with a message of ‘’Access token expired’. You now have to call that get oauth endpoint again to get a new token.

Check for results

A truly successful call will result in success=True, empty errors and warnings keys as well as having a result list with JSON value(s).

Marketo Python Script With To Handle Errors and Warnings

The script below is an example of an approach you can use where you look to see whether the success key value is true or false, and if there are any warnings, setting the warnings variable to true. If the success variable is true and the warnings variable is false then you can continue to get the result of the call. If there are errors or warnings you can either print them or add them to a file.

def some_marketo_call():
    mat = get_token()
    success = False
    warnings = False
    try:
        max_results = 200
        mkto_call = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/smartCampaign/123456.json?access_token={mat}')
        mkto_json = mkto_call.json()
        print(mkto_json)
        success = mkto_json['success']
        warnings = True if len(mkto_json['warnings']) > 0 else False

        if success == True and warnings == False:
            mkto_response = mkto_json['result']
            print(mkto_response[0]['id'])
        else:
            if success == False:
                if mkto_json['errors'][0]['code'] == '602':
                    get_token(endpoint='https://{mmc}.mktorest.com/rest/asset/v1/lmarketosky/7190.json?access_token={mat}')
                else:             
                    error_code = mkto_json['errors'][0]['code']
                    error_message = mkto_json['errors'][0]['message']
                    print(f'Error with {WHATEVER VARIABLE YOU ARE USING FOR ID} with a code of {error_code} and message of {error_message}')
            else:
                warning = mkto_json['warnings']
                print(f'Warning for {WHATEVER VARIABLE YOU ARE USING FOR ID} of {warning}')
    except Exception as e:
        print(e)

Similar Posts