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:
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!