<@U04UQ68083H> How would I go about customizing th...
# ory-copilot
c
@chilly-king-10285 How would I go about customizing the OTC code email templates depending upon the origin of the API request for the code. For example, the API request for the code may come from domain1.com or domain.net.
b
A message from kapa.ai
To customize the one-time code (OTC) email templates based on the origin of the API request (e.g., domain1.com vs. domain.net), you would need a way to pass the origin information into the email template rendering context. Ory supports passing additional, temporary data to email templates using the "transient payload" mechanism. This allows you to include custom data (such as the request origin) in the self-service flow, which can then be accessed in your email templates. For example, you can set a key like origin in the transient payload when initiating the flow, and then use it in your template: How to use transient payload in templates: 1. Pass the origin as a transient payload when initiating the self-service flow (e.g., login or registration). 2. Access the transient payload in your template using Go template syntax:
Copy code
gotmpl <p>Origin: {{index .TransientPayload "origin"}}</p> {{- if eq (index .TransientPayload "origin") "<http://domain1.com|domain1.com>" -}}   <!-- Custom content for domain1.com --> {{- else if eq (index .TransientPayload "origin") "<http://domain.net|domain.net>" -}}   <!-- Custom content for domain.net --> {{- else -}}   <!-- Default content --> {{- end -}}
This approach allows you to conditionally render different content in your OTC email templates based on the origin of the request, as long as you ensure the origin is included in the transient payload when starting the flow. For more details and examples, see the documentation on [transient payload in templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#transient-payload-in-templates). Note: The knowledge sources do not provide a built-in way to automatically detect the request origin and inject it into the transient payload; you must ensure your application logic sets this value when initiating the flow. If you need to customize templates based on other identity attributes (like language), you can also use the
.Identity
object in templates, but for request-specific data like origin, the transient payload is the recommended approach [Customizing template content for specific users](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#customizing-template-content-for-specific-users). Summary: - Pass the origin as a transient payload when starting the flow. - Use
{{index .TransientPayload "origin"}}
in your template to customize content. If you need more details on how to set the transient payload in your API requests, please specify your integration environment.
c
@chilly-king-10285 Show an example of passing the origin when initiating the OTC flow with an API request.
b
A message from kapa.ai
To pass the origin when initiating an OTC (one-time code) flow, you should include the origin information in the transient_payload field of your API request. This field is designed to carry custom, temporary data through the flow and make it available to email templates and webhooks. Here’s an example of how you might initiate a verification flow (for email or code) and include the origin in the transient_payload using a JSON API request:
Copy code
json {   "method": "code",   "email": "<mailto:user@example.com|user@example.com>",   "transient_payload": {     "origin": "<http://domain1.com|domain1.com>"   } }
- For the verification flow, the request body allows a transient_payload object, which will be available in your email templates as
.TransientPayload
 [Customizing template content for specific users](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#transient-payload-in-templates). - The same approach applies to other flows that support transient_payload, such as registration or recovery [Github Issue: Possibility to define complex data types in the metadata of identity](https://github.com/ory/network/issues/187). How to use in a cURL request:
Copy code
bash curl -X POST \   -H 'Content-Type: application/json' \   -d '{     "method": "code",     "email": "<mailto:user@example.com|user@example.com>",     "transient_payload": {       "origin": "<http://domain1.com|domain1.com>"     }   }' \   "https://<your-kratos-endpoint>/self-service/verification?flow=<flow-id>"
You can then access this value in your email template using:
Copy code
gotmpl {{index .TransientPayload "origin"}}
This allows you to customize the email content based on the origin value you passed in the API request [Customizing template content for specific users](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#transient-payload-in-templates). > Note: The exact endpoint and required fields may vary depending on the flow (verification, recovery, etc.), but the use of transient_payload is consistent across these flows as shown in the API reference and documentation.
c
@chilly-king-10285 So how do I implement a custom template when using Ory Network.
b
A message from kapa.ai
To implement a custom email template when using Ory Network, you need to use Base64-encoded templates and update your project configuration accordingly. Here’s how you can do it: 1. Create your custom template Write your email template using the Go template syntax. You can use variables and logic as described in the documentation. For example, for a login code email:
Copy code
Hello {{ .Identity.traits.firstName }},     Your login code is: {{ .LoginCode }}
2. Encode your template in Base64 Convert your template to Base64. You can use the command line or an online tool:
Copy code
bash    echo "Hello {{ .Identity.traits.firstName }},\nYour login code is: {{ .LoginCode }}" | base64
3. Update your Ory Identities configuration Add the Base64-encoded template to your configuration file, making sure to prefix it with
base64://
. For example, to customize the login code email:
Copy code
yaml    courier:      smtp:        from_name: MyProject via Ory        templates:          login:            valid:              email:                body:                  plaintext: "base64://<ENCODED_TEMPLATE_HERE>"
4. Upload the updated configuration Use the Ory CLI to update your project configuration:
Copy code
bash    ory update identity-config --project <project-id> --workspace <workspace-id> --file updated_config.yaml
Important notes: - In Ory Network, only Base64-encoded templates are supported. You cannot use
http://
or
file://
URIs for templates; if you do, the system will fall back to the default templates [Using custom message templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates). - You can version and manage your templates in your repository, but you must always encode them to Base64 before uploading [Email Templates (GitOps)](https://www.ory.sh/docs/guides/gitops#email-templates). - For more advanced customization (e.g., per-user or per-request logic), you can use variables like
.Identity
and
.TransientPayload
in your templates [Customizing template content for specific users](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#customizing-template-content-for-specific-users). For a detailed step-by-step guide, see the [Ory documentation on custom email templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates).
c
@chilly-king-10285 Now how would I customize the From name/email depending on the origin?
b
A message from kapa.ai
I'm sorry, but the knowledge sources do not provide a way to dynamically customize the "From" name or email address based on the origin of the API request when using Ory Network. The documentation states that to change the sender address (
from_address
) and sender name (
from_name
), you must configure them statically in your project configuration, and these values apply globally to all emails sent by Ory for that project [Use a custom server to send Ory Identity messages to users](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp). There is no mention of support for per-request or per-origin customization of the sender fields. If you require different "From" addresses or names based on the origin, you would need to set up separate Ory projects, each with its own configuration, or handle email sending externally via webhooks or a custom integration. However, this approach is not explicitly documented in the provided sources. Summary: Ory Network does not currently support dynamic "From" name/email customization based on request origin in its documented configuration options.