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
npm install sms-bridgepnpm add sms-bridgeyarn add sms-bridgebun add sms-bridge| Module | Exports |
|---|---|
sms-bridge | SMSBridge, createDashboard, domain types, ports, and the built-in adapters. |
sms-bridge/dashboard | createDashboard. |
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, orcustom. Default:memorywhendatabaseis omitted from a library configuration. The self-hosted server uses SQLite.path<string>SQLite file. Used whentypeissqlite.repositories<Object>Repository implementations. Used whentypeiscustom. See Custom database.
redis<Object>Optional. When omitted, balance and health are kept in memory andsend()delivers directly.url<string>Redis connection string.useQueue<boolean>Whentrue, andurlis set,send()enqueues the message and returnsqueued.
routing<Object>Optional.defaultStrategy<string>priority_failover,weighted_distribution,health_aware,rule_based, orfixed.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:
| Field | Type | Description |
|---|---|---|
type | string | bulksmsbd, greenweb, sandbox, or a custom type. Defaults to the provider id. |
apiKey | string | Required for bulksmsbd and greenweb. |
apiSecret | string | Passed to BulkSMSBd when set. |
baseUrl | string | Vendor base URL. BulkSMSBd defaults to https://api.bulksmsbd.com/v1. GreenWeb defaults to https://api.greenweb.com.bd/api.php. |
senderIds | string[] | Sender IDs for this provider. |
gateway | object | A constructed IProviderGateway. When set, apiKey and baseUrl are not required. |
config | any | Opaque 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, orcritical. Default:normal.profileId<string>Profile id. Default:"default".routingHint<string>Provider id, name, or type.isMasked<boolean>Whentrue, only a masked sender ID can be selected. Default:false.metadata<Object>Forwarded togateway.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.
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.
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.
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.
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.