Skip to main content

Send a message

import { createClient } from "@messages-dev/sdk";

const client = createClient();

await client.sendMessage({
  from: "+15551234567",
  to: "+15559876543",
  text: "Hello from Messages.dev!",
});
FieldRequiredDescription
fromYesThe line handle to send from
toYesRecipient phone number or Apple ID
textYesMessage text

Track delivery

Register a webhook for message.sent and your server will receive a POST when the message is delivered:
import { verifyWebhook } from "@messages-dev/sdk";

app.post("/webhooks", async (req, res) => {
  const event = await verifyWebhook(
    req.body,
    req.headers["x-webhook-signature"],
    "your_webhook_secret",
  );

  if (event.event === "message.sent") {
    console.log(`Delivered: ${event.data.id}`);
  }

  res.sendStatus(200);
});
StatusMeaning
pendingQueued for delivery
claimedBeing processed
sentDelivered
failedDelivery failed (check the error field)

Errors

StatusCodeCause
400missing_required_parameterMissing from, to, or text
401missing_api_keyNo Authorization header
403insufficient_scopeKey lacks messages:write scope
403line_not_accessibleKey can’t access this line
404line_not_foundInvalid line handle
With the SDK, errors are thrown as typed exceptions:
import { createClient, InvalidRequestError } from "@messages-dev/sdk";

try {
  await client.sendMessage({ from: "+15551234567", to: "+1555...", text: "Hi" });
} catch (err) {
  if (err instanceof InvalidRequestError) {
    console.error(err.code, err.param);
  }
}