(Beta) Add a code action to a flow
Execute custom Python and Node.js functions in flows with code actions.
Adding a code action to a flow is a beta feature. To be added to the beta, you must submit our early access form.
You will learn
Learn how to add a code action to a flow in Klaviyo that executes custom functions in Python or Node.js. Custom functions allow you to move data and build a personalized experience for your business.
Before you begin
Code actions require custom development and experience with either Python or Node.js. Klaviyo’s support team is unable to offer hands-on assistance. If you are not a developer, consider reaching out to a Klaviyo Partner for assistance.
Code action use cases
Example use cases for code actions include:
- Normalizing Placed Order values across storefronts that use different currencies.
- Modifying an event payload and forwarding to an external webhook endpoint.
- Saving parts of an event payload as customer profile properties.
- Using GPT-3 to generate personalized text that thanks a customer for their order.
- Setting a customer's birthdate on their profile after they sign up.
- Sending a Slack or Microsoft Teams notification in response to a given event.
Add a code action to your flow
- Navigate to a specific flow.
- In the flow builder, drag and drop a Code action into the flow.
- In the sidebar that appears, select either Python or Node.js.
- Click on a prebuilt function or a blank Python or Node.js function.
If you need help designing a function from scratch, view a prebuilt function as an example. Each example includes a Readme section explaining the function.
- In the modal showing the preview, click Select. After selecting a function, you will be brought to an interface with the following tabs:
- Editor
- Test output
- Logs
- Needs review
Read the sections below for information on each tab.
Editor
The Editor tab contains 3 sub-tabs:
- Code
The code tab has an editor where you can write Python or Javascript code. - Modules
Modules are 3rd party packages that add functionality to your functions. - Environment Variables
Environment variables are the key-value pairs that your function accesses when it runs.
Code
On the Code tab, you’ll see an editor you can use to write Python or Node.js functions that are executed in response to the selected topic.
In order for Klaviyo to execute the code you write, you need to wrap it in a function named handler
that accepts 2 parameters:
event
Consists of the event data associated with the triggering event in JSON:API format. If the trigger is non-event based (e.g., Added To List), then the value of this parameter will beNone
ornull
depending on the language.context
Contains additional metadata about the function execution, including the profile associated with the function invocation. You can access the profile object viacontext.profile
in Javascript orcontext["profile"]
in Python.
In Python, defining the
handler
is all that’s needed, but with Node.js, you must alsoexport
the function so that Klaviyo can access it. In Node.js, it’s good practice to declare your handler function asasync
, so that you can await Promises inside it.
Modules
On the Modules tab, you can select from the most popular prebuilt modules (i.e., 3rd-party packages that add capabilities to your functions).
To add an external module to use in your code:
- Click Add module.
- On the modal that appears, search and select the module you’d like to add:
Once added, you can use the module with the code you write in Klaviyo.
Refer to the external module’s native documentation for information regarding how to use it.
Environment variables
The Environment variables tab allows you to set key-value pairs that your code in Klaviyo can reference when it runs. These can be used to store information like credentials and secret keys so your functions can access them as they run.
To add an environment variable:
- Click Add a variable.
- On the modal that appears, you can set a key-value pair for your environment variable.
Once created, the environment variables will be listed on the page and can be used in your code.
To access your environment variables in your code, use
os.getenv("Key")
for Python, orprocess.env.Key
for Node.js.
Test output
The Test output tab allows you to test your code with recent events to confirm that the output is behaving as expected.
To test your function:
- Click Run Test.
- On the modal that appears, select a profile or event to test with. You can select profiles from the 10 most recent events captured in Klaviyo.
After selecting an event to test with, the test output will be shown.
Logs
The Logs tab shows the health of the currently viewed function. You’ll see:
-
Profile
The email address of the profile associated with the given function execution. Click the email to navigate to the profile page. -
Status
The progress of a function’s execution. -
Run ID
The unique ID of this function execution. Click the ID to open a side drawer with
additional execution information, such as the function logs. -
Response time
The amount of time it takes for your code to execute in response to an event. -
Date
A timestamp of when the function was executed.
Needs Review
If the code action is set to manual status, profiles will be queued in the Needs Review tab to be manually approved for the code to execute.
Deploy the code and set it live
- In the code editor, click Deploy in the upper right to deploy your local code changes. Any changes will be preserved even if you leave the page, but only deployed changes will be reflected in subsequent flow evaluations. If you chose a prebuilt function and didn’t make any changes, it will automatically be deployed.
- Click Done in the upper right.
- Click on the code action again in the flow builder.
- In the sidebar, rename the action if desired.
- Click the Status dropdown to change the status from Draft to Manual or Live.
- Click Save.
Additional resources
Updated about 1 month ago