twillio_verify
A Gleam library for sending and verifying SMS codes using Twilio Verify API.
Example
import twillio_verify.{TwilioConfig, Sms}
let config = TwilioConfig(
account_sid: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
auth_token: "your_auth_token",
service_sid: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
)
// Send verification code
let request = twillio_verify.send_verification_request(config, "+1xxxxxxxxxxxx", Sms)
// Check verification code
let request = twillio_verify.check_verification_request(config, "+1xxxxxxxx", "1111111")
Types
Channel for sending verification code
pub type Channel {
Sms
Call
Email
Whatsapp
}
Constructors
-
SmsSend via SMS
-
CallSend via phone call
-
EmailSend via email
-
WhatsappSend via WhatsApp
Response from checking a verification
pub type CheckVerificationResponse {
CheckVerificationResponse(
sid: String,
account_sid: String,
service_sid: String,
to: String,
channel: String,
status: VerificationStatus,
valid: Bool,
amount: option.Option(String),
payee: option.Option(String),
date_created: option.Option(String),
date_updated: option.Option(String),
)
}
Constructors
-
CheckVerificationResponse( sid: String, account_sid: String, service_sid: String, to: String, channel: String, status: VerificationStatus, valid: Bool, amount: option.Option(String), payee: option.Option(String), date_created: option.Option(String), date_updated: option.Option(String), )
Lookup information from the verification
pub type Lookup {
Lookup(carrier: option.Option(String))
}
Constructors
-
Lookup(carrier: option.Option(String))
A single code send attempt
pub type SendCodeAttempt {
SendCodeAttempt(
attempt_sid: String,
channel: String,
time: String,
)
}
Constructors
-
SendCodeAttempt( attempt_sid: String, channel: String, time: String, )
Response from sending a verification
pub type SendVerificationResponse {
SendVerificationResponse(
sid: String,
account_sid: String,
service_sid: String,
to: String,
channel: String,
status: VerificationStatus,
valid: Bool,
amount: option.Option(String),
payee: option.Option(String),
date_created: String,
date_updated: String,
lookup: Lookup,
send_code_attempts: List(SendCodeAttempt),
url: String,
)
}
Constructors
-
SendVerificationResponse( sid: String, account_sid: String, service_sid: String, to: String, channel: String, status: VerificationStatus, valid: Bool, amount: option.Option(String), payee: option.Option(String), date_created: String, date_updated: String, lookup: Lookup, send_code_attempts: List(SendCodeAttempt), url: String, )
Configuration for Twilio API
pub type TwilioConfig {
TwilioConfig(
account_sid: String,
auth_token: String,
service_sid: String,
)
}
Constructors
-
TwilioConfig( account_sid: String, auth_token: String, service_sid: String, )Arguments
- account_sid
-
Your Twilio Account SID (starts with AC)
- auth_token
-
Your Twilio Auth Token
- service_sid
-
Your Twilio Verify Service SID (starts with VA)
Errors that can occur when using the Twilio API
pub type TwilioError {
TwilioApiError(
code: Int,
message: String,
more_info: option.Option(String),
)
UnexpectedResponseError(status: Int, body: String)
DecodeError(String)
}
Constructors
-
TwilioApiError( code: Int, message: String, more_info: option.Option(String), )An error returned by the Twilio API
-
UnexpectedResponseError(status: Int, body: String)An unexpected response from the API
-
DecodeError(String)Failed to decode the response
Status of a verification
pub type VerificationStatus {
Pending
Approved
Canceled
Unknown(String)
}
Constructors
-
Pending -
Approved -
Canceled -
Unknown(String)
Values
pub fn check_verification_request(
config: TwilioConfig,
to: String,
code: String,
) -> request.Request(String)
Create an HTTP request to check a verification code.
Example
let request = check_verification_request(config, "+1234567890", "123456")
// Send with your HTTP client (hackney, httpc, etc.)
pub fn check_verification_response(
response: response.Response(String),
) -> Result(CheckVerificationResponse, TwilioError)
Parse the response from checking a verification.
pub fn is_approved(response: CheckVerificationResponse) -> Bool
Check if a verification was approved
pub fn send_verification_request(
config: TwilioConfig,
to: String,
channel: Channel,
) -> request.Request(String)
Create an HTTP request to send a verification code.
Example
let request = send_verification_request(config, "+1234567890", Sms)
// Send with your HTTP client (hackney, httpc, etc.)
pub fn send_verification_response(
response: response.Response(String),
) -> Result(SendVerificationResponse, TwilioError)
Parse the response from sending a verification.