<@U04UQ68083H> In kratos, I want to send a webhook...
# ory-copilot
f
@chilly-king-10285 In kratos, I want to send a webhook to my server when a user is registered (with an input value).
c
A message from kapa.ai
To send a webhook to your server when a user is registered in Kratos, you need to follow these steps: 1. Create a JSON object for your Ory Action. This object should include the
hook
set to
web_hook
, the URL of your server, the HTTP method (usually
POST
), and the body of the request. The body should be a Base64 encoded Jsonnet template that transforms the identity data from Ory to a format your server understands. Here is an example of such a JSON object:
Copy code
json {    "hook": "web_hook",    "config": {    "url": "<https://your-server.com/your-endpoint>",    "method": "POST",    "body": "base64://{BASE64_ENCODED_JSONNET}"    }   }
2. Save this JSON object to a file, for example
webhook-action.json
. 3. Use the Ory CLI to add this action to your Ory Network project. You can trigger this action after all successful registrations with the following command:
Copy code
bash ory patch identity-config {project-id} \    --add "/selfservice/flows/registration/after/hooks/0=$(cat webhook-action.json)" \    --format yaml
Remember to replace
{project-id}
with your actual project ID and
{BASE64_ENCODED_JSONNET}
with your actual Base64 encoded Jsonnet. For more information, refer to the [Ory documentation](https://www.ory.sh/docs/actions/integrations/hubspot) and the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks).
f
@chilly-king-10285 In kratos on selfservice, can i make custom hooks?
c
A message from kapa.ai
Yes, in Ory Kratos, you can create custom hooks to extend self-service user flows. These hooks can be used to trigger custom, external logic. The
web_hook
action is particularly useful for this purpose as it allows you to trigger any custom, external logic. Here's an example of how you can add a custom hook: 1. Get the Ory Identities config with Ory CLI:
Copy code
bash ## List all available projects   ory list projects      ## Get config   ory get identity-config {project-id} --format yaml > identity-config.yaml
2. Add the hook configuration to the downloaded file:
Copy code
yaml selfservice:     flows:     recovery:     enabled: true     ui_url: <http://your.ui/recovery>     after:     hooks:     - hook: your_custom_hook
3. Update the Ory Identities configuration using the file you worked with:
Copy code
bash ory update identity-config {project-id} --file identity-config.yaml
Remember to replace
{project-id}
with your actual project ID and
your_custom_hook
with your actual hook. For more information, refer to the [Ory documentation on configuring hooks](https://www.ory.sh/docs/kratos/hooks/configure-hooks).
f
@chilly-king-10285
Copy code
version: v0.13.0

serve:
  public:
    base_url: <http://127.0.0.1:4433/>
    cors:
      enabled: true
  admin:
    base_url: <http://kratos:4434/>

selfservice:
  default_browser_return_url: <http://localhost:5173/>
  allowed_return_urls:
    - <http://localhost:5173>

  methods:
    password:
      enabled: true

  flows:
    error:
      ui_url: <http://localhost:5173/error>

    settings:
      ui_url: <http://localhost:5173/settings>
      privileged_session_max_age: 15m
      required_aal: highest_available

    recovery:
      enabled: true
      ui_url: <http://localhost:5173/recovery>
      use: code

    verification:
      enabled: true
      ui_url: <http://localhost:5173/verification>
      use: code
      after:
        default_browser_return_url: <http://localhost:5173/>

    logout:
      after:
        default_browser_return_url: <http://localhost:5173/login>

    login:
      ui_url: <http://localhost:5173/login>
      lifespan: 10m

    registration:
      lifespan: 10m
      ui_url: <http://localhost:5173/registration>
      after:
        password:
          hooks:
            - hook: session

log:
  level: debug
  format: text
  leak_sensitive_values: true

secrets:
  cookie:
    - ASDVEW-CSASWW-MA-X-CD-BNFD-DSFGREWS
  cipher:
    - 32-ZXCV-QWERTY-ASD-ZXCASD-12-ASD

ciphers:
  algorithm: xchacha20-poly1305

hashers:
  algorithm: bcrypt
  bcrypt:
    cost: 8

identity:
  default_schema_id: default
  schemas:
    - id: default
      url: file:///etc/config/kratos/identity.schema.json

courier:
  smtp:
    connection_uri: <smtps://test:test@smtp.cozymail.dev:465/>
In my configuration, I want to give a webhook to http://localhost:5555 when a user is registered, how do I do that?
c
A message from kapa.ai
To add a webhook to your configuration when a user is registered, you need to create a custom hook using the
web_hook
action. Here's how you can do it: 1. First, create a Jsonnet file that transforms the identity data from Ory to a format your server understands. Save it as
identity_transform.jsonnet
.
Copy code
jsonnet function(ctx) {     userId: ctx.identity.id,     traits: {       email: ctx.identity.traits.email,       name: ctx.identity.traits.name,       // Add more fields as needed     },   }
2. Encode this Jsonnet snippet to Base64:
Copy code
bash cat identity_transform.jsonnet | base64
3. Define the Ory Action as a JSON object. This object should include the
hook
set to
web_hook
, the URL of your server, the HTTP method (usually
POST
), and the body of the request. The body should be the Base64 encoded Jsonnet you created earlier. Save this JSON object to a file, for example
webhook-action.json
.
Copy code
json {    "hook": "web_hook",    "config": {    "url": "<http://localhost:5555>",    "method": "POST",    "body": "base64://{BASE64_ENCODED_JSONNET}"    }   }
4. Add this action to your Ory Network project using the Ory CLI. You can trigger this action after all successful registrations with the following command:
Copy code
bash ory patch identity-config {project-id} \    --add "/selfservice/flows/registration/after/hooks/0=$(cat webhook-action.json)" \    --format yaml
Remember to replace
{project-id}
with your actual project ID and
{BASE64_ENCODED_JSONNET}
with your actual Base64 encoded Jsonnet. Finally, add the hook to your configuration file under the
registration
flow:
Copy code
yaml selfservice:   flows:     registration:       after:         password:           hooks:             - hook: session             - hook: web_hook               config:                 url: <http://localhost:5555>                 method: POST                 body: base64://{BASE64_ENCODED_JSONNET}
For more information, refer to the [Ory documentation](https://www.ory.sh/docs/actions/integrations/hubspot) and the [Ory Actions webhook guide](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks).
f
@chilly-king-10285 I do not want use ory-network(cloud), I use on-premised selfhosting kratos. in my on-premised kratos, how can i send webhook
c
A message from kapa.ai
New message text here
f
@chilly-king-10285 What hooks are available in selfservice?
c
A message from kapa.ai
In Ory Kratos self-service, there are several hooks available that you can use to extend self-service user flows. Here are the available hooks: 1. `session`: This hook signs in the user immediately after they create an account. It can only be used with the
after
registration flow. To use it, you must define secrets for secret rotation. [More details](https://www.ory.sh/docs/actions/session) 2. `revoke_active_sessions`: This hook revokes all other active sessions of the user on successful login. It can only be used with the login flow. [More details](https://www.ory.sh/docs/actions/revoke-active-sessions) 3. `require_verified_address`: This hook allows users to sign in only when they have verified their email address. It can only be used with the
after
login flow,
password
method only. [More details](https://www.ory.sh/docs/actions/require-verified-address) 4. `web_hook`: This hook allows triggering external and custom logic. It can be used with all flows and methods except error and logout. This is the only action type available for the
after
settings flow. [More details](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks) For more information, refer to the [Ory documentation on configuring hooks](https://www.ory.sh/docs/kratos/hooks/configure-hooks).
f
@chilly-king-10285 Can i verify(via user) before registration?
c
A message from kapa.ai
@few-scooter-86891 That's a great question, but unfortunately I am only capable of responding to a maximum of five questions per thread. Don't hesitate to create a new thread for any further questions you have though. I'm here to help!
d
@chilly-king-10285 How to verify user session in gateway?
c
A message from kapa.ai
@delightful-garage-16669 That's a great question, but unfortunately I am only capable of responding to a maximum of five questions per thread. Don't hesitate to create a new thread for any further questions you have though. I'm here to help!
A message from kapa.ai
@delightful-garage-16669 That's a great question, but unfortunately I am only capable of responding to a maximum of five questions per thread. Don't hesitate to create a new thread for any further questions you have though. I'm here to help!