<@U04UQ68083H> I want to send webhook after regist...
# ory-copilot
n
@chilly-king-10285 I want to send webhook after registration on ory kratos on the request body I want to send `User-Agent`and
Referer
of the frontend how to do that?
b
A message from kapa.ai
To send a webhook after registration in Ory Kratos with the
User-Agent
and
Referer
of the frontend in the request body, you can use Ory Actions and configure a webhook. Here's how you can do it: 1. First, create a Jsonnet template that includes the
User-Agent
and
Referer
in the request body. Here's an example:
Copy code
jsonnet function(ctx) {   identity: ctx.identity,   user_agent: ctx.request_headers['User-Agent'][0],   referer: ctx.request_headers['Referer'][0] }
2. Encode this Jsonnet template to Base64. 3. Create an Ory Action configuration for the webhook. Here's an example:
Copy code
json {   "hook": "web_hook",   "config": {     "url": "<https://your-webhook-url.com>",     "method": "POST",     "body": "<base64://YOUR_BASE64_ENCODED_JSONNET>"   } }
4. Add this action to your Ory Network project using the Ory CLI. To trigger this webhook after successful registration:
Copy code
bash ory patch identity-config --project <project-id> --workspace <workspace-id> \   --add "/selfservice/flows/registration/after/hooks/0=$(cat webhook-action.json)" \   --format yaml
This configuration will send a webhook after registration, including the
User-Agent
and
Referer
in the request body. The
User-Agent
and
Referer
are available in the
ctx.request_headers
object, as shown in the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#accessing-request-headers). Remember to replace
<https://your-webhook-url.com>
with your actual webhook URL and
YOUR_BASE64_ENCODED_JSONNET
with the Base64-encoded Jsonnet template. For more detailed information on configuring webhooks and Ory Actions, you can refer to the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks) and the [Ory Kratos hooks documentation](https://www.ory.sh/docs/kratos/hooks/configure-hooks).
n
@chilly-king-10285 can I use this?
Copy code
function(ctx) {
  identity: ctx.identity,
  user_agent: ctx.request_headers.User-Agent[0],
  referer: ctx.request_headers.Referer[0]
}
b
A message from kapa.ai
Yes, you can use that Jsonnet function, but there's a small modification needed to make it work correctly. Here's the corrected version:
Copy code
jsonnet function(ctx) {   identity: ctx.identity,   user_agent: ctx.request_headers['User-Agent'][0],   referer: ctx.request_headers['Referer'][0] }
The correction is in how you access the
User-Agent
and
Referer
headers. In Jsonnet, when accessing object properties with special characters or spaces, you need to use square bracket notation with string keys. This function will work as intended to include the identity, User-Agent, and Referer in the webhook payload. Here's why it works: 1.
ctx.identity
contains the identity information of the user who just registered. 2.
ctx.request_headers['User-Agent'][0]
accesses the first (and typically only) User-Agent header value. 3.
ctx.request_headers['Referer'][0]
accesses the first Referer header value. The
ctx.request_headers
object contains various request headers, including
User-Agent
and
Referer
, as documented in the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#accessing-request-headers). Remember to encode this Jsonnet function to Base64 before using it in your webhook configuration. After that, you can use it in your Ory Action configuration as described in the previous answer.
n
@chilly-king-10285 what about
Origin
?
b
A message from kapa.ai
Certainly! You can include the
Origin
header in your Jsonnet function as well. Here's an updated version of the function that includes the
Origin
header:
Copy code
jsonnet function(ctx) {   identity: ctx.identity,   user_agent: ctx.request_headers['User-Agent'][0],   referer: ctx.request_headers['Referer'][0],   origin: ctx.request_headers['Origin'][0] }
The
Origin
header is indeed available in the
ctx.request_headers
object, just like the
User-Agent
and
Referer
headers. This is confirmed in the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#accessing-request-headers), which lists
Origin
as one of the available request headers. Remember that, as with the other headers, we access it using square bracket notation and take the first element of the array (
[0]
), as header values are stored as arrays in the
ctx.request_headers
object. After updating your Jsonnet function to include the
Origin
header, don't forget to encode it to Base64 before using it in your webhook configuration. This will allow you to send the
Origin
header along with the
User-Agent
and
Referer
in your webhook payload after a successful registration.
n
@chilly-king-10285 can i use
file://
instead of
base64://
for the request body?
b
A message from kapa.ai
Yes, you can use
file://
instead of
base64://
for the request body in Ory Actions webhooks, but there's an important distinction to make: 1. For self-hosted Ory Kratos instances, you can indeed use
file://
URIs to specify the request body template. This is confirmed in the [Ory Kratos self-hosted email templates documentation](https://www.ory.sh/docs/kratos/self-hosted/mail-courier-templates#configuration), which states that you can add custom templates from
http://
,
file://
, and
base64://
URIs. 2. However, for Ory Network (the cloud-hosted version), only
base64://
URIs are supported. This limitation is mentioned in the [Ory Network email templates documentation](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates), which states: "In the Ory Network, you can use only Base64-encoded templates.
http://
or
file://
URIs are not supported." Given that your previous questions were about Ory Network (as evidenced by the use of the Ory CLI to update configurations), it's likely that you're using Ory Network. In this case, you should continue using
base64://
for the request body. If you're using a self-hosted instance of Ory Kratos, you could use
file://
URIs. However, it's generally recommended to use
base64://
for consistency and portability across different deployment scenarios. Remember, when using
base64://
, you need to encode your Jsonnet template to Base64 before adding it to your configuration. This ensures that your webhook payload, including headers like
User-Agent
,
Referer
, and
Origin
, is correctly processed and sent. [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks)