CFML Assistance Requested

  • 3 February 2023
  • 3 replies

Userlevel 1
Badge +1

We are trying to integrate Klaviyo within a CFML (Lucee, ColdFusion) environment, but there is very little help in the documents for this language.


Are there any other CFML developers using Klaviyo?


Right now, I could just use a couple of examples, such as how to authenticate. The documents give you one line with no context surrounding it.  Is this in a POST?   Is this in a Javascript?    In any case, does anyone (CFML, JQuery, JavaScript) have a working example (with your credentials dummied up, of course) that you can share with me to get me started?

Thank you!


Best answer by rlscsi 10 February 2023, 16:11

View original

3 replies

Userlevel 1
Badge +1

For CFML Programmers who end up here looking for that kick to get you rolling, here’s a short intro, written CFML’er to CFML’er.

In your Application.cfc/cfm or other page that loads with every refresh, you need to identify who you are with something like this:

<script async type="text/javascript" src="//"></script>

Of course, you would have your Public and Private API keys from Klaviyo generated and variable-ized. We determine if we’re running this in test or production and load those variables accordingly.

Then you can write a CFHTTP call to manage the information.  Remember, you can use CFHTTP to POST, GET, and PATCH. Pay attention to the Klaviyo docs as to which to use for what operation. Their docs also let you start filling out the call so you can see which URL to use by using the drop-downs and inputs on that page (example:

I found it easiest to put the functions I need into a CFML component, and then, after creating an object from the component, it became quite easy to get what I needed with a more familiar interface.

For instance, here’s the component function to retrieve a profile -- This may or may not be the most efficient way to write this, but this will at least give you the kick in the pants to move ahead with more confidence. 

NOTE THAT we store our profiles with our Company ID so I am using that as my “filter” term in my request for the profile.   Oh -- and the revision (see below) is needed by Klaviyo to know which version of the API you’re basing this request on.   All this information is in their documents, but it’s scattered far and wide with nothing put in one nice example package to give you a clue on how it all fits together, as far as I could see.

    <!--- ****** Get One Klaviyo Profile ****** --->
    <cffunction name="get_Klaviyo_Profile" returnType="struct" access="public"
            displayName="Get One Klaviyo Profile"
            description = "I return a structure containing the Klaviyo profile for one person based on Customer ID number.">
        <cfargument name="KlaviyoPrivateAPI" type="string"  required="yes" displayname="Klaviyo Private API">
        <cfargument name="customer_id"   type="numeric" required="yes" displayname="Customer ID">

        <cfset VAR myResult = structNew() />

        <cfhttp method="get"
            <cfhttpparam type="header" name="Authorization" value="Klaviyo-API-Key #ARGUMENTS.KlaviyoPRivateAPI#">
            <cfhttpparam type="header" name="revision" value="2023-01-24">
            <cfhttpparam type="header" name="Accept" value="application/json">
            <cfhttpparam type="header" name="Content-Type" value="application/json">
        <cfset kResult = deserializejson(klaviyoSelectFieldsFilteredProfile.filecontent) />
        <cfset myResult.Klaviyo_Profile_ID = kResult['data'][1]['id'] />
        <cfset myResult.first_name = kResult['data'][1]['attributes']['first_name'] />
        <cfset myResult.last_name = kResult['data'][1]['attributes']['last_name'] />
        <cfset = kResult['data'][1]['attributes']['email'] />
        <cfset myResult.customer_id = kResult['data'][1]['attributes']['external_id'] />
        <cfset myResult.phone_number = kResult['data'][1]['attributes']['phone_number'] />
        <cfset myResult.anonymous_id = kResult['data'][1]['attributes']['anonymous_id'] />
        <cfset myResult.organization = kResult['data'][1]['attributes']['organization'] />
        <cfset myResult.title = kResult['data'][1]['attributes']['title'] />
        <cfset myResult.image = kResult['data'][1]['attributes']['image'] />
        <cfset myResult.address1 = kResult['data'][1]['attributes']['location']['address1'] />
        <cfset myResult.address2 = kResult['data'][1]['attributes']['location']['address2'] />
        <cfset = kResult['data'][1]['attributes']['location']['city'] />
        <cfset = kResult['data'][1]['attributes']['location']['country'] />
        <cfset myResult.latitude = kResult['data'][1]['attributes']['location']['latitude'] />
        <cfset myResult.region = kResult['data'][1]['attributes']['location']['region'] />
        <cfset = kResult['data'][1]['attributes']['location']['zip'] />
        <cfset myResult.timezone = kResult['data'][1]['attributes']['location']['timezone'] />
        <cfreturn myResult />

So once you have this in a component (for example “oKlaviyoFunctions”, e.g. oKlaviyoFunctions   = CreateObject("component","components.klaviyo") ), you can retrieve your results by a simple function call, such as:




I hope this helps to reduce the hair pulling and gnashing of teeth for you. It would have zoomed me to the productivity line much more quickly.

Userlevel 7
Badge +33

Hi @rlscsi and welcome to the Community!


I’m not too familiar with CFML-based integrations, but you may be able to find help with one of our official Klaviyo Partners since this is a custom integration.


Also, here are some key developer docs in case you haven’t seen them:


I’m excited to see if other members can provide their tips for this type of integration, and thanks for using the Community!

- Brian

Userlevel 1
Badge +1

I’ve made some advances, but still getting Invalid API key. I’ve tried both public and private keys for both test and production sites.


    <cfset useKey = rls_Klaviyo_Private_API_Key />








        <cfhttpparam type="header" name="Klaviyo-API-Key" value=useKey>

        <cfhttpparam type="header" name="revision" value="2023-01-24">

        <cfhttpparam type="header" name="Accept" value="application/json">

        <cfhttpparam type="header" name="Content-Type" value="application/json">