Package: klaviyo-sdk
Klaviyo Python SDK (Legacy)Legacy NoticeThis SDK is set to be deprecated + retired on 2024-01-01.We have a NEW python SDK to go along with ournew API.We recommend migrating over to ournewest SDK.You can read more about our SDK release history and supporthereFor a comparison between our old and new APIs, check outthis guide.Klaviyo Python SDKSDK version: 1.0.9.20220329Helpful ResourcesAPI ReferenceAPI GuidesPostman WorkspaceDesign & ApproachThis SDK is a thin wrapper around our API. See our API Reference for full documentation on API behavior.This SDK exactly mirrors the organization and naming convention of the above language-agnostic resources, with a few namespace changes to make it Pythonic (details in Appendix).OrganizationThis SDK is organized into the following resources:CampaignsDataPrivacyListsSegmentsMetricsProfilesTemplatesTrackIdentifyInstallationpipYou can install this library usingpip.Depending on your system configuration, you will need to runoneof the following shell commands:pip install klaviyo-sdkORpip3 install klaviyo-sdksource codeAlternatively, you can also run this using this repo as source code, by runningoneof the following shell commands within the cloned repo:pip install -r requirements.txtORpip3 install -r requirements.txtand then running python from the cloned repo.Usage ExampleTo instantiate the clientfromklaviyo_sdkimportClientclient=Client("YOUR API KEY HERE",max_delay=60,max_retries=3)NOTE:The SDK retries on resolvable errors, namely: rate limits (common) and server errors on klaviyo (rare).The keyword arguments define retry behavior; the example is populated with the default values.max_delaydenotes total delay (in seconds) across all attempts.To call themetric-exportoperation:metric_id="METRIC_ID"count=100client.Metrics.metric_export(metric_id,count=count)Error HandlingThis SDK throws anApiExceptionerror when the server returns a non-2XXresponse.AnApiExceptionconsists of the following attributes:status:intreason:strbody:bytesthis can be decoded into a native python dictionary as follows:# to decode to a dictionaryimportjsonBODY_DICT=json.loads(YOUR_EXCEPTION.body)# to decode to a stringBODY_STRING=YOUR_EXCEPTION.body.decode('utf-8')headers:class 'urllib3._collections.HTTPHeaderDict'This can be interacted with as a normal dictionary:ex:date = YOUR_EXCEPTION.headers['Date']
keys = YOUR_EXCEPTION.headers.keys()
values = YOUR_EXCEPTION.headers.values()Comprehensive list of Operations & ParametersNOTE:Organization: Resource groups and operation_ids are listed in alphabetical order, first by Resource name, then byOpenAPI Summary. Operation summaries are those listed in the right side bar of theAPI Reference.For example values / data types, as well as whether parameters are required/optional, please reference the corresponding API Reference link.Some keyword args are required for the API call to succeed, the API docs above are the source of truth regarding which keyword params are required.JSON payloads should be passed in as native python dictionaries.CampaignsCancel a Campaignclient.Campaigns.cancel_campaign(campaign_id)Clone a Campaignclient.Campaigns.clone_campaign(campaign_id,name=name,list_id=list_id)Create New Campaignclient.Campaigns.create_campaign(list_id=list_id,template_id=template_id,from_email=from_email,from_name=from_name,subject=subject,name=name,use_smart_sending=use_smart_sending,add_google_analytics=add_google_analytics)Get Campaign Infoclient.Campaigns.get_campaign_info(campaign_id)Get Campaign Recipientsclient.Campaigns.get_campaign_recipients(campaign_id,count=count,sort=sort,offset=offset)Get Campaignsclient.Campaigns.get_campaigns(page=page,count=count)Schedule a Campaignclient.Campaigns.schedule_campaign(campaign_id,send_time=send_time)Send a Campaign Immediatelyclient.Campaigns.send_campaign(campaign_id)Update Campaignclient.Campaigns.update_campaign(campaign_id,list_id=list_id,template_id=template_id,from_email=from_email,from_name=from_name,subject=subject,name=name,use_smart_sending=use_smart_sending,add_google_analytics=add_google_analytics)DataPrivacyRequest a Deletionclient.DataPrivacy.request_deletion(body=body)ListsSegmentsAdd Members to a Listclient.ListsSegments.add_members(list_id,body=body)Create Listclient.ListsSegments.create_list(list_name=list_name)Delete Listclient.ListsSegments.delete_list(list_id)Exclude Profile From All Emailclient.ListsSegments.exclude_globally(email=email)Get Global Exclusions & Unsubscribesclient.ListsSegments.get_global_exclusions(reason=reason,sort=sort,count=count,page=page)Get All Exclusions for a Listclient.ListsSegments.get_list_exclusions(list_id,marker=marker)Get List Infoclient.ListsSegments.get_list_info(list_id)Check if Profiles Are in a Listclient.ListsSegments.get_list_members(list_id,body=body)Check if Profiles Are in a List and not Suppressedclient.ListsSegments.get_list_subscriptions(list_id,body=body)Get Listsclient.ListsSegments.get_lists()Get List and Segment Membersclient.ListsSegments.get_members(list_or_segment_id,marker=marker)Check if Profiles Are in a Segmentclient.ListsSegments.get_segment_members(segment_id,body=body)Remove Profiles From Listclient.ListsSegments.remove_members(list_id,body=body)Subscribe Profiles to Listclient.ListsSegments.subscribe(list_id,body=body)Unsubscribe Profiles From Listclient.ListsSegments.unsubscribe(list_id,body=body)Update List Nameclient.ListsSegments.update_list_name(list_id,list_name=list_name)MetricsGet Metrics Infoclient.Metrics.get_metrics(page=page,count=count)Query Event Dataclient.Metrics.metric_export(metric_id,start_date=start_date,end_date=end_date,unit=unit,measurement=measurement,where=where,by=by,count=count)Get Events for a Specific Metricclient.Metrics.metric_timeline(metric_id,since=since,count=count,sort=sort)Get Events for All Metricsclient.Metrics.metrics_timeline(since=since,count=count,sort=sort)ProfilesExchange ID for Profile IDclient.Profiles.exchange(body=body)Get Profileclient.Profiles.get_profile(person_id)Get Profile IDclient.Profiles.get_profile_id(email=email,phone_number=phone_number,external_id=external_id)Get Profile's Events for a Specific Metricclient.Profiles.profile_metric_timeline(person_id,metric_id,since=since,count=count,sort=sort)Get Profile's Events for all Metricsclient.Profiles.profile_metrics_timeline(person_id,since=since,count=count,sort=sort)Update Profileclient.Profiles.update_profile(person_id,params=params)TemplatesClone Templateclient.Templates.clone_template(template_id,name=name)Create New Templateclient.Templates.create_template(name=name,html=html)Delete Templateclient.Templates.delete_template(template_id)Get All Templatesclient.Templates.get_templates(page=page,count=count)Render Templateclient.Templates.render_template(template_id,context=context)Render and Send Templateclient.Templates.send_template(template_id,from_email=from_email,from_name=from_name,subject=subject,to=to,context=context)Update Templateclient.Templates.update_template(template_id,name=name,html=html)TrackIdentifyIdentify Profile (Legacy)client.TrackIdentify.identify_get(data)Identify Profileclient.TrackIdentify.identify_post(data=data)Track Profile Activity (Legacy)client.TrackIdentify.track_get(data)Track Profile Activityclient.TrackIdentify.track_post(data=data)AppendixLimitationsTheapi_keyis set at the global level: this means that if you manage multiple stores, you will need to run the code for each store in a different environmentRefresher on catching exceptions:try:YOUR_CALLexceptExceptionase:print(e.status)print(e.reason)print(e.body)print(e.headers)NamespaceIn the interest of making the SDK Pythonic, we made the following namespace changesrelativeto the language agnostic resources up top.dashes-become underscores_all other non-alphanumeric symbols, including spaces, are removed.For example:get-campaignsbecomesget_campaignsTrack & IdentifybecomesTrackIdentifyParameters & ArgumentsThe parameters follow the same naming conventions as the resource groups and operations.We stick to the following convention for parameters/argumentsAll parameters are passed as function args.All optional params, as well as all Body and Form Data params (including required ones), are passed as keyword args.All query and path params that are tagged asrequiredin the docs are passed as positional args.There is no need to pass in your privateapi_keyfor any operations, as it is defined upon client instantiation; public key is still required where.