Send a message with Node.js
This quickstart sends a published message definition through the sandbox. It runs only on a trusted server because the SDK contains your secret API key.
-
Install the SDK
Terminal window npm install @fabric-messaging/sdk -
Store your sandbox key
.env FABRIC_API_KEY=sk_test_replace_with_your_keyLoad this value with your framework’s server-side environment configuration. Do not prefix it with
PUBLIC_or expose it to client bundles. -
Send by definition key
send.ts import { Fabric } from "@fabric-messaging/sdk";const apiKey = process.env.FABRIC_API_KEY;if (!apiKey) {throw new Error("FABRIC_API_KEY is required");}const fabric = new Fabric({ apiKey });const response = await fabric.messages.send("welcome.otp", {to: "+233201234567",data: {name: "Ama",code: "401922",},reference: "signin:customer-4821",idempotencyKey: "otp:customer-4821:attempt-1",});console.log(response.data.id, response.data.status, response.data.cost.minor); -
Run it
Terminal window node --env-file=.env send.ts
In sandbox, the recipient is virtual and the delivery resolves without contacting a carrier. Open the dashboard and confirm that the delivery, rendered content, and cost match the response.
Retry safely
Section titled “Retry safely”Keep the same idempotencyKey when retrying the same logical send. Fabric returns the original
result instead of creating a second delivery. Use a new key only when you intend to send again.