New Ory Agent Security is now live! Claim your complimentary test drive. Get Started!

Skip to main content

Send SMS to your users

Ory Network comes with an HTTP based SMS delivery option that can be configured to point to any service that supports sending SMS via HTTP API, such as Twilio, Plivo, AWS SNS, or your own microservice.

Configuration​

SMS delivery can be configured through Ory Console or the Ory CLI. Follow these steps to configure SMS:

  1. Go to Authentication → SMS Configuration in the Ory Console
  2. Add the configuration for your SMS provider

Body configuration​

The body field holds a Jsonnet template, encoded in Base64 and prefixed with base64://. Ory Kratos Identities evaluates the template for every message and sends its output as the request body. The Twilio example above uses this template:

function(ctx) {
To: ctx.recipient,
From: '<your-twilio-phone-number>',
Body: ctx.body,
}

Replace the placeholder, save the template to a file, and encode it:

base64 < body.jsonnet | tr -d '\n'

The Content-Type header sets the body format. It must be application/json, the default, or application/x-www-form-urlencoded. For application/x-www-form-urlencoded, the template must return an object whose values are all strings. Each key becomes a form field.

Fields available on the ctx object are:

  • recipient: The recipient's phone number
  • body: The rendered message body
  • template_type: The template type, for example login_code_valid
  • template_data: The template data, for example { "to": "+491701234567", "login_code": "123456", "identity": { ... } }. The code is in login_code for login codes, in registration_code for registration codes, and in verification_code for verification codes. The recovery SMS (recovery_code_valid) also puts its code in verification_code.
  • message_type: The message type, for example sms
  • request_headers: A map of all HTTP request headers forwarded from the user request (see below)

Accessing request headers​

The following request headers are available via ctx.request_headers:

Accept
Accept-Encoding
Accept-Language
Content-Length
Content-Type
Origin
Priority
Referer
Sec-Ch-Ua
Sec-Ch-Ua-Mobile
Sec-Ch-Ua-Platform
Sec-Fetch-Dest
Sec-Fetch-Mode
Sec-Fetch-Site
Sec-Fetch-User
True-Client-Ip
User-Agent
X-Forwarded-Host
Ory-Base-Url-Rewrite
Ory-Base-Url-Rewrite-Token
X-Ory-Original-Host
Ory-No-Custom-Domain-Redirect
Cf-Ipcountry

Read the Jsonnet documentation to learn more about the Jsonnet templating language.

Provider examples​

The Configuration example above uses Twilio. For another provider, replace the channel's request_config with the provider's snippet and set body to the provider's Jsonnet template, encoded as described in Body configuration.

Twilio​

url: https://api.twilio.com/2010-04-01/Accounts/<your-account-sid>/Messages.json
method: POST
body: base64://<base64-encoded-jsonnet>
headers:
Content-Type: application/x-www-form-urlencoded # Required. Twilio doesn't accept JSON.
auth:
type: basic_auth
config:
user: <your-account-sid>
password: <your-auth-token>

Twilio requires a sender. Use the template from Body configuration and set From to your Twilio phone number. To send through a Messaging Service, replace From with MessagingServiceSid.

MessageBird (Bird)​

url: https://rest.messagebird.com/messages
method: POST
body: base64://<base64-encoded-jsonnet>
headers:
Content-Type: application/json
Authorization: AccessKey <your-messagebird-access-key>

MessageBird expects recipients in MSISDN format, so the template strips the leading + from the recipient:

function(ctx) {
originator: 'YourApp',
recipients: [
if std.startsWith(ctx.recipient, '+')
then std.substr(ctx.recipient, 1, std.length(ctx.recipient) - 1)
else ctx.recipient,
],
body: ctx.body,
}

Plivo​

url: https://api.plivo.com/v1/Account/<your-auth-id>/Message/
method: POST
body: base64://<base64-encoded-jsonnet>
headers:
Content-Type: application/json
auth:
type: basic_auth
config:
user: <your-auth-id>
password: <your-auth-token>

Plivo takes the sender in src, the recipient in E.164 format in dst, and the message in text:

function(ctx) {
src: '<your-plivo-phone-number>',
dst: ctx.recipient,
text: ctx.body,
}

To send from a Plivo Powerpack number pool, replace src with powerpack_uuid.

Sinch​

# US stack
url: https://us.sms.api.sinch.com/xms/v1/<your-service-plan-id>/batches
# EU stack
# url: https://eu.sms.api.sinch.com/xms/v1/<your-service-plan-id>/batches
method: POST
body: base64://<base64-encoded-jsonnet>
headers:
Authorization: Bearer <your-sinch-api-token>
Content-Type: application/json

Sinch takes the recipients as an array in to:

function(ctx) {
from: '<your-sinch-sender>',
to: [ctx.recipient],
body: ctx.body,
}

Sinch enforces per-country sender registration — using the wrong sender for a region silently degrades delivery.

Vonage (Nexmo)​

url: https://rest.nexmo.com/sms/json
method: POST
body: base64://<base64-encoded-jsonnet>
headers:
Content-Type: application/x-www-form-urlencoded
auth:
type: basic_auth
config:
user: <your-vonage-api-key>
password: <your-vonage-api-secret>

Vonage expects the recipient without the leading +, so the template strips it:

function(ctx) {
from: 'YourApp',
to:
if std.startsWith(ctx.recipient, '+')
then std.substr(ctx.recipient, 1, std.length(ctx.recipient) - 1)
else ctx.recipient,
text: ctx.body,
}

WhatsApp Business​

WhatsApp delivers OTPs as pre-approved template messages rather than plain text. The reference setup keeps the sms channel in courier.channels, but points its request_config at a small handler that you deploy. The handler builds the template message and calls the WhatsApp Cloud API. See WhatsApp Business OTP courier for the full setup.

Templates​

The verification_code, login_code, registration_code, and verifiable_address_changed templates support an SMS variant. Self-hosted Ory Kratos also supports one for recovery_code. Ory Network ignores courier.templates.recovery_code.valid.sms and sends the default recovery SMS. SMS templates use the same template language as email templates. Use the CLI to configure the SMS templates:

  1. Download the Ory Kratos Identities config from your project and save it to a file:

    ## List all available workspaces
    ory list workspaces

    ## List all available projects
    ory list projects --workspace <workspace-id>

    ## Get config
    ory get identity-config --project <project-id> --workspace <workspace-id> --format yaml > identity-config.yaml
  2. Add the SMS templates next to the existing email templates. Keep the email entries, because each valid block requires an email entry:

    identity-config.yaml
    courier:
    templates:
    verification_code:
    valid:
    sms:
    body:
    plaintext: "base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgLlZlcmlmaWNhdGlvbkNvZGUgfX0="
    login_code:
    valid:
    sms:
    body:
    plaintext: "base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19"
  3. Update the Ory Kratos Identities configuration using the file you worked with:

    ory update identity-config --project <project-id> --workspace <workspace-id> --file identity-config.yaml