Better Auth
Better Auth creates the one-time code and checks the code the user submits. sms-bridge delivers the SMS.
Add the phone number plugin and run the Better Auth migration so the user table has a phone number column. The plugin calls sendOTP with { phoneNumber, code }. Throw from that function when delivery fails. The request then reports the failure.
Leave verifyOTP unset. The built-in providers deliver a message. Checking the code stays in Better Auth.
Use the library when the Better Auth process runs on Node.js or Bun. Self-host the server and call the HTTP API from any other runtime.
Library
Call init() once at startup. Omit redis, or leave redis.useQueue unset, so send() waits for the provider. success is then the provider result.
import { betterAuth } from "better-auth";
import { phoneNumber, twoFactor } from "better-auth/plugins";
import { SMSBridge } from "sms-bridge";
const sms = new SMSBridge({
providers: {
bulksmsbd: {
apiKey: process.env.BULKSMSBD_API_KEY ?? "",
senderIds: ["BRAND_NAME"],
},
},
database: { type: "memory" },
});
await sms.init();
export const auth = betterAuth({
plugins: [
phoneNumber({
sendOTP: ({ phoneNumber, code }) => sendCode(phoneNumber, code),
}),
twoFactor({
otpOptions: {
sendOTP: ({ user, otp }) => {
if (!user.phoneNumber) {
throw new Error("User has no phone number");
}
return sendCode(user.phoneNumber, otp);
},
},
}),
],
});
async function sendCode(recipient: string, code: string) {
const result = await sms.send({
recipient: toLocalBd(recipient),
body: `Your code is ${code}`,
priority: "high",
});
if (!result.success) {
throw new Error(result.error ?? "SMS send failed");
}
}
function toLocalBd(phoneNumber: string) {
if (phoneNumber.startsWith("+880")) return `0${phoneNumber.slice(4)}`;
return phoneNumber;
}twoFactor is the two-factor plugin. Its otpOptions.sendOTP callback receives { user, otp }. With the phone number plugin installed, user.phoneNumber is the number stored for that user. Enable OTP as the second factor with method: "otp" when calling twoFactor.enable. The client requests the code with authClient.twoFactor.sendOtp().
HTTP API
POST /v1/sms/send returns status 202 after the message is queued. The worker delivers it after that response. A status other than 202 means the request was rejected. The bearer token is the profile API key printed on the first start of the server.
The plugin callbacks match the library example. Replace sendCode and keep toLocalBd:
async function sendCode(recipient: string, code: string) {
const response = await fetch(`${process.env.SMS_BRIDGE_URL}/v1/sms/send`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SMS_BRIDGE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
recipient: toLocalBd(recipient),
body: `Your code is ${code}`,
priority: "high",
}),
});
if (!response.ok) {
throw new Error("SMS send failed");
}
}SMS_BRIDGE_URL is the server origin, for example http://localhost:8080.
Recipient
The examples in this guide send 01700000000. BulkSMSBd and GreenWeb forward recipient unchanged. Better Auth stores the string the client submits, and verify receives that same string. Convert the stored form inside sendCode when it differs from the form the provider accepts.
toLocalBd maps +8801700000000 to 01700000000. A provider that expects another form needs its own conversion. The HTTP API accepts a recipient of length 10 to 20.
Client
await authClient.phoneNumber.sendOtp({
phoneNumber: "+8801700000000",
});
await authClient.phoneNumber.verify({
phoneNumber: "+8801700000000",
code: "123456",
});Pass the stored number to verify. The conversion in sendCode applies only to the SMS gateway.