Skip to main content

Hello!

 

I’m a relatively new developer, especially in the world of Klaviyo. I’m trying to update the campaign message using the docs found at this link:

 

https://developers.klaviyo.com/en/reference/update_campaign_message?utm_source=google-search&utm_medium=paid-search&utm_campaign=7013o000002L2zGAAS&utm_content=NA-Brand-Features-SMS&utm_keyword=klaviyo%20sms&utm_matchtype=b&gad_source=1&gclid=Cj0KCQjw2ou2BhCCARIsANAwM2FyvT-6z6X_8GYiA2UO_f_8Ngt_C30mFTmcrPU5k7Vn9jcax4tnSxQaAg1fEALw_wcB&gclsrc=aw.ds

 

I keep getting a failure which says the authentication isn’t working. I know my API key works because it works in a different part of my code. This is the code that I’m using (help from ChatGPT):

 

import requests

def update_campaign_message(campaign_message_id, subject, preview_text, from_email='x@mycompany.com', from_label='X Company', reply_to_email='x@mycompany.com', cc_email=None, bcc_email=None):
    """
    Updates the content of an existing campaign message in Klaviyo with custom details.

    Parameters:
        campaign_message_id (str): The ID of the campaign message to update.
        subject (str): The subject of the email.
        preview_text (str): The preview text of the email.
        from_email (str): The sender email address.
        from_label (str): The sender name.
        reply_to_email (str): The reply-to email address.
        cc_email (str, optional): The CC email address.
        bcc_email (str, optional): The BCC email address.

    Returns:
        response (dict): The JSON response from the Klaviyo API, or an error message.
    """


    url = f"https://a.klaviyo.com/api/campaign-messages/{campaign_message_id}/"

    # Payload to update the campaign message with the new content
    payload = {
        "data": {
            "type": "campaign-message",
            "id": campaign_message_id,
            "attributes": {
                "label": "Updated Campaign Message",
                "content": {
                    "subject": subject,
                    "preview_text": preview_text,
                    "from_email": from_email,
                    "from_label": from_label,
                    "reply_to_email": reply_to_email
                },
                "render_options": {
                    "shorten_links": True,
                    "add_org_prefix": True,
                    "add_info_link": True,
                    "add_opt_out_language": False
                }
            }
        }
    }

    # Headers for the request
    headers = {
        "accept": "application/json",
        "revision": revision,
        "content-type": "application/json",
        "Authorization": f"Klaviyo-API-Key {KLAVIYO_API_KEY}"
    }

    try:
        # Make the PATCH request to update the campaign message content
        response = requests.patch(url, json=payload, headers=headers)
        
        # Check if the request was successful
        response.raise_for_status()
        
        # Return the JSON response
        return response.json()
    
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
        return None
    except Exception as err:
        print(f"Other error occurred: {err}")
        return None

 

 

Thank you for your help!

Hi @mecrack2000

Welcome to the world of development! Glad that working with Klaviyo could be one of your first projects.

1. Please provide a log showing the complete error: error message in addition to http status code. While the API key may have worked in other parts of the code, it's possible there is an issue with your account accessing this particular campaign ID.
2. Verify that the account bound to that API key has access to that campaign ID by calling the following endpoint with the same "campaign_message_id" as you are using in your original PATCH request.

GET https://a.klaviyo.com/api/campaign-messages/{id}/

~Chloe


Reply