SMS Service

Better Auth Infrastructure provides a managed SMS service for sending OTP codes for phone verification and two-factor authentication. Send verification codes without managing SMS providers.

Overview

The SMS service offers:

  • Pre-built templates for phone verification, two-factor, and sign-in OTP
  • E.164 phone numbers
  • Managed delivery

Call sendSMS and createSMSSender from server code only. They authenticate with your Infrastructure API key. SMS delivery is intended only for authentication flows.

Installation

The SMS service is included in @better-auth/infra. Install the package and set BETTER_AUTH_API_KEY as described in Getting Started.

import { sendSMS } from "@better-auth/infra";

Quick Start

import { sendSMS } from "@better-auth/infra";

const result = await sendSMS({
  to: "+1234567890",
  code: "123456",
  template: "phone-verification",
});

if (!result.success) {
  console.error("Failed to send SMS:", result.error);
}

These helpers return a result instead of throwing when delivery fails. Check result.success when the caller needs to know whether the message was sent.

To reuse credentials across calls:

import { createSMSSender } from "@better-auth/infra";

const smsSender = createSMSSender({
  apiKey: process.env.BETTER_AUTH_API_KEY,
});

await smsSender.send({
  to: "+1234567890",
  code: "123456",
  template: "two-factor",
});

Use with Better Auth

dash() does not send SMS for you. Pass sendSMS into the plugin callback that already receives the OTP, such as phoneNumber.sendOTP. Keep sendOTP async and await the send:

auth.ts
import { betterAuth } from "better-auth";
import { phoneNumber } from "better-auth/plugins";
import { sendSMS } from "@better-auth/infra";

export const auth = betterAuth({
  plugins: [
    phoneNumber({
      sendOTP: async ({ phoneNumber, code }) => {
        const result = await sendSMS({
          to: phoneNumber,
          code,
          template: "phone-verification",
        });
        if (!result.success) {
          console.error("Failed to send SMS:", result.error);
        }
      },
    }),
  ],
});

On serverless, set advanced.backgroundTasks.handler to your platform's waitUntil. Better Auth then passes the sendOTP promise to the handler, so the HTTP response does not wait for delivery.

Available Templates

phone-verification

Sends a verification code for phone number verification.

await sendSMS({
  to: "+1234567890",
  code: "123456",
  template: "phone-verification",
});

Example message:

Your verification code is 123456. It expires in 10 minutes.

two-factor

Sends a two-factor authentication code.

await sendSMS({
  to: "+1234567890",
  code: "123456",
  template: "two-factor",
});

Example message:

Your two-factor authentication code is 123456. Do not share this code with anyone.

sign-in-otp

Sends a one-time password for passwordless sign-in.

await sendSMS({
  to: "+1234567890",
  code: "123456",
  template: "sign-in-otp",
});

Example message:

Your sign-in code is 123456. It expires in 10 minutes.

Default (No Template)

If you don't specify a template, a generic verification message is sent:

await sendSMS({
  to: "+1234567890",
  code: "123456",
});

Example message:

Your verification code is 123456.

Phone Number Format

Phone numbers must be in E.164 format:

+[country code][number]

Examples:

  • US: +14155551234
  • UK: +447911123456
  • Germany: +4915112345678
  • Japan: +819012345678

Common mistakes:

  • Missing + prefix: 14155551234 ❌
  • Including spaces: +1 415 555 1234 ❌
  • Including dashes: +1-415-555-1234 ❌
  • Including parentheses: +1 (415) 555-1234 ❌

Configuration

OptionTypeDescription
apiKeystringYour API key. Falls back to BETTER_AUTH_API_KEY.
apiUrlstringBetter Auth Infrastructure API URL. Default: https://dash.better-auth.com
apiOptionsobjectHTTP client options. Accepts timeout?: number (ms, default 3000) and headers?: Record<string, string>.
apiTimeoutnumberDeprecated alias for apiOptions.timeout.

Pass this object to createSMSSender, or as the second argument to sendSMS. If you omit apiKey or apiUrl, the client reads BETTER_AUTH_API_KEY and BETTER_AUTH_API_URL.

API Reference

sendSMS

async function sendSMS(
  options: SendSMSOptions,
  config?: SMSConfig
): Promise<SendSMSResult>
PropertyTypeRequiredDescription
tostringYesPhone number in E.164 format
codestringYesThe OTP code to send
templateSMSTemplateIdNophone-verification, two-factor, or sign-in-otp. Omit for a generic verification message.
clientIpstringNoEnd-user IP for abuse limits. Omit in Better Auth callbacks. If you send outside a request, pass only an IP your server determined — never a client-supplied value.

createSMSSender

const smsSender = createSMSSender(config?: SMSConfig);

await smsSender.send(options);

Results

interface SendSMSResult {
  success: boolean;
  messageId?: string;
  error?: string;
}

Treat error as an opaque string.

Plan Requirements

FeatureStarterProBusinessEnterprise
Transactional SMS-YesYesYes

Transactional SMS is available on Pro plans and above.