webhookadmin 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/dist/index.cjs +586 -0
- package/dist/index.d.cts +430 -0
- package/dist/index.d.ts +430 -0
- package/dist/index.js +569 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SHANNON LIMITED LIABILITY COMPANY
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# webhookadmin
|
|
2
|
+
|
|
3
|
+
Node.js / TypeScript SDK for [Webhook Admin](https://webhookadmin.com/docs/).
|
|
4
|
+
|
|
5
|
+
- No dependencies. Uses `fetch` and Web Crypto.
|
|
6
|
+
- Node.js 18+, Bun, Deno, Cloudflare Workers.
|
|
7
|
+
- ESM and CommonJS, with TypeScript types.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install webhookadmin
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Send a message
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { WebhookAdmin } from 'webhookadmin';
|
|
19
|
+
|
|
20
|
+
const wha = new WebhookAdmin('sk_live_...'); // or set WEBHOOK_ADMIN_API_KEY
|
|
21
|
+
|
|
22
|
+
const { id, deliveries } = await wha.messages.send(
|
|
23
|
+
{ consumer: 'cus_123', event_type: 'invoice.paid', payload: { invoice_id: 'inv_1', amount: 1200 } },
|
|
24
|
+
{ idempotencyKey: 'invoice-inv_1-paid' },
|
|
25
|
+
);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`consumer` is your customer's ID (`external_id`). The consumer is created if it does not exist.
|
|
29
|
+
|
|
30
|
+
## Verify incoming webhooks
|
|
31
|
+
|
|
32
|
+
Pass the raw request body, not a parsed object.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { Webhook } from 'webhookadmin';
|
|
36
|
+
|
|
37
|
+
const wh = new Webhook(process.env.WEBHOOK_SECRET!); // whsec_...
|
|
38
|
+
const event = await wh.verify(rawBody, headers); // { type, timestamp, data }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`verify` throws `WebhookVerificationError` when the signature does not match or the timestamp is more than 5 minutes off (`new Webhook(secret, { tolerance: 300 })`). While a secret is being rotated, messages carry two signatures and either secret verifies.
|
|
42
|
+
|
|
43
|
+
Express:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
app.post('/webhooks', express.raw({ type: 'application/json' }), async (req, res) => {
|
|
47
|
+
try {
|
|
48
|
+
const event = await wh.verify(req.body, req.headers);
|
|
49
|
+
// handle event
|
|
50
|
+
res.sendStatus(204);
|
|
51
|
+
} catch {
|
|
52
|
+
res.sendStatus(400);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Hono:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
app.post('/webhooks', async (c) => {
|
|
61
|
+
const event = await wh.verify(await c.req.text(), c.req.raw.headers);
|
|
62
|
+
return c.body(null, 204);
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Next.js (App Router, `app/webhooks/route.ts`):
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
export async function POST(req: Request) {
|
|
70
|
+
const event = await wh.verify(await req.text(), req.headers);
|
|
71
|
+
return new Response(null, { status: 204 });
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API
|
|
76
|
+
|
|
77
|
+
| Method | Endpoint |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `messages.send(params, { idempotencyKey? })` | `POST /v1/messages` |
|
|
80
|
+
| `messages.list({ status?, event_type?, q?, limit?, cursor? })` | `GET /v1/messages` |
|
|
81
|
+
| `messages.get(id)` | `GET /v1/messages/:id` |
|
|
82
|
+
| `deliveries.retry(id)` | `POST /v1/deliveries/:id/retry` |
|
|
83
|
+
| `consumers.create({ external_id, name? })` | `POST /v1/consumers` |
|
|
84
|
+
| `consumers.list({ limit?, cursor? })` | `GET /v1/consumers` |
|
|
85
|
+
| `endpoints.create({ consumer_id, url, event_types?, fixed_ip?, description? })` | `POST /v1/endpoints` |
|
|
86
|
+
| `endpoints.list({ consumer_id? })` | `GET /v1/endpoints` |
|
|
87
|
+
| `endpoints.get(id)` | `GET /v1/endpoints/:id` |
|
|
88
|
+
| `endpoints.update(id, { url?, event_types?, status?, description? })` | `PATCH /v1/endpoints/:id` |
|
|
89
|
+
| `endpoints.delete(id)` | `DELETE /v1/endpoints/:id` |
|
|
90
|
+
| `endpoints.rotateSecret(id)` | `POST /v1/endpoints/:id/rotate-secret` |
|
|
91
|
+
| `endpoints.sendTest(id, { event_type? })` | `POST /v1/endpoints/:id/test` |
|
|
92
|
+
| `portal.createLink(consumerId, { frame_origin?, locale? })` | `POST /v1/consumers/:id/portal` |
|
|
93
|
+
|
|
94
|
+
Every method also takes request options as the last argument: `{ timeout?, maxRetries?, signal? }`.
|
|
95
|
+
|
|
96
|
+
### Pagination
|
|
97
|
+
|
|
98
|
+
`list()` returns the first page when awaited, and every item across pages with `for await`.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const page = await wha.messages.list({ status: 'failed' }); // { items, next_cursor }
|
|
102
|
+
|
|
103
|
+
for await (const message of wha.messages.list({ status: 'failed' })) {
|
|
104
|
+
console.log(message.id);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Errors
|
|
109
|
+
|
|
110
|
+
| Class | Status |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `AuthenticationError` | 401 |
|
|
113
|
+
| `PermissionError` | 403 |
|
|
114
|
+
| `NotFoundError` | 404 |
|
|
115
|
+
| `ValidationError` | 400, 413, 422 |
|
|
116
|
+
| `PlanLimitError` | 402 |
|
|
117
|
+
| `ConflictError` | 409 |
|
|
118
|
+
| `RateLimitError` | 429 |
|
|
119
|
+
| `ApiError` | other, including 5xx |
|
|
120
|
+
| `ConnectionError` | no response |
|
|
121
|
+
| `TimeoutError` | no response within `timeout` (subclass of `ConnectionError`) |
|
|
122
|
+
|
|
123
|
+
All extend `WebhookAdminError`, which has `status`, `code`, `message`, `fields` and `requestId`.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { ValidationError } from 'webhookadmin';
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
await wha.endpoints.create({ consumer_id: 'con_...', url: 'http://example.com' });
|
|
130
|
+
} catch (e) {
|
|
131
|
+
if (e instanceof ValidationError) console.log(e.fields); // { url: '...' }
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Retries
|
|
136
|
+
|
|
137
|
+
| Response | Retried |
|
|
138
|
+
|---|---|
|
|
139
|
+
| 429 | Always. Waits for `retry-after`, or until `x-ratelimit-reset`. |
|
|
140
|
+
| 5xx, network error, timeout | Only requests that are safe to repeat: `GET`, `PATCH`, `DELETE`, `messages.send`, `portal.createLink` |
|
|
141
|
+
| Other 4xx | Never |
|
|
142
|
+
|
|
143
|
+
Backoff starts at 0.5 s, doubles up to 8 s, with ±25% jitter. `messages.send` generates an `Idempotency-Key` when you omit it and sends the same key on every retry, so a retried send is delivered once. Keys are kept for 24 hours.
|
|
144
|
+
|
|
145
|
+
## Configuration
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const wha = new WebhookAdmin('sk_live_...', {
|
|
149
|
+
baseUrl: 'https://api.webhookadmin.com', // default
|
|
150
|
+
timeout: 30_000, // ms per attempt, default 30000
|
|
151
|
+
maxRetries: 2, // default 2
|
|
152
|
+
fetch: customFetch, // default globalThis.fetch
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
| Environment variable | Used when |
|
|
157
|
+
|---|---|
|
|
158
|
+
| `WEBHOOK_ADMIN_API_KEY` | the API key argument is omitted |
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
MIT
|