MessagePipedocs
SDKs

Node / TypeScript

@messagepipe/sdk: typed responses, automatic retry, idempotency helper.

Install

npm install @messagepipe/sdk
# or
pnpm add @messagepipe/sdk

Initialise

import { MessagePipe } from "@messagepipe/sdk";

const mp = new MessagePipe({
  apiKey: process.env.MP_KEY!,
  // optional, default is https://api.dev.messagepipe.dev
  baseUrl: process.env.MP_BASE_URL,
});

Send

const send = await mp.email.send({
  to: "maya@example.com",
  subject: "Welcome!",
  html: "<p>Glad to have you.</p>",
});

console.log(send.id, send.status); // msg_…  "queued"

The SDK auto-generates an Idempotency-Key per call. Override it when you have a natural stable id:

await mp.email.send(
  { to: "...", template: "welcome-email", data: { ... } },
  { idempotencyKey: `order:${orderId}:confirmation` },
);

Verify a webhook

import { verifyWebhook } from "@messagepipe/sdk";

// In your Express / Fastify / Next.js route handler:
const signature = req.headers["x-mp-signature"] as string;
const timestamp = req.headers["x-mp-timestamp"] as string;
const rawBody = await getRawBody(req); // your framework's helper

if (!verifyWebhook(rawBody, signature, timestamp, process.env.MP_WEBHOOK_SECRET!)) {
  return res.status(400).end("invalid signature");
}

See Webhooks for the payload reference.

Catching errors

import { MessagePipe, MessagePipeError } from "@messagepipe/sdk";

try {
  await mp.email.send({ ... });
} catch (err) {
  if (err instanceof MessagePipeError && err.code === "credits_exhausted") {
    await topUpCredits();
  } else {
    throw err;
  }
}