|
| 1 | +--- |
| 2 | +id: transient-payload |
| 3 | +title: Transient Payload passthrough |
| 4 | +sidebar_label: Transient Payload |
| 5 | +--- |
| 6 | + |
| 7 | +Transient payload is an Ory Kratos concept, that allows users of the APIs to pass data through to webhooks. All self-service |
| 8 | +Kratos flows support transient payloads, and they are passed through to the webhooks as JSON objects. This allows you to use the |
| 9 | +data in your webhooks, for example to customize the user experience or to trigger specific actions based on the data. |
| 10 | + |
| 11 | +Ory Elements allows defining transient payloads on the self-service flow components. To do this, you can use the |
| 12 | +`transientPayload` prop on the self-service flow components. The value of this prop should be an object that contains the data you |
| 13 | +want to pass through to the webhooks or a function that returns such an object. The data will be passed through to the webhooks as |
| 14 | +JSON objects. |
| 15 | + |
| 16 | +- [Read more about webhooks in Ory](../../guides/integrate-with-ory-cloud-through-webhooks.mdx) |
| 17 | + |
| 18 | +## Static Transient Payload |
| 19 | + |
| 20 | +In this example, we define a static transient payload that contains the user's preferred language. This data will be passed |
| 21 | +through to the webhooks as a JSON object. |
| 22 | + |
| 23 | +```tsx |
| 24 | +import { Registration } from "@ory/elements-react/theme" |
| 25 | +import { getRegistrationFlow, OryPageParams } from "@ory/nextjs/app" |
| 26 | + |
| 27 | +import config from "@/ory.config" |
| 28 | +import { headers } from "next/headers" |
| 29 | + |
| 30 | +export default async function RegistrationPage(props: OryPageParams) { |
| 31 | + const flow = await getRegistrationFlow(config, props.searchParams) |
| 32 | + |
| 33 | + const language = (await headers()).get("Accept-Language") |
| 34 | + |
| 35 | + if (!flow) { |
| 36 | + return null |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <Registration |
| 41 | + flow={flow} |
| 42 | + config={config} |
| 43 | + components={{ |
| 44 | + Card: {}, |
| 45 | + }} |
| 46 | + transientPayload={{ |
| 47 | + language, |
| 48 | + }} |
| 49 | + /> |
| 50 | + ) |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Dynamic Transient Payload |
| 55 | + |
| 56 | +In this example, we define a dynamic transient payload that contains the user's preferred language. The function is called when |
| 57 | +the user submits the form and the API request is made. This allows you to pass dynamic data to the webhooks, based on the user's |
| 58 | +input or other factors. |
| 59 | + |
| 60 | +```tsx |
| 61 | +"use client" |
| 62 | + |
| 63 | +import { Registration } from "@ory/elements-react/theme" |
| 64 | +import { RegistrationFlow } from "@ory/client-fetch" |
| 65 | +import config from "@/ory.config" |
| 66 | + |
| 67 | +export function RegistrationWithPayload({ flow }: { flow: RegistrationFlow }) { |
| 68 | + return ( |
| 69 | + <Registration |
| 70 | + flow={flow} |
| 71 | + config={config} |
| 72 | + transientPayload={(formValues) => { |
| 73 | + const referralCode = localStorage.getItem("referral-code") |
| 74 | + return { |
| 75 | + referralCode, |
| 76 | + } |
| 77 | + }} |
| 78 | + /> |
| 79 | + ) |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The function also receives the form values as an argument, which allows you to use the user's input to determine the transient |
| 84 | +payload. In this example, we are retrieving a referral code from local storage and passing it through to the webhooks. |
0 commit comments