Class: Stytch::OTPs::Sms
- Inherits:
-
Object
- Object
- Stytch::OTPs::Sms
- Includes:
- RequestHelper
- Defined in:
- lib/stytch/otps.rb
Instance Method Summary collapse
-
#initialize(connection) ⇒ Sms
constructor
A new instance of Sms.
-
#login_or_create(phone_number:, expiration_minutes: nil, attributes: nil, create_user_as_pending: nil, locale: nil) ⇒ Object
Send a One-Time Passcode (OTP) to a User using their phone number.
-
#send(phone_number:, expiration_minutes: nil, attributes: nil, locale: nil, user_id: nil, session_token: nil, session_jwt: nil) ⇒ Object
Send a one-time passcode (OTP) to a user's phone number.
Methods included from RequestHelper
#delete_request, #get_request, #post_request, #put_request, #request_with_query_params
Constructor Details
#initialize(connection) ⇒ Sms
Returns a new instance of Sms.
130 131 132 |
# File 'lib/stytch/otps.rb', line 130 def initialize(connection) @connection = connection end |
Instance Method Details
#login_or_create(phone_number:, expiration_minutes: nil, attributes: nil, create_user_as_pending: nil, locale: nil) ⇒ Object
Send a One-Time Passcode (OTP) to a User using their phone number. If the phone number is not associated with a user already, a user will be created.
Cost to send SMS OTP
Before configuring SMS or WhatsApp OTPs, please review how Stytch bills the costs of international OTPs and understand how to protect your app against toll fraud.
Note: SMS to phone numbers outside of the US and Canada is disabled by default for customers who did not use SMS prior to October 2023. If you're interested in sending international SMS, please add those countries to your Project's allowlist via the Dashboard or Programmatic Workspace Actions, and add credit card details to your account.
Even when international SMS is enabled, we do not support sending SMS to countries on our Unsupported countries list.
Next steps
Collect the OTP which was delivered to the User. Call Authenticate OTP using the OTP code along with the phone_id found in the response as the method_id.
Parameters:
- phone_number
The phone number to use for one-time passcodes. The phone number should be in E.164 format (i.e. +1XXXXXXXXXX). You may use +10000000000 to test this endpoint, see Testing for more detail. The type of this field is
String.- expiration_minutes
Set the expiration for the one-time passcode, in minutes. The minimum expiration is 1 minute and the maximum is 10 minutes. The default expiration is 2 minutes. The type of this field is nilable
Integer.- attributes
Provided attributes to help with fraud detection. These values are pulled and passed into Stytch endpoints by your application. The type of this field is nilable
Attributes(+object+).- create_user_as_pending
Flag for whether or not to save a user as pending vs active in Stytch. Defaults to false. If true, users will be saved with status pending in Stytch's backend until authenticated. If false, users will be created as active. An example usage of a true flag would be to require users to verify their phone by entering the OTP code before creating an account for them. The type of this field is nilable
Boolean.- locale
Used to determine which language to use when sending the user this delivery method. Parameter is an IETF BCP 47 language tag, e.g.
"en".
Currently supported languages are English ("en"), Spanish ("es"), French ("fr") and Brazilian Portuguese ("pt-br"); if no value is provided, the copy defaults to English.
Request support for additional languages here!
The type of this field is nilable +LoginOrCreateRequestLocale+ (string enum).
Returns:
An object with the following fields:
- request_id
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is
String.- user_id
The unique ID of the affected User. The type of this field is
String.- phone_id
The unique ID for the phone number. The type of this field is
String.- user_created
In
login_or_createendpoints, this field indicates whether or not a User was just created. The type of this field isBoolean.- status_code
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is
Integer.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/stytch/otps.rb', line 274 def login_or_create( phone_number:, expiration_minutes: nil, attributes: nil, create_user_as_pending: nil, locale: nil ) headers = {} request = { phone_number: phone_number } request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil? request[:attributes] = attributes unless attributes.nil? request[:create_user_as_pending] = create_user_as_pending unless create_user_as_pending.nil? request[:locale] = locale unless locale.nil? post_request('/v1/otps/sms/login_or_create', request, headers) end |
#send(phone_number:, expiration_minutes: nil, attributes: nil, locale: nil, user_id: nil, session_token: nil, session_jwt: nil) ⇒ Object
Send a one-time passcode (OTP) to a user's phone number. If you'd like to create a user and send them a passcode with one request, use our log in or create endpoint.
Note that sending another OTP code before the first has expired will invalidate the first code.
Cost to send SMS OTP
Before configuring SMS or WhatsApp OTPs, please review how Stytch bills the costs of international OTPs and understand how to protect your app against toll fraud.
Note: SMS to phone numbers outside of the US and Canada is disabled by default for customers who did not use SMS prior to October 2023. If you're interested in sending international SMS, please add those countries to your Project's allowlist via the Dashboard or Programmatic Workspace Actions, and add credit card details to your account.
Even when international SMS is enabled, we do not support sending SMS to countries on our Unsupported countries list.
Add a phone number to an existing user
This endpoint also allows you to add a new phone number to an existing Stytch User. Including a user_id, session_token, or session_jwt in your Send one-time passcode by SMS request will add the new, unverified phone number to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new phone number will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the User object, and any subsequent login requests using that phone number will create a new User.
Next steps
Collect the OTP which was delivered to the user. Call Authenticate OTP using the OTP code along with the phone_id found in the response as the method_id.
Parameters:
- phone_number
The phone number to use for one-time passcodes. The phone number should be in E.164 format (i.e. +1XXXXXXXXXX). You may use +10000000000 to test this endpoint, see Testing for more detail. The type of this field is
String.- expiration_minutes
Set the expiration for the one-time passcode, in minutes. The minimum expiration is 1 minute and the maximum is 10 minutes. The default expiration is 2 minutes. The type of this field is nilable
Integer.- attributes
Provided attributes to help with fraud detection. These values are pulled and passed into Stytch endpoints by your application. The type of this field is nilable
Attributes(+object+).- locale
Used to determine which language to use when sending the user this delivery method. Parameter is an IETF BCP 47 language tag, e.g.
"en".
Currently supported languages are English ("en"), Spanish ("es"), French ("fr") and Brazilian Portuguese ("pt-br"); if no value is provided, the copy defaults to English.
Request support for additional languages here!
The type of this field is nilable +SendRequestLocale+ (string enum).
- user_id
The unique ID of a specific User. You may use an
external_idhere if one is set for the user. The type of this field is nilableString.- session_token
The
session_tokenassociated with a User's existing Session. The type of this field is nilableString.- session_jwt
The
session_jwtassociated with a User's existing Session. The type of this field is nilableString.
Returns:
An object with the following fields:
- request_id
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is
String.- user_id
The unique ID of the affected User. The type of this field is
String.- phone_id
The unique ID for the phone number. The type of this field is
String.- status_code
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is
Integer.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/stytch/otps.rb', line 195 def send( phone_number:, expiration_minutes: nil, attributes: nil, locale: nil, user_id: nil, session_token: nil, session_jwt: nil ) headers = {} request = { phone_number: phone_number } request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil? request[:attributes] = attributes unless attributes.nil? request[:locale] = locale unless locale.nil? request[:user_id] = user_id unless user_id.nil? request[:session_token] = session_token unless session_token.nil? request[:session_jwt] = session_jwt unless session_jwt.nil? post_request('/v1/otps/sms/send', request, headers) end |