Webhooks
Get an HTTPS request in your own service whenever a text arrives, is starred or is deleted.
A webhook tells your service that something happened in your archive, so it doesn't have to keep asking. pSMS sends a small JSON request to your URL; your service then fetches the message through the API and decrypts it with the archive key.
Webhooks never contain message text or sender names. Only the message ID.
There are two kinds:
- Self-hosted webhooks, sent by pSMS on your computer. Free and unlimited, but only sent while that computer is on and pSMS is running.
- Managed webhooks on papamo.dev, sent by Papamo for hosted and connected archives, with a delivery log and replay. They don't need your computer. Included in Plus and Enterprise.
For one-time codes, where a late delivery is useless, use managed webhooks.
What your service receives
Every delivery is a POST to your URL:
POST /psms HTTP/1.1
Host: example.com
Authorization: Bearer YOUR_WEBHOOK_SECRET
Content-Type: application/json
{"delivery_id": 1042, "event": "message.created", "message_id": "33f25af6-8774-4d98-b5bf-d73545644319"}| Field | Meaning |
|---|---|
delivery_id | Unique for this delivery. Retries of the same delivery reuse it. |
event | What happened (below) |
message_id | The message it's about. Fetch it with GET /v1/messages/{id}, or on papamo.dev find it in GET /messages. |
Events
| Event | When | Self-hosted | Managed |
|---|---|---|---|
message.created | A new message is stored | Yes | Yes |
message.updated | A message is starred or unstarred | Yes | — |
message.deleted | A message is deleted | Yes | — |
Handling a delivery
- Check the secret. Compare the
Authorizationheader with your webhook's secret in constant time, and reject anything else with 401. - Answer quickly. Return any 2xx within 8 seconds (10 for managed webhooks). Do slow work afterwards, for example in a queue.
- Expect repeats. The same delivery can arrive more than once. Use
delivery_id(ormessage_idandevent) to skip ones you've handled. - Fetch the message with your owner or API token, then decrypt it on your own system. Don't act on the request body alone: it only names a message, and the message is what you trust.
A handler in Node.js that reads one-time codes as they arrive and deletes each text once it's used. It needs psms-decrypt.mjs next to it:
import express from "express";
import { timingSafeEqual } from "node:crypto";
import { archiveKey, openEnvelope, findCode } from "./psms-decrypt.mjs";
const app = express();
const secret = Buffer.from("Bearer " + process.env.PSMS_WEBHOOK_SECRET);
const key = archiveKey(process.env.PSMS_RECOVERY_WORDS);
const api = process.env.PSMS_API; // your archive's /v1, or https://na.sms.papamo.dev/free/v1
const seen = new Set();
// Your own logic, for example handing the code to a login script.
async function useCode(sender, code) {
console.log(`Code from ${sender}: ${code}`);
}
async function call(path, init = {}) {
const res = await fetch(api + path, {
...init,
headers: { authorization: `Bearer ${process.env.PSMS_TOKEN}` },
});
if (!res.ok) throw new Error(`${init.method || "GET"} ${path}: ${res.status}`);
return res.status === 204 ? null : res.json();
}
async function fetchMessage(id) {
if (new URL(api).hostname.endsWith(".sms.papamo.dev")) {
// papamo.dev has no single-message request: look through the newest messages.
const { messages } = await call("/messages?limit=20");
return messages.find((m) => m.id === id);
}
return call(`/messages/${id}`);
}
app.post("/psms", express.json(), async (req, res) => {
const given = Buffer.from(req.get("authorization") || "");
if (given.length !== secret.length || !timingSafeEqual(given, secret)) return res.sendStatus(401);
res.sendStatus(204); // answer first
const { delivery_id, event, message_id } = req.body;
if (event !== "message.created" || seen.has(delivery_id)) return;
seen.add(delivery_id);
try {
const message = await fetchMessage(message_id);
if (message?.sender !== "papamo:encrypted:v1") return; // gone, or an OpenPGP message
const { sender, body } = openEnvelope(key, message.body);
const code = findCode(body);
if (!code) return;
await useCode(sender, code);
await call(`/messages/${message_id}`, { method: "DELETE" }); // don't keep used codes
} catch (error) {
console.error(`delivery ${delivery_id}:`, error.message);
}
});
app.listen(8080);seen lives in memory, so a restart forgets it. Keep handled IDs in your database if repeats would cause harm.
Keep it safe
- The secret is the only check. Each delivery carries just the bearer secret: there's no signature or timestamp. Anyone with the secret can send your URL fake deliveries, so keep it out of source code and logs, and replace it if it leaks:
psms manage webhook rotate IDon your computer, or remove the destination and add it again on papamo.dev. - Trust the API, not the request. A forged delivery can only name a message ID. Because your handler fetches the message with your token and decrypts it with your key, it can't be fed a fake code.
- Delete codes you've used. One-time codes are worthless after a few minutes but stay in the archive until deleted. Delete each with
DELETE /v1/messages/{id}(DELETE /messages/{id}on papamo.dev) once you've used it, as the handler above does, or clear old ones from the inbox. - Give the handler its own token:
psms manage access create apion your computer, or an API key from Settings → API keys in your papamo.dev inbox. Revoke it when you retire the handler. The handler also holds your recovery words, which open every message, so run it only on systems you control.
Retries
If your service doesn't answer with 2xx in time, pSMS tries again later, doubling the wait each time up to an hour between attempts. Self-hosted webhooks wait 10 seconds, then 20, 40 and so on; managed webhooks start at 20 seconds.
- Self-hosted keeps retrying until it succeeds or you remove the destination.
- Managed stops after 10 attempts and marks the delivery failed. You can replay it.
Deliveries for one destination can arrive out of order. Use the message's own timestamps when order matters.
Self-hosted webhooks
Add a destination in Settings → Event webhooks, or:
psms manage webhook add https://example.com/psms
psms manage webhook list
psms manage webhook rotate ID
psms manage webhook remove IDOr with the API and an owner token:
| Request | Purpose |
|---|---|
POST /v1/webhooks | Add {"url": "https://…"}. Returns the secret once. |
GET /v1/webhooks | List destinations |
POST /v1/webhooks/{id}/rotate | Replace the secret. Returns the new one once. |
DELETE /v1/webhooks/{id} | Remove the destination and its pending deliveries |
Destinations must use HTTPS. Each destination gets its own secret; pSMS keeps it so it can send it, so protect the archive files and backups.
Managed webhooks on papamo.dev
Papamo sends the webhooks for you, so your computer doesn't need to be on. They cover messages in your papamo.dev inbox, including ones synced from a connected self-hosted archive.
Add a destination in the inbox: Settings → Webhooks → Add webhook. The secret is shown once, so copy it then. You can add up to 10 destinations.
Destinations must be public HTTPS addresses. Papamo refuses private, local and cloud-metadata addresses, and doesn't follow redirects.
Delivery log and replay
Settings → Webhooks → Recent deliveries lists the latest deliveries with their status, attempts and last error. Choose Retry on any delivery that isn't delivered to send it again right away.
API
Use an API key from your papamo.dev inbox against https://na.sms.papamo.dev/free/v1:
| Request | Purpose |
|---|---|
GET /webhooks | Destinations, and whether your plan includes webhooks |
POST /webhooks | Add {"url": "https://…"}. Returns id, url and secret once. |
DELETE /webhooks/{id} | Remove a destination |
GET /webhooks/deliveries | The latest 100 deliveries |
POST /webhooks/deliveries/{id}/replay | Send a delivery again |
Without Plus, adding a destination returns 402.
Webhooks or sync?
Webhooks tell you about each change as it happens. To keep a full copy of the encrypted archive in your own database, use a sync webhook instead: it sends the sealed messages themselves on a schedule.