Skip to content

Library ​

The library embeds sms-bridge in a Node.js or Bun process. The application calls SMSBridge.send(). It does not call the HTTP API.

An application written in another language self-hosts the server and calls the HTTP API. npm, pnpm, yarn, and bun install this package. Node.js 24 runs it with the built-in node:sqlite module. Node.js 22 runs it when started with --experimental-sqlite. Bun uses bun:sqlite. The same import works in either runtime.

Installation ​

sh
npm install sms-bridge
sh
pnpm add sms-bridge
sh
yarn add sms-bridge
sh
bun add sms-bridge
ModuleExports
sms-bridgeSMSBridge, createDashboard, domain types, ports, and the built-in adapters.
sms-bridge/dashboardcreateDashboard.

The examples below are also in examples/library-usage.ts and examples/dashboard-usage.ts.

new SMSBridge(options) ​

  • options <Object>
    • providers <Object> Required. A map of provider id to a provider entry.
    • database <Object> Optional.
      • type <string> memory, sqlite, or custom. Default: memory when database is omitted from a library configuration. The self-hosted server uses SQLite.
      • path <string> SQLite file. Used when type is sqlite.
      • repositories <Object> Repository implementations. Used when type is custom. See Custom database.
    • redis <Object> Optional. When omitted, balance and health are kept in memory and send() delivers directly.
      • url <string> Redis connection string.
      • useQueue <boolean> When true, and url is set, send() enqueues the message and returns queued.
    • routing <Object> Optional.
      • defaultStrategy <string> priority_failover, weighted_distribution, health_aware, rule_based, or fixed.
      • weightedWeights <Object> Map of provider id to a number.
      • rules <Array> Objects { prefix: string, provider: string }.
    • gateways <Object> Optional. Map of provider type to a factory (entry) => gateway. See Custom providers.

A provider entry:

FieldTypeDescription
typestringbulksmsbd, greenweb, sandbox, or a custom type. Defaults to the provider id.
apiKeystringRequired for bulksmsbd and greenweb.
apiSecretstringPassed to BulkSMSBd when set.
baseUrlstringVendor base URL. BulkSMSBd defaults to https://api.bulksmsbd.com/v1. GreenWeb defaults to https://api.greenweb.com.bd/api.php.
senderIdsstring[]Sender IDs for this provider.
gatewayobjectA constructed IProviderGateway. When set, apiKey and baseUrl are not required.
configanyOpaque value passed to a factory in gateways.

sms.init() builds the container. Call it once before send(), migrate(), startWorker(), or getContainer().

sms.send(message) ​

  • message <Object>
    • recipient <string>
    • body <string>
    • priority <string> low, normal, high, or critical. Default: normal.
    • profileId <string> Profile id. Default: "default".
    • routingHint <string> Provider id, name, or type.
    • isMasked <boolean> When true, only a masked sender ID can be selected. Default: false.
    • metadata <Object> Forwarded to gateway.send. Stored on the queue job. Not written to SQLite.

Returns <Promise<Object>>. In direct mode the object is the provider result: success, and messageId, error, providerId, providerName, and routingStrategy when the attempt produced them. In queued mode the object is { success: true, messageId, status: "queued" }.

Direct delivery ​

send() waits for the provider. No Redis connection is required. The database may be :memory: or a SQLite file.

ts
import { SMSBridge } from "sms-bridge";

const sms = new SMSBridge({
  providers: {
    sandbox: {
      apiKey: "sandbox-api-key",
      baseUrl: "https://api.sandbox.mock",
      type: "sandbox",
      senderIds: ["BRAND_NAME"],
    },
  },
  database: { type: "memory" },
  routing: { defaultStrategy: "priority_failover" },
});

await sms.init();

const result = await sms.send({
  recipient: "01700000000",
  body: "Hello",
  priority: "high",
});

Queued delivery ​

Set redis.url and redis.useQueue to true. send() returns after the job is enqueued. startWorker() has to run in the same process, because the worker uses the providers registered on that instance. A bun run worker process in this repository loads only the built-in providers from the environment and from configs.yaml.

startWorker() throws SMSBridge.startWorker() requires redis.url when redis.url is not set. It returns the BullMQ Worker. Call worker.close() before the process exits.

ts
const sms = new SMSBridge({
  providers: {
    bulksmsbd: {
      apiKey: process.env.BULKSMSBD_API_KEY ?? "",
      senderIds: ["BRAND_NAME"],
    },
  },
  database: { type: "sqlite", path: "./data/sms-bridge.db" },
  redis: { url: "redis://localhost:6379", useQueue: true },
});

await sms.init();
await sms.migrate();
const worker = sms.startWorker();

const result = await sms.send({
  recipient: "01700000000",
  body: "Hello",
  metadata: { campaign: "otp" },
});
// { success: true, messageId, status: "queued" }

sms.migrate() applies the SQL files in the package directory src/infra/db/migrations. In this repository those files are gitignored. A checkout applies the schema with bunx drizzle-kit push.

getContainer() returns the repositories and providerRegistry after init().

Dashboard ​

createDashboard(sms) returns a Hono application. The UI uses hash routing and relative asset URLs, so it can be mounted at any path.

ts
import { Hono } from "hono";
import { createDashboard, listen, SMSBridge } from "sms-bridge";

const sms = new SMSBridge({
  providers: {
    sandbox: {
      type: "sandbox",
      apiKey: "sandbox",
      senderIds: ["SANDBOX"],
    },
  },
  database: { type: "memory" },
});
await sms.init();

const app = new Hono();
app.route("/admin/sms", createDashboard(sms));

await listen({ port: 4000, fetch: app.fetch });

prepublishOnly builds the UI into dashboard/dist. A dependency on the git repository needs that build before the UI files exist.

Custom database ​

Set database.type to "custom" to skip SQLite. The objects in database.repositories implement the ports in src/core/ports/repositories.ts.

ts
database: {
  type: "custom",
  repositories: {
    message,
    provider,
    providerConfig,
    profile,
  },
}

providerConfig.upsert is called during init(). providerConfig.findEnabledOrderedByPriority() supplies the candidate list when a profile has no linked providers. profile.findById may return null. Routing then uses every enabled provider in the configuration.