OtpResource
The otp resource provides methods for creating, verifying, and managing one-time passwords for authentication flows.
new()
await contiguity.otp.new(params: OtpNewParams): Promise<{ otp_id: string }>
Generate and send a new one-time password to a phone number.
Phone number to send the OTP to, in E.164 format (e.g., +1234567890).
Language code for the OTP message (e.g., en, es, fr).
Your application or company name to include in the message.
Unique identifier for the OTP session. Use this to verify the code later.
const result = await contiguity.otp.new({
to: '+1234567890',
language: 'en',
name: 'MyApp'
});
console.log(result.otp_id); // "otp_..."
verify()
await contiguity.otp.verify(params: OtpVerifyParams): Promise<{ verified: boolean }>
Verify an OTP code entered by the user.
The OTP session ID returned from new().
The OTP code entered by the user.
true if the OTP code is correct, false otherwise.
const result = await contiguity.otp.verify({
otp_id: 'otp_...',
otp: '123456'
});
if (result.verified) {
console.log('OTP verified successfully!');
} else {
console.log('Invalid OTP code.');
}
resend()
await contiguity.otp.resend(params: OtpResendParams): Promise<{ resent: boolean }>
Resend the OTP code to the same phone number.
The OTP session ID to resend.
true if the OTP was successfully resent.
const result = await contiguity.otp.resend({
otp_id: 'otp_...'
});
console.log(result.resent); // true
reverse.initiate()
await contiguity.otp.reverse.initiate(params: OtpReverseInitiateParams): Promise<any>
Initiate a reverse OTP flow where the user calls or texts a number to verify.
params
OtpReverseInitiateParams
required
The phone number to verify.
Custom verification factor (max 16 characters).
Destination number for the verification.
Language code for instructions.
URL to redirect to after successful verification.
const result = await contiguity.otp.reverse.initiate({
number: '+1234567890',
language: 'en',
success_url: 'https://yourapp.com/verified'
});
console.log(result);
reverse.verify()
await contiguity.otp.reverse.verify(otp_id: string): Promise<any>
Check the verification status of a reverse OTP session.
The reverse OTP session ID.
const status = await contiguity.otp.reverse.verify('otp_...');
console.log(status);
reverse.cancel()
await contiguity.otp.reverse.cancel(otp_id: string): Promise<any>
Cancel an ongoing reverse OTP session.
The reverse OTP session ID to cancel.
await contiguity.otp.reverse.cancel('otp_...');
Complete Example
import { Contiguity } from 'contiguity';
const contiguity = new Contiguity('contiguity_sk_...');
// Step 1: Send OTP
const { otp_id } = await contiguity.otp.new({
to: '+1234567890',
language: 'en',
name: 'MyApp'
});
// Step 2: User enters code, verify it
const userCode = '123456'; // From user input
const { verified } = await contiguity.otp.verify({
otp_id,
otp: userCode
});
if (verified) {
// Proceed with authentication
console.log('User verified!');
} else {
// Show error or allow resend
console.log('Invalid code');
}
// Step 3 (optional): Resend if needed
await contiguity.otp.resend({ otp_id });