Start journeys from Klaviyo
A Klaviyo flow can start a JustAI journey when its trigger fires, for example when an order is delivered. You add a Custom action to the flow. The action sends the triggering event to the journey and saves the person’s holdout bucket on their Klaviyo profile. A split after the action keeps everyone the journey doesn’t take on the emails the flow already sends.
This page covers starting a journey. Sending the journey’s messages through Klaviyo is a separate setup. See Send through Klaviyo.

How it works
Section titled “How it works”- The flow’s trigger fires for a person.
- The custom action sends the person’s email, the event’s ID, and the event’s properties to the journey.
- JustAI accepts the request and answers with the person’s holdout bucket,
treatmentorholdout. - The action saves the bucket on the Klaviyo profile as
justai_holdout_bucket, withjustai_holdout_version. - A split sends everyone whose bucket isn’t
treatmentdown the flow’s existing path. People intreatmentget the journey’s messages from JustAI.
If your workspace holds nobody back, or the journey doesn’t use a holdout, everyone gets treatment. Your JustAI team sets up holdouts with you.
Each time it runs, the action first sets the bucket to pending, so a request that fails leaves the person on the existing path instead of reusing an earlier event’s bucket. If JustAI answers without a bucket, the action saves accepted, which also follows the existing path.
Before you begin
Section titled “Before you begin”- The flow starts from a metric, such as an order or a delivery. Flows that start from a list, a segment, or a date property don’t pass an event, so this code doesn’t work for them.
- Your Klaviyo account can use custom actions. Klaviyo offers them only to larger accounts. See Klaviyo’s custom action guide.
- You can edit flows in that Klaviyo account, and someone on your team can read a short Python function.
- The journey exists in JustAI. Creating a Platform API key needs an organization admin.
- You know which person ID your customer profiles use. The code on this page sends the person’s email address as the user ID. If your profiles use a different ID, ask your JustAI team what to send.
Step 1: Copy the endpoint and create an API key
Section titled “Step 1: Copy the endpoint and create an API key”-
In JustAI, open the journey and select the Starts when card.
-
In How people get in, set Starts when to API triggered.
Leave Recipient email field empty. The grey
properties.gift.toin the field is only an example. With the field empty, the journey starts for the person the request names. -
Under How to integrate, copy the POST endpoint.
-
Select Create & copy.
JustAI creates a Platform API key, copies it, and remembers it in this browser. Keep it somewhere safe. You add it to Klaviyo in step 3. If this browser already remembers a key, you see Replace key instead. Use the key you saved, or select Replace key to create a new one.
-
Open the journey’s Settings. Under Entering again, choose Any time.

If you work in a non-production environment, How to integrate also shows an environment header. Send it with each request. See Send to a non-production environment.
Step 2: Add the custom action to the flow
Section titled “Step 2: Add the custom action to the flow”-
In Klaviyo, open the flow that should start the journey.
-
Open Actions and drag Custom action from Data to just below the trigger.

-
Choose Python, pick a blank action, and confirm with Select.
The first time you create a custom action, Klaviyo asks you to authorize its code app.
-
Give the action a name your team will recognize, for example
JustAI trigger.
Step 3: Add the API key and modules
Section titled “Step 3: Add the API key and modules”-
Open the action and select Edit to open its editor.
-
Under Editor, open Environment Variables and select Add a variable. Set the key to
X-Api-Keyand the value to the Platform API key from step 1.Klaviyo masks the value in the list.

-
Open Modules and check that
requestsis listed. If it isn’t, select Add new module and add it. Klaviyo preinstalls theklaviyomodule the code also uses.
Step 4: Add the code
Section titled “Step 4: Add the code”Open Code and replace its contents with the function below. Set TRIGGER_URL to the POST endpoint you copied in step 1. Then select Deploy.
import os
import klaviyoimport requests
TRIGGER_URL = "https://platform.justwords.ai/api/orgs/<your-org>/scripts/<journey-id>/trigger"API_KEY = os.environ["X-Api-Key"]
def save_bucket(profile_id, props): klaviyo.Profiles.update_profile( profile_id, {"data": {"type": "profile", "id": profile_id, "attributes": {"properties": props}}}, )
def handler(event, profile, context): profile_id = profile["data"]["id"] email = profile["data"]["attributes"]["email"] event_data = event["data"]
# Replace the last event's bucket first. If this request fails, the person # stays on your existing emails. save_bucket(profile_id, {"justai_holdout_bucket": "pending"})
response = requests.post( TRIGGER_URL, json={ "user_id": email, "message_id": event_data["id"], "properties": {**event_data["attributes"]["event_properties"], "email": email}, }, headers={"X-Api-Key": API_KEY}, timeout=10, ) response.raise_for_status() result = response.json()
holdout = result.get("holdout") if holdout is None: # No bucket: keep the person on your existing emails. props = {"justai_holdout_bucket": result.get("status") or "none"} else: props = { "justai_holdout_bucket": holdout["bucket"], "justai_holdout_version": holdout["version"], } save_bucket(profile_id, props) print(f"Saved to profile {profile_id}: {props}, JustAI answered {result}")
The request carries three fields:
| Field | Value | Why |
|---|---|---|
user_id |
The profile’s email address | Names the person. It has to match the user ID on your customer profiles. |
message_id |
The Klaviyo event’s ID | Klaviyo retries a failed action. With the same ID, JustAI returns the first run instead of starting the journey again. |
properties |
The event’s properties, plus email |
Fields the journey’s messages can use, such as the items in an order. |
Send to a non-production environment
Section titled “Send to a non-production environment”Add the environment header from How to integrate to the request’s headers:
headers={"X-Api-Key": API_KEY, "X-JustAI-Environment": "<environment key>"},Use the endpoint that How to integrate shows in that environment.
Step 5: Test before you go live
Section titled “Step 5: Test before you go live”A test in Klaviyo runs your code for real, against a recent event you pick.
-
Keep the journey unpublished for the first test.
-
In the action’s editor, open Test and select Run test. Pick a recent event.
JustAI starts no one while the journey isn’t live, so the test fails with
409 Client Error: Conflict. That is expected. -
In JustAI, check Test request under How to integrate.
It shows the fields JustAI received, so you can use them in the journey’s messages.

When the journey is live, the test prints the saved bucket and JustAI’s answer, which looks like this:
{ "run_id": "trigger-0f3c9a2e7b1d4c58a6e2f9b0c4d7e1a3", "status": "accepted", "holdout": { "bucket": "treatment", "version": 2 }}version is null when your workspace holds nobody back or the journey doesn’t use a holdout.
Step 6: Keep holdout people on your existing emails
Section titled “Step 6: Keep holdout people on your existing emails”-
Drag Split from Logic to just below the custom action.
-
Set Path #1 to profiles whose
justai_holdout_bucketis not equal totreatment. Connect it to the emails the flow already sends. -
Leave Everyone else without messages. JustAI sends those people the journey.

The split reads the bucket the custom action just saved. Profiles marked pending or accepted also take Path #1.
Try it with testers first
Section titled “Try it with testers first”Don’t filter the whole flow to testers. A flow filter keeps everyone else out of the flow, so they stop getting your existing emails too. Gate testers on a path instead:
-
Add a split between the trigger and the custom action.
-
Send profiles that match your testers, for example members of a tester list, down the first path to the custom action and the holdout split.
-
Connect the other path straight to your existing emails.
-
When testers’ events start the journey and their profiles show
justai_holdout_bucket, delete this split so everyone reaches the custom action.
Step 7: Go live
Section titled “Step 7: Go live”-
Publish the journey in JustAI.
Do this first. Requests to an unpublished journey fail, and Klaviyo retries each one before it gives up.
-
In Klaviyo, open the custom action and change Status from Draft to Live.
Manual holds each person under Needs Review until someone approves them. Use it only for tests.
-
Turn the flow on with Review and turn on.

Check your work
Section titled “Check your work”After the first real event:
- The action’s Logs list the run. Select it to see the printed line with the bucket and the run ID.
- The person’s Klaviyo profile has
justai_holdout_bucketandjustai_holdout_version. - The journey in JustAI shows the person entering. Check this too: JustAI can answer before it finishes letting the person in.
Troubleshooting
Section titled “Troubleshooting”Find the error in the action’s Test result or, once the action is live, in Logs. Select a run to see its output and any error.
NameError: name ‘…’ is not defined
Section titled “NameError: name ‘…’ is not defined”The code uses a name that no longer exists, often a test-only list of allowed emails that was removed while the line that checks it stayed. Remove every line that uses the name, or restore the code on this page, and select Deploy again.
KeyError: ‘X-Api-Key’
Section titled “KeyError: ‘X-Api-Key’”The action has no environment variable with that exact key. Add it under Environment Variables, or change os.environ["X-Api-Key"] to match the key you used.
400 Client Error: Bad Request
Section titled “400 Client Error: Bad Request”The request is missing user_id, usually because the profile has no email address. Profiles without an email can’t start this journey. Filter them out of the flow, or send the person ID your customer profiles use.
401 Client Error: Unauthorized
Section titled “401 Client Error: Unauthorized”The API key is wrong, was revoked, or was replaced. Create a new key with Create & copy, update the X-Api-Key variable, and select Deploy.
403 Client Error: Forbidden
Section titled “403 Client Error: Forbidden”The API key belongs to a different organization or environment than the endpoint. Copy the POST endpoint and create the key in the same organization and environment. If the error continues, contact your JustAI team.
404 Client Error: Not Found
Section titled “404 Client Error: Not Found”The endpoint doesn’t name a journey in your organization. Copy the POST endpoint from the journey again and update TRIGGER_URL.
409 Client Error: Conflict
Section titled “409 Client Error: Conflict”The journey isn’t live. It’s a draft or paused, so no one entered it. Publish the journey. Before launch this response is expected, and JustAI keeps the request’s fields under Test request.
413 Client Error
Section titled “413 Client Error”The request is larger than about 60 KB, usually because the event carries very large properties. Send only the properties the journey needs instead of every event property.
The bucket is saved, but the person never enters the journey
Section titled “The bucket is saved, but the person never enters the journey”JustAI answered the request, then didn’t let the person in. It checks Who can enter, Entering again, and Recipient email field after it answers. Set Entering again to Any time and Who can enter to Everyone it starts for, leave Recipient email field empty, and gate testers with a split before the custom action instead. See Try it with testers first.
The profile shows pending
Section titled “The profile shows pending”The request failed after the action reset the bucket, so the person stays on your existing emails. Find the error in the run’s output and fix it with the sections above. Klaviyo retries the action, and a later success replaces pending.
The profile shows accepted
Section titled “The profile shows accepted”JustAI accepted the request without assigning a bucket, so the person follows your existing emails and may also get the journey. This is rare and depends on how the journey’s holdout is set up. Ask your JustAI team to check the journey.
The action times out or retries
Section titled “The action times out or retries”The code waits up to 10 seconds for JustAI, and Klaviyo stops a custom action after 12 seconds. Klaviyo retries a failed action up to three times. Each retry sends the same event ID, so JustAI doesn’t start the journey twice.
People wait under Needs Review
Section titled “People wait under Needs Review”The action’s Status is Manual. Approve them under Needs Review, or set Status to Live.
Logs are empty
Section titled “Logs are empty”Klaviyo lists runs under Logs only after the action is live. Use Test before that.
Events wait under Queued or Rate Limited
Section titled “Events wait under Queued or Rate Limited”Klaviyo runs a custom action for at most 10 people per second, so large bursts wait their turn. You don’t need to resend them.
Profiles don’t get justai_holdout_bucket
Section titled “Profiles don’t get justai_holdout_bucket”The action didn’t run for that person, or it failed before it could save anything, for example with a KeyError. Check the run in Logs, and check that the split comes after the custom action, not before it.
A person gets both your Klaviyo emails and the journey
Section titled “A person gets both your Klaviyo emails and the journey”The split is sending treatment people down the existing path. Check that Path #1 is set to justai_holdout_bucket not equal to treatment, and that the existing emails sit only on that path.
A message is missing event details
Section titled “A message is missing event details”The message uses a property the event didn’t carry. Compare the event’s properties in Klaviyo with the fields under Test request in JustAI, and give the message a fallback for properties some events don’t have.
