Change SMS content
To change the content of the default SMS templates, you can override the getContent
function in the smsDelivery
object. It allows you to return an object that has the following properties:
body
: This is the email's body. This can be HTML or just text as well.toPhoneNumber
: The phone number where the SMS will be sent to.
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
import supertokens from "supertokens-node";
import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";
import Session from "supertokens-node/recipe/session";
import { TwilioService } from "supertokens-node/recipe/passwordless/smsdelivery";
supertokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
ThirdPartyPasswordless.init({
smsDelivery: {
service: new TwilioService({
twilioSettings: { /*...*/ },
override: (originalImplementation) => {
return {
...originalImplementation,
getContent: async function ({
isFirstFactor,
codeLifetime, // amount of time the code is alive for (in MS)
phoneNumber,
urlWithLinkCode, // magic link
userInputCode, // OTP
}) {
if (isFirstFactor) {
// send some custom SMS content
return {
toPhoneNumber: phoneNumber,
body: "SMS BODY"
}
} else {
// for second factor, urlWithLinkCode will always be
// undefined since we only support OTP based for second factor
return {
toPhoneNumber: phoneNumber,
body: "SMS BODY"
}
}
// You can even call the original implementation and
// modify its content:
/*let originalContent = await originalImplementation.getContent(input)
originalContent.body = "My custom body";
return originalContent;*/
}
}
}
})
}
}),
Session.init()
]
});
import (
"fmt"
"github.com/supertokens/supertokens-golang/ingredients/smsdelivery"
"github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless"
"github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless/tplmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
smsService, err := thirdpartypasswordless.MakeTwilioService(smsdelivery.TwilioServiceConfig{
Settings: smsdelivery.TwilioSettings{ /* ... */ },
Override: func(originalImplementation smsdelivery.TwilioInterface) smsdelivery.TwilioInterface {
// originalGetContent := *originalImplementation.GetContent
(*originalImplementation.GetContent) = func(input smsdelivery.SmsType, userContext supertokens.UserContext) (smsdelivery.SMSContent, error) {
// amount of time the code is alive for (in MS)
codeLifetime := input.PasswordlessLogin.CodeLifetime
phoneNumber := input.PasswordlessLogin.PhoneNumber
// magic link
urlWithLinkCode := input.PasswordlessLogin.UrlWithLinkCode
// OTP
userInputCode := input.PasswordlessLogin.UserInputCode
fmt.Println(codeLifetime)
fmt.Println(phoneNumber)
fmt.Println(urlWithLinkCode)
fmt.Println(userInputCode)
// send some custom email content
return smsdelivery.SMSContent{
Body: "SMS BODY",
ToPhoneNumber: phoneNumber,
}, nil
// Or you can call the original implemenation and change its content:
/*
originalResponse, err := originalGetContent(input, userContex)
if err != nil {
return smsdelivery.SMSContent{}, nil
}
originalResponse.body = "SMS Body"
return originalResponse
*/
}
return originalImplementation
},
})
if err != nil {
panic(err)
}
supertokens.Init(supertokens.TypeInput{
RecipeList: []supertokens.Recipe{
thirdpartypasswordless.Init(tplmodels.TypeInput{
SmsDelivery: &smsdelivery.TypeInput{
Service: smsService,
},
}),
},
})
}
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import thirdpartypasswordless
from supertokens_python.recipe.thirdpartypasswordless.types import TwilioOverrideInput, SMSTemplateVars
from supertokens_python.ingredients.smsdelivery.types import SMSDeliveryConfig, SMSContent, TwilioSettings
from typing import Dict, Any
def custom_sms_content_override(original_implementation: TwilioOverrideInput) -> TwilioOverrideInput:
# original_get_content = original_implementation.get_content
async def get_content(template_vars: SMSTemplateVars, user_context: Dict[str, Any]) -> SMSContent:
# amount of time the code is alive for (in MS)
_ = template_vars.code_life_time
phone_number = template_vars.phone_number
__ = template_vars.url_with_link_code # magic link
___ = template_vars.user_input_code # OTP
# send some custom email content
return SMSContent(body="EMAIL BODY", to_phone=phone_number)
# you can even call the original implementation and modify that
# original_content = await original_get_content(template_vars, user_context)
# original_content.body = "My custom body"
# return original_content
original_implementation.get_content = get_content
return original_implementation
init(
app_info=InputAppInfo(
api_domain="...", app_name="...", website_domain="..."),
framework='...',
recipe_list=[
thirdpartypasswordless.init(
sms_delivery=SMSDeliveryConfig(
service=thirdpartypasswordless.TwilioService(
twilio_settings=TwilioSettings(...),
override=custom_sms_content_override
)
)
)
]
)