Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(workflows): transactional emails in the template library (#117726)

Harley Alexander8d5b0b1ff530
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index 441f1f537960..a5b2e09e4863 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -642,6 +642,7 @@ export const FEATURE_FLAGS = {
WORKFLOWS_LINEAR_VIEW: 'workflows-linear-view', // owner: #team-workflows
WORKFLOWS_OVERVIEW_TAB: 'workflows-overview-tab', // owner: @dmarchuk #team-workflows, experimental Overview tab on a workflow
WORKFLOWS_PUSH_NOTIFICATIONS: 'workflows-push-notifications', // owner: #team-workflows
+ WORKFLOWS_TRANSACTIONAL_EMAIL_API: 'workflows-transactional-email-api', // owner: @mayteio #team-workflows
WORKFLOWS_TRIGGER_VOLUME_ESTIMATE: 'workflows-trigger-volume-estimate', // owner: @mayteio #team-workflows
XAA_AUTHENTICATION: 'xaa-authentication', // owner: @reecejones #team-platform-features
} as const
diff --git a/products/workflows/frontend/TemplateLibrary/MakeTransactionalPrompt.tsx b/products/workflows/frontend/TemplateLibrary/MakeTransactionalPrompt.tsx
new file mode 100644
index 000000000000..02d739e89ca6
--- /dev/null
+++ b/products/workflows/frontend/TemplateLibrary/MakeTransactionalPrompt.tsx
@@ -0,0 +1,49 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { messageTemplateLogic } from './messageTemplateLogic'
+import { MessageTemplateSceneLogicProps } from './messageTemplateSceneLogic'
+
+export function MakeTransactionalPrompt(props: MessageTemplateSceneLogicProps): JSX.Element {
+ const logic = messageTemplateLogic(props)
+ const { template, templateChanged, isMakingTransactional } = useValues(logic)
+ const { makeTransactional } = useActions(logic)
+
+ const email = template.content.email
+ const disabledReason = templateChanged
+ ? 'Save your changes first'
+ : !email?.subject
+ ? 'Add a subject first'
+ : !(email.html || email.text)
+ ? 'Add a body first'
+ : undefined
+
+ return (
+ <div className="flex flex-col gap-4 max-w-200" data-attr="make-transactional-prompt">
+ <div>
+ <h3 className="mb-1">Send this email from your code</h3>
+ <p className="mb-0">
+ Make this template transactional to send it with one API call. Use it for emails a person triggers,
+ like password resets, receipts and sign-in codes.
+ </p>
+ </div>
+ <ul className="list-disc pl-6 mb-0 flex flex-col gap-1">
+ <li>Each send shows up here under Metrics and Sends, and on the recipient's person.</li>
+ <li>Saving the template updates the live email straight away. Every save keeps a version.</li>
+ <li>Transactional emails ignore marketing opt-outs, so don't use them for marketing.</li>
+ </ul>
+ <div>
+ <LemonButton
+ data-attr="make-message-template-transactional"
+ type="primary"
+ onClick={makeTransactional}
+ loading={isMakingTransactional}
+ disabledReason={disabledReason}
+ >
+ Make transactional
+ </LemonButton>
+ </div>
+ </div>
+ )
+}
diff --git a/products/workflows/frontend/TemplateLibrary/MessageTemplate.tsx b/products/workflows/frontend/TemplateLibrary/MessageTemplate.tsx
index caa737b6b7e8..9b2d9147420e 100644
--- a/products/workflows/frontend/TemplateLibrary/MessageTemplate.tsx
+++ b/products/workflows/frontend/TemplateLibrary/MessageTemplate.tsx
@@ -1,9 +1,11 @@
-import { useActions, useValues } from 'kea'
+import { BindLogic, useActions, useValues } from 'kea'
import { Form } from 'kea-forms'
-import { LemonBanner, LemonButton, LemonDivider, Spinner, SpinnerOverlay } from '@posthog/lemon-ui'
+import { LemonBanner, LemonButton, LemonDivider, LemonTag, Spinner, SpinnerOverlay } from '@posthog/lemon-ui'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { More } from 'lib/lemon-ui/LemonButton/More'
+import { LemonTabs } from 'lib/lemon-ui/LemonTabs'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { EmailTemplater, TemplatePickerModal } from 'scenes/hog-functions/email-templater/EmailTemplater'
import { emailTemplaterLogic } from 'scenes/hog-functions/email-templater/emailTemplaterLogic'
@@ -15,6 +17,11 @@ import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { ProductKey } from '~/queries/schema/schema-general'
+import { WorkflowInvocations } from '../Workflows/WorkflowInvocations'
+import { workflowLogic } from '../Workflows/workflowLogic'
+import { WorkflowMetrics } from '../Workflows/WorkflowMetrics'
+import { WorkflowRevisions } from '../Workflows/WorkflowRevisions'
+import { MakeTransactionalPrompt } from './MakeTransactionalPrompt'
import { messageTemplateLogic } from './messageTemplateLogic'
import { MessageTemplateSceneLogicProps, messageTemplateSceneLogic } from './messageTemplateSceneLogic'
import { messageTemplateTestSendLogic } from './messageTemplateTestSendLogic'
@@ -22,6 +29,7 @@ import { NewTemplateAgent } from './NewTemplateAgent'
import { newTemplateAgentLogic } from './newTemplateAgentLogic'
import { SendTestEmailModal } from './SendTestEmailModal'
import { NEW_TEMPLATE_AGENT_HEADLINES } from './templateAgentContext'
+import { TransactionalEmailApiExample } from './TransactionalEmailApiExample'
export const scene: SceneExport<MessageTemplateSceneLogicProps> = {
component: MessageTemplate,
@@ -45,6 +53,7 @@ export function MessageTemplate(props: MessageTemplateSceneLogicProps): JSX.Elem
setTemplatePickerOpen,
syncExternalEdit,
keepMyTemplateVersion,
+ setTransactionalTab,
} = useActions(logic)
const {
template,
@@ -56,7 +65,11 @@ export function MessageTemplate(props: MessageTemplateSceneLogicProps): JSX.Elem
templatePickerOpen,
externallyEdited,
isSyncingExternalEdit,
+ transactionalHogFlowId,
+ transactionalTab,
} = useValues(logic)
+ const transactionalEmailsEnabled = useFeatureFlag('WORKFLOWS_TRANSACTIONAL_EMAIL_API')
+ const showTransactional = transactionalEmailsEnabled && props.id !== 'new'
const { setIsSaveTemplateModalOpen } = useActions(emailTemplaterLogic)
@@ -88,6 +101,66 @@ export function MessageTemplate(props: MessageTemplateSceneLogicProps): JSX.Elem
)
}
+ const editor = (
+ <div className="flex flex-col flex-1 gap-2 min-h-0 relative">
+ {/* The editor stays mounted through a sync: the templater pushes the new design into the open canvas. */}
+ {isSyncingExternalEdit && <SpinnerOverlay />}
+ {(messageLoading || templateLoading) && !isSyncingExternalEdit ? (
+ <Spinner className="text-lg" />
+ ) : (
+ <EmailTemplater
+ value={template?.content.email}
+ onChange={(value) => setTemplateValue('content.email', value)}
+ onChangeTemplating={(templating) => setTemplateValue('content.email.templating', templating)}
+ type="native_email_template"
+ layout="inline"
+ />
+ )}
+ </div>
+ )
+
+ const tabs = (
+ <LemonTabs
+ activeKey={transactionalTab}
+ onChange={setTransactionalTab}
+ className="flex flex-col grow [&>div]:flex [&>div]:flex-col [&>div]:grow"
+ tabs={[
+ // Unmounting the editor would drop a canvas edit it hasn't exported yet.
+ { key: 'design', label: 'Design', content: editor, keepMounted: true },
+ transactionalHogFlowId
+ ? {
+ key: 'metrics',
+ label: 'Metrics',
+ content: <WorkflowMetrics id={transactionalHogFlowId} />,
+ }
+ : null,
+ transactionalHogFlowId
+ ? {
+ key: 'sends',
+ label: 'Sends',
+ content: <WorkflowInvocations id={transactionalHogFlowId} />,
+ }
+ : null,
+ transactionalHogFlowId
+ ? {
+ key: 'versions',
+ label: 'Versions',
+ content: <WorkflowRevisions id={transactionalHogFlowId} />,
+ }
+ : null,
+ {
+ key: 'api',
+ label: 'API',
+ content: transactionalHogFlowId ? (
+ <TransactionalEmailApiExample templateId={props.id} />
+ ) : (
+ <MakeTransactionalPrompt {...props} />
+ ),
+ },
+ ]}
+ />
+ )
+
return (
<Form
logic={messageTemplateLogic}
@@ -105,6 +178,13 @@ export function MessageTemplate(props: MessageTemplateSceneLogicProps): JSX.Elem
isLoading={messageLoading || templateLoading}
onNameChange={(name) => setTemplateValue('name', name)}
onDescriptionChange={(description) => setTemplateValue('description', description)}
+ nameSuffix={
+ showTransactional && transactionalHogFlowId ? (
+ <LemonTag type="success" className="ml-1">
+ Transactional
+ </LemonTag>
+ ) : undefined
+ }
actions={
<>
<LemonDivider vertical />
@@ -215,23 +295,16 @@ export function MessageTemplate(props: MessageTemplateSceneLogicProps): JSX.Elem
</LemonBanner>
)}
- <div className="flex flex-col flex-1 gap-2 min-h-0 relative">
- {/* The editor stays mounted through a sync: the templater pushes the new design into the open canvas. */}
- {isSyncingExternalEdit && <SpinnerOverlay />}
- {(messageLoading || templateLoading) && !isSyncingExternalEdit ? (
- <Spinner className="text-lg" />
- ) : (
- <EmailTemplater
- value={template?.content.email}
- onChange={(value) => setTemplateValue('content.email', value)}
- onChangeTemplating={(templating) =>
- setTemplateValue('content.email.templating', templating)
- }
- type="native_email_template"
- layout="inline"
- />
- )}
- </div>
+ {!showTransactional ? (
+ editor
+ ) : transactionalHogFlowId ? (
+ // The reused workflow tabs read workflowLogic without props, so bind it to this email's workflow.
+ <BindLogic logic={workflowLogic} props={{ id: transactionalHogFlowId }}>
+ {tabs}
+ </BindLogic>
+ ) : (
+ tabs
+ )}
</SceneContent>
</Form>
)
diff --git a/products/workflows/frontend/TemplateLibrary/MessageTemplateCard.tsx b/products/workflows/frontend/TemplateLibrary/MessageTemplateCard.tsx
index bb27d4cae3e5..4e96a05cde49 100644
--- a/products/workflows/frontend/TemplateLibrary/MessageTemplateCard.tsx
+++ b/products/workflows/frontend/TemplateLibrary/MessageTemplateCard.tsx
@@ -1,3 +1,5 @@
+import { LemonTag } from '@posthog/lemon-ui'
+
import { FallbackCoverImage } from 'lib/components/FallbackCoverImage/FallbackCoverImage'
import { TZLabel } from 'lib/components/TZLabel'
import { ProfilePicture } from 'lib/lemon-ui/ProfilePicture'
@@ -39,7 +41,14 @@ export function MessageTemplateCard({
</div>
<div className="px-2 py-2 border-t">
- <h5 className="mb-0.5">{template.name || 'Unnamed template'}</h5>
+ <div className="flex items-center gap-1 mb-0.5">
+ <h5 className="mb-0 truncate">{template.name || 'Unnamed template'}</h5>
+ {template.transactional_hog_flow_id && (
+ <LemonTag type="success" size="small">
+ Transactional
+ </LemonTag>
+ )}
+ </div>
{template.description && (
<p className="text-secondary text-xs line-clamp-1 mb-1">{template.description}</p>
)}
diff --git a/products/workflows/frontend/TemplateLibrary/MessageTemplatesTable.tsx b/products/workflows/frontend/TemplateLibrary/MessageTemplatesTable.tsx
index 92ade950cab2..62eeb6389858 100644
--- a/products/workflows/frontend/TemplateLibrary/MessageTemplatesTable.tsx
+++ b/products/workflows/frontend/TemplateLibrary/MessageTemplatesTable.tsx
@@ -5,10 +5,12 @@ import { router } from 'kea-router'
import * as readingIsMagicPng from '@posthog/brand/hoggies/png/reading-is-magic'
import { IconPlus, IconTrash } from '@posthog/icons'
+import { LemonCheckbox } from '@posthog/lemon-ui'
import { pngHoggie } from 'lib/brand/hoggies'
import { MemberSelect } from 'lib/components/MemberSelect'
import { ProductIntroduction } from 'lib/components/ProductIntroduction/ProductIntroduction'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { LemonMenuOverlay } from 'lib/lemon-ui/LemonMenu/LemonMenu'
@@ -27,9 +29,11 @@ const HedgehogReadingIsMagic = pngHoggie(readingIsMagicPng)
export function MessageTemplatesTable(): JSX.Element {
useMountedLogic(messageTemplatesLogic)
- const { filteredTemplates, templates, templatesLoading, search, createdByFilter } = useValues(messageTemplatesLogic)
- const { deleteTemplate, createTemplate, duplicateTemplate, setSearch, setCreatedByFilter } =
+ const { filteredTemplates, templates, templatesLoading, search, createdByFilter, transactionalOnly } =
+ useValues(messageTemplatesLogic)
+ const { deleteTemplate, createTemplate, duplicateTemplate, setSearch, setCreatedByFilter, setTransactionalOnly } =
useActions(messageTemplatesLogic)
+ const transactionalEmailsEnabled = useFeatureFlag('WORKFLOWS_TRANSACTIONAL_EMAIL_API')
const { startNewTemplate } = useActions(newTemplateAgentLogic)
const showProductIntroduction = !templatesLoading && templates.length === 0
@@ -72,6 +76,16 @@ export function MessageTemplatesTable(): JSX.Element {
<span className="text-secondary whitespace-nowrap">Created by:</span>
<MemberSelect value={createdByFilter} onChange={(user) => setCreatedByFilter(user?.id ?? null)} />
</div>
+ {transactionalEmailsEnabled && (
+ <LemonCheckbox
+ label="Transactional only"
+ checked={transactionalOnly}
+ onChange={setTransactionalOnly}
+ bordered
+ size="small"
+ data-attr="templates-transactional-only"
+ />
+ )}
</div>
{templatesLoading ? (
<Spinner className="text-6xl" />
diff --git a/products/workflows/frontend/TemplateLibrary/TransactionalEmailApiExample.tsx b/products/workflows/frontend/TemplateLibrary/TransactionalEmailApiExample.tsx
new file mode 100644
index 000000000000..ed76413c1fe6
--- /dev/null
+++ b/products/workflows/frontend/TemplateLibrary/TransactionalEmailApiExample.tsx
@@ -0,0 +1,45 @@
+import { useValues } from 'kea'
+
+import { Link } from '@posthog/lemon-ui'
+
+import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
+import { apiHostOrigin } from 'lib/utils/apiHost'
+import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
+
+export function TransactionalEmailApiExample({ templateId }: { templateId: string }): JSX.Element {
+ const { currentTeamIdStrict } = useValues(teamLogic)
+ const snippet = `curl -X POST ${apiHostOrigin()}/api/projects/${currentTeamIdStrict}/messaging_templates/${templateId}/send/ \\
+ -H "Authorization: Bearer $POSTHOG_PROJECT_SECRET_API_KEY" \\
+ -H "Content-Type: application/json" \\
+ -d '{
+ "identifiers": { "distinct_id": "jane-distinct-id" },
+ "message_data": { "first_name": "Jane", "reset_link": "https://example.com/reset" }
+ }'`
+
+ return (
+ <div className="flex flex-col gap-4 max-w-200" data-attr="transactional-email-api-example">
+ <p className="mb-0">
+ Send this email from your code. Each send runs once and shows up under Metrics and Sends, and on the
+ recipient's person.
+ </p>
+ <ul className="list-disc pl-6 mb-0 flex flex-col gap-1">
+ <li>
+ Authenticate with a{' '}
+ <Link to={urls.settings('environment-secret-api-keys')}>project secret API key</Link> that has the{' '}
+ <code>hog_flow:write</code> scope.
+ </li>
+ <li>
+ <code>message_data</code> fills the template's <code>{'{{ variables.* }}'}</code> placeholders. The
+ template can't read person properties, so pass everything it needs here.
+ </li>
+ <li>
+ <code>identifiers.distinct_id</code> is the person the send is recorded against. Leave it out and
+ the recipient's email address is used.
+ </li>
+ </ul>
+ <CodeSnippet language={Language.Bash}>{snippet}</CodeSnippet>
+ </div>
+ )
+}
diff --git a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts
index f0596e88d22f..789f407f9c22 100644
--- a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts
+++ b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts
@@ -330,4 +330,31 @@ describe('messageTemplateLogic', () => {
await expectLogic(logic).toMatchValues({ templatePickerOpen: true })
})
})
+
+ describe('make transactional', () => {
+ it('keeps edits made while the conversion runs', async () => {
+ useMocks({
+ post: {
+ '/api/projects/:team_id/messaging_templates/:id/make_transactional/': {
+ id: 'existing-id',
+ name: 'Existing',
+ transactional_hog_flow_id: 'flow-id',
+ updated_at: '2026-01-02T00:00:00Z',
+ },
+ },
+ })
+ logic = messageTemplateLogic({ id: 'existing-id' })
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.makeTransactional()
+ logic.actions.setTemplateValue('name', 'Renamed while converting')
+ })
+ .toDispatchActions(['makeTransactionalDone'])
+ .toMatchValues({ transactionalHogFlowId: 'flow-id', templateChanged: true })
+
+ expect(logic.values.template.name).toEqual('Renamed while converting')
+ })
+ })
})
diff --git a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts
index 75cacfc134c8..d3520d761dd9 100644
--- a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts
+++ b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts
@@ -12,14 +12,18 @@ import { EDITOR_MODE_PARAM, EDITOR_MODE_VALUE } from 'scenes/max/aiFirstCreate/a
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
+import { messagingTemplatesMakeTransactionalCreate } from 'products/messaging/frontend/generated/api'
import { resourceEditedLogic } from 'products/notifications/frontend/resourceEditedLogic'
import type { EmailTemplate } from '../../../../frontend/src/scenes/hog-functions/email-templater/types'
import type { HogFunctionType, ResourceEditedEvent, UserBasicType } from '../../../../frontend/src/types'
+import { workflowLogic } from '../Workflows/workflowLogic'
import { NEW_TEMPLATE } from './constants'
import { newTemplateAgentLogic } from './newTemplateAgentLogic'
import { MessageTemplate } from './types'
+export type TransactionalTemplateTab = 'design' | 'metrics' | 'sends' | 'versions' | 'api'
+
export interface MessageTemplateLogicProps {
id: string
messageId?: string | null
@@ -31,6 +35,7 @@ export interface messageTemplateLogicValues {
currentTeamIdStrict: number | string // teamLogic
deferredExternalEdit: ResourceEditedEvent | null
externallyEdited: boolean
+ isMakingTransactional: boolean
isSyncingExternalEdit: boolean
isTemplateSubmitting: boolean
isTemplateValid: boolean
@@ -53,6 +58,7 @@ export interface messageTemplateLogicValues {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
},
ValidationErrorType
@@ -74,10 +80,13 @@ export interface messageTemplateLogicValues {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
},
ValidationErrorType
>
+ transactionalHogFlowId: string | null
+ transactionalTab: TransactionalTemplateTab
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -124,6 +133,12 @@ export interface messageTemplateLogicActions {
template: MessageTemplate
payload?: any
}
+ makeTransactional: () => {
+ value: true
+ }
+ makeTransactionalDone: (hogFlowId: string | null) => {
+ hogFlowId: string | null
+ }
replayDeferredExternalEdit: () => {
value: true
}
@@ -137,6 +152,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}) => {
values?: {
@@ -149,6 +165,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}
}
@@ -206,6 +223,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}>
) => {
@@ -219,9 +237,13 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}>
}
+ setTransactionalTab: (tab: TransactionalTemplateTab) => {
+ tab: TransactionalTemplateTab
+ }
submitTemplate: () => {
value: boolean
}
@@ -242,6 +264,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}) => {
template: {
@@ -254,6 +277,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}
}
@@ -267,6 +291,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}) => {
template: {
@@ -279,6 +304,7 @@ export interface messageTemplateLogicActions {
description: string
id: string
name: string
+ transactional_hog_flow_id?: string | null | undefined
updated_at: string | null
}
}
@@ -298,6 +324,7 @@ export interface messageTemplateLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
logicProps: (arg: MessageTemplateLogicProps) => MessageTemplateLogicProps
+ transactionalHogFlowId: (originalTemplate: MessageTemplate) => string | null
}
}
@@ -329,6 +356,9 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
keepMyTemplateVersion: true,
setExternallyEdited: (externallyEdited: boolean) => ({ externallyEdited }),
setSyncingExternalEdit: (syncing: boolean) => ({ syncing }),
+ makeTransactional: true,
+ makeTransactionalDone: (hogFlowId: string | null) => ({ hogFlowId }),
+ setTransactionalTab: (tab: TransactionalTemplateTab) => ({ tab }),
}),
selectors({
logicProps: [
@@ -370,6 +400,19 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
},
},
],
+ isMakingTransactional: [
+ false,
+ {
+ makeTransactional: () => true,
+ makeTransactionalDone: () => false,
+ },
+ ],
+ transactionalTab: [
+ 'design' as TransactionalTemplateTab,
+ {
+ setTransactionalTab: (_, { tab }) => tab,
+ },
+ ],
templatePickerOpen: [
false,
{
@@ -400,6 +443,13 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
},
],
}),
+ selectors({
+ // The saved state decides it: an unsaved template can't be transactional yet.
+ transactionalHogFlowId: [
+ (s) => [s.originalTemplate],
+ (originalTemplate: MessageTemplate): string | null => originalTemplate.transactional_hog_flow_id ?? null,
+ ],
+ }),
loaders(({ props }) => ({
template: {
loadTemplate: async () => {
@@ -436,7 +486,7 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
templateEditedElsewhere: ({ event }) => {
// The echo of our own save can arrive before its response. Parked until the flight settles,
// it then compares equal to the loaded stamp and is dropped.
- if (values.templateLoading) {
+ if (values.templateLoading || values.isMakingTransactional) {
actions.setDeferredExternalEdit(event)
return
}
@@ -464,6 +514,34 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
keepMyTemplateVersion: () => {
actions.setExternallyEdited(false)
},
+ makeTransactional: async () => {
+ try {
+ const updated = await messagingTemplatesMakeTransactionalCreate(
+ String(values.currentTeamIdStrict),
+ props.id,
+ {}
+ )
+ // Only these two fields changed. Merging them in keeps edits made while this ran.
+ const changes = {
+ transactional_hog_flow_id: updated.transactional_hog_flow_id ?? null,
+ updated_at: updated.updated_at,
+ }
+ actions.setOriginalTemplate({ ...values.originalTemplate, ...changes })
+ if (values.templateChanged) {
+ actions.setTemplateValues(changes)
+ } else {
+ // Resetting moves the form's baseline too, so the form doesn't read as edited.
+ actions.resetTemplate({ ...values.template, ...changes })
+ }
+ actions.makeTransactionalDone(updated.transactional_hog_flow_id ?? null)
+ actions.replayDeferredExternalEdit()
+ lemonToast.success('Template is now transactional. You can send it over the API.')
+ } catch (error: any) {
+ actions.makeTransactionalDone(null)
+ actions.replayDeferredExternalEdit()
+ lemonToast.error(error?.detail ?? 'Could not make the template transactional.')
+ }
+ },
loadTemplateFailure: () => {
// The form is still the stale copy. The banner keeps the conflict visible and offers the retry.
if (values.isSyncingExternalEdit) {
@@ -509,6 +587,10 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([
// does not intercept the post-save redirect.
actions.resetTemplate(template)
actions.setOriginalTemplate(template)
+ if (values.transactionalHogFlowId) {
+ // The save wrote a new workflow version, and nothing else tells the bound workflow tabs.
+ workflowLogic.findMounted({ id: values.transactionalHogFlowId })?.actions.loadWorkflow()
+ }
if (props.id === 'new') {
router.actions.push(urls.workflows('library'))
} else if (template.id) {
diff --git a/products/workflows/frontend/TemplateLibrary/messageTemplatesLogic.ts b/products/workflows/frontend/TemplateLibrary/messageTemplatesLogic.ts
index e8332198471e..e63b5074ef8d 100644
--- a/products/workflows/frontend/TemplateLibrary/messageTemplatesLogic.ts
+++ b/products/workflows/frontend/TemplateLibrary/messageTemplatesLogic.ts
@@ -18,6 +18,7 @@ export interface messageTemplatesLogicValues {
search: string
templates: MessageTemplate[]
templatesLoading: boolean
+ transactionalOnly: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -94,6 +95,9 @@ export interface messageTemplatesLogicActions {
setSearch: (search: string) => {
search: string
}
+ setTransactionalOnly: (transactionalOnly: boolean) => {
+ transactionalOnly: boolean
+ }
updateTemplate: ({ templateId, template }: { template: Partial<MessageTemplate>; templateId: string }) => {
templateId: string
template: Partial<MessageTemplate>
@@ -126,7 +130,8 @@ export interface messageTemplatesLogicMeta {
filteredTemplates: (
templates: MessageTemplate[],
search: string,
- createdByFilter: number | null
+ createdByFilter: number | null,
+ transactionalOnly: boolean
) => MessageTemplate[]
}
}
@@ -146,10 +151,12 @@ export const messageTemplatesLogic = kea<messageTemplatesLogicType>([
actions({
setSearch: (search: string) => ({ search }),
setCreatedByFilter: (createdBy: number | null) => ({ createdBy }),
+ setTransactionalOnly: (transactionalOnly: boolean) => ({ transactionalOnly }),
}),
reducers({
search: ['' as string, { setSearch: (_, { search }) => search }],
createdByFilter: [null as number | null, { setCreatedByFilter: (_, { createdBy }) => createdBy }],
+ transactionalOnly: [false, { setTransactionalOnly: (_, { transactionalOnly }) => transactionalOnly }],
}),
loaders(({ values, actions }) => ({
templates: [
@@ -219,8 +226,13 @@ export const messageTemplatesLogic = kea<messageTemplatesLogicType>([
})),
selectors({
filteredTemplates: [
- (s) => [s.templates, s.search, s.createdByFilter],
- (templates: MessageTemplate[], search: string, createdByFilter: number | null): MessageTemplate[] => {
+ (s) => [s.templates, s.search, s.createdByFilter, s.transactionalOnly],
+ (
+ templates: MessageTemplate[],
+ search: string,
+ createdByFilter: number | null,
+ transactionalOnly: boolean
+ ): MessageTemplate[] => {
let filtered = templates
if (search) {
const lowerSearch = search.toLowerCase()
@@ -233,6 +245,9 @@ export const messageTemplatesLogic = kea<messageTemplatesLogicType>([
if (createdByFilter !== null) {
filtered = filtered.filter((t) => t.created_by?.id === createdByFilter)
}
+ if (transactionalOnly) {
+ filtered = filtered.filter((t) => !!t.transactional_hog_flow_id)
+ }
return filtered
},
],
diff --git a/products/workflows/frontend/TemplateLibrary/types.ts b/products/workflows/frontend/TemplateLibrary/types.ts
index e949e04f9531..975f2c20c3d0 100644
--- a/products/workflows/frontend/TemplateLibrary/types.ts
+++ b/products/workflows/frontend/TemplateLibrary/types.ts
@@ -2,6 +2,8 @@ import type { EmailTemplate } from 'scenes/hog-functions/email-templater/types'
import type { UserBasicType } from '~/types'
+import type { MessageTemplateApi } from 'products/messaging/frontend/generated/api.schemas'
+
export interface MessageTemplate {
id: string
name: string
@@ -13,4 +15,5 @@ export interface MessageTemplate {
created_at: string | null
updated_at: string | null
created_by: UserBasicType | null
+ transactional_hog_flow_id?: MessageTemplateApi['transactional_hog_flow_id']
}
diff --git a/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.tsx b/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.tsx
index 43e50a327634..8eb7cf6ef8c9 100644
--- a/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.tsx
+++ b/products/workflows/frontend/Workflows/hogflows/hogFlowEditorLogic.tsx
@@ -791,6 +791,9 @@ export interface hogFlowEditorLogicActions {
}
| {
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -962,6 +965,9 @@ export interface hogFlowEditorLogicActions {
tags: string[]
team_id: number
trigger?:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -1668,6 +1674,9 @@ export interface hogFlowEditorLogicActions {
}
| {
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -1839,6 +1848,9 @@ export interface hogFlowEditorLogicActions {
tags: string[]
team_id: number
trigger?:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
diff --git a/products/workflows/frontend/Workflows/hogflows/panel/testing/hogFlowEditorTestLogic.ts b/products/workflows/frontend/Workflows/hogflows/panel/testing/hogFlowEditorTestLogic.ts
index 637b296f098b..7684ccfba861 100644
--- a/products/workflows/frontend/Workflows/hogflows/panel/testing/hogFlowEditorTestLogic.ts
+++ b/products/workflows/frontend/Workflows/hogflows/panel/testing/hogFlowEditorTestLogic.ts
@@ -336,6 +336,9 @@ export interface hogFlowEditorTestLogicMeta {
triggerAction:
| ({
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -470,6 +473,9 @@ export interface hogFlowEditorTestLogicMeta {
triggerAction:
| ({
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -608,6 +614,9 @@ export interface hogFlowEditorTestLogicMeta {
triggerAction:
| ({
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -742,6 +751,9 @@ export interface hogFlowEditorTestLogicMeta {
triggerAction:
| ({
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/stepDelayLogic.ts b/products/workflows/frontend/Workflows/hogflows/steps/stepDelayLogic.ts
index cf0e55b5da03..db33a10173bd 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/stepDelayLogic.ts
+++ b/products/workflows/frontend/Workflows/hogflows/steps/stepDelayLogic.ts
@@ -192,6 +192,9 @@ export interface stepDelayLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -503,6 +506,9 @@ export interface stepDelayLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/stepWaitUntilTimeWindowLogic.ts b/products/workflows/frontend/Workflows/hogflows/steps/stepWaitUntilTimeWindowLogic.ts
index a14b1a15dbea..754633a23188 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/stepWaitUntilTimeWindowLogic.ts
+++ b/products/workflows/frontend/Workflows/hogflows/steps/stepWaitUntilTimeWindowLogic.ts
@@ -118,6 +118,9 @@ export interface stepWaitUntilTimeWindowLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -430,6 +433,9 @@ export interface stepWaitUntilTimeWindowLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/types.ts b/products/workflows/frontend/Workflows/hogflows/steps/types.ts
index 4861428f1e81..8b5c12608113 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/types.ts
+++ b/products/workflows/frontend/Workflows/hogflows/steps/types.ts
@@ -173,6 +173,10 @@ export const HogFlowTriggerSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('schedule'),
}),
+ // A transactional email: its message template's send endpoint runs the workflow once per call.
+ z.object({
+ type: z.literal('api'),
+ }),
z.object({
type: z.literal('cohort'),
// Null until a cohort is picked. The backend requires one before the workflow runs.
diff --git a/products/workflows/frontend/Workflows/overview/workflowOverviewSummary.ts b/products/workflows/frontend/Workflows/overview/workflowOverviewSummary.ts
index b06d241ebaf1..b79fa15c7186 100644
--- a/products/workflows/frontend/Workflows/overview/workflowOverviewSummary.ts
+++ b/products/workflows/frontend/Workflows/overview/workflowOverviewSummary.ts
@@ -24,6 +24,7 @@ const BUILT_IN_TRIGGER_LABELS: Record<BuiltInTriggerType, string> = {
tracking_pixel: 'when a tracking pixel loads',
batch: 'when a batch run starts',
'internal-event': 'when PostHog records',
+ api: 'when your code sends it through the API',
}
export function triggerLeadIn(trigger: HogFlow['trigger']): string {
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index 41d5e6229548..5dcd68874080 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -834,6 +834,9 @@ export interface workflowLogicActions {
}
| {
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -1115,6 +1118,9 @@ export interface workflowLogicActions {
tags: string[]
team_id: number
trigger?:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -1711,6 +1717,9 @@ export interface workflowLogicActions {
}
| {
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -1992,6 +2001,9 @@ export interface workflowLogicActions {
tags: string[]
team_id: number
trigger?:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -2186,6 +2198,9 @@ export interface workflowLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -2583,6 +2598,9 @@ export interface workflowLogicActions {
| {
reason?: string | undefined
}
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}
@@ -3001,6 +3019,9 @@ export interface workflowLogicMeta {
triggerAction:
| ({
config:
+ | {
+ type: 'api'
+ }
| {
type: 'schedule'
}