convex-vapi 0.0.1

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.
Files changed (52) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +437 -0
  3. package/dist/client/_generated/_ignore.d.ts +1 -0
  4. package/dist/client/_generated/_ignore.d.ts.map +1 -0
  5. package/dist/client/_generated/_ignore.js +3 -0
  6. package/dist/client/_generated/_ignore.js.map +1 -0
  7. package/dist/client/index.d.ts +115 -0
  8. package/dist/client/index.d.ts.map +1 -0
  9. package/dist/client/index.js +172 -0
  10. package/dist/client/index.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +34 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +125 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +4 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/lib.d.ts +101 -0
  32. package/dist/component/lib.d.ts.map +1 -0
  33. package/dist/component/lib.js +177 -0
  34. package/dist/component/lib.js.map +1 -0
  35. package/dist/component/schema.d.ts +53 -0
  36. package/dist/component/schema.d.ts.map +1 -0
  37. package/dist/component/schema.js +30 -0
  38. package/dist/component/schema.js.map +1 -0
  39. package/package.json +105 -0
  40. package/src/client/_generated/_ignore.ts +1 -0
  41. package/src/client/index.ts +250 -0
  42. package/src/client/setup.test.ts +26 -0
  43. package/src/component/_generated/api.ts +50 -0
  44. package/src/component/_generated/component.ts +159 -0
  45. package/src/component/_generated/dataModel.ts +60 -0
  46. package/src/component/_generated/server.ts +169 -0
  47. package/src/component/convex.config.ts +5 -0
  48. package/src/component/lib.test.ts +167 -0
  49. package/src/component/lib.ts +193 -0
  50. package/src/component/schema.ts +31 -0
  51. package/src/component/setup.test.ts +11 -0
  52. package/src/test.ts +18 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,437 @@
1
+ # convex-vapi
2
+
3
+ Sync Vapi voice AI calls into your Convex database reactively, and place
4
+ outbound calls directly from Convex functions.
5
+
6
+ [![npm version](https://img.shields.io/npm/v/convex-vapi)](https://www.npmjs.com/package/convex-vapi)
7
+ [![Convex Component](https://www.convex.dev/components/badge/sholajegede/convex-vapi)](https://www.convex.dev/components/sholajegede/convex-vapi)
8
+ [![npm downloads](https://img.shields.io/npm/dw/convex-vapi)](https://www.npmjs.com/package/convex-vapi)
9
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](./LICENSE)
10
+
11
+ ```ts
12
+ const vapi = new Vapi(components.convexVapi, {
13
+ apiKey: process.env.VAPI_API_KEY!,
14
+ webhookSecret: process.env.VAPI_WEBHOOK_SECRET!,
15
+ });
16
+
17
+ // Place a call and get the result back immediately — no waiting on the webhook
18
+ const { callId } = await vapi.createCall(ctx, {
19
+ assistantId,
20
+ phoneNumberId,
21
+ customerNumber,
22
+ });
23
+
24
+ // Stays live from here — status, transcript, summary, and cost all update
25
+ // reactively as Vapi's webhooks arrive, including from a call ended outside
26
+ // your app, like one you hung up from Vapi's own dashboard
27
+ const call = useQuery(api.example.getCall, { callId });
28
+ ```
29
+
30
+ <!-- START: Include on https://convex.dev/components -->
31
+
32
+ ## What this does
33
+
34
+ `convex-vapi` gives your Convex app a live, queryable record of Vapi voice AI
35
+ calls, kept up to date by Vapi's server-URL webhooks, plus a small set of
36
+ actions for placing and inspecting calls:
37
+
38
+ - **Reactive call tracking** — every `status-update` and `end-of-call-report`
39
+ webhook event updates a Convex row, so `useQuery` in your React app re-renders
40
+ as a call rings, connects, and ends, complete with transcript, summary, and
41
+ recording once available.
42
+ - **Place outbound calls** — call `createCall` from a Convex action to start a
43
+ phone call through a Vapi assistant.
44
+ - **Non-destructive merges** — a sparser event (e.g. a mid-call status update)
45
+ never overwrites richer data an earlier event already recorded (e.g. a
46
+ transcript from the final report).
47
+ - **Secret-verified webhooks** — every inbound webhook is checked against Vapi's
48
+ `X-Vapi-Secret` header with a constant-time comparison before anything is
49
+ written.
50
+
51
+ This is a [Convex component](https://convex.dev/components): its `calls` and
52
+ `webhookEvents` tables live in an isolated schema, not your app's schema, and
53
+ are only reachable through the functions this component exposes.
54
+
55
+ ## Table of Contents
56
+
57
+ - [Install](#install)
58
+ - [Quick Start](#quick-start)
59
+ - [Setup](#setup)
60
+ - [Usage](#usage)
61
+ - [API Reference](#api-reference)
62
+ - [Type Reference](#type-reference)
63
+ - [Webhook Events](#webhook-events)
64
+ - [Database Schema](#database-schema)
65
+ - [Authentication](#authentication)
66
+ - [Example App](#example-app)
67
+ - [Testing](#testing)
68
+ - [Limitations](#limitations)
69
+ - [Troubleshooting](#troubleshooting)
70
+ - [Contributing](#contributing)
71
+ - [Changelog](#changelog)
72
+
73
+ ## Install
74
+
75
+ ```sh
76
+ npm install convex-vapi
77
+ ```
78
+
79
+ ## Quick Start
80
+
81
+ ### 1. Add the component
82
+
83
+ ```ts
84
+ // convex/convex.config.ts
85
+ import { defineApp } from "convex/server";
86
+ import convexVapi from "convex-vapi/convex.config";
87
+
88
+ const app = defineApp();
89
+ app.use(convexVapi);
90
+
91
+ export default app;
92
+ ```
93
+
94
+ ### 2. Set environment variables
95
+
96
+ ```sh
97
+ npx convex env set VAPI_API_KEY your-private-api-key
98
+ npx convex env set VAPI_WEBHOOK_SECRET whsec_...
99
+ ```
100
+
101
+ `VAPI_API_KEY` is your private key from the Vapi dashboard (Settings → API
102
+ Keys). `VAPI_WEBHOOK_SECRET` is a secret string you choose yourself — you'll
103
+ configure the same value on the assistant in step 4.
104
+
105
+ ### 3. Mount the webhook handler
106
+
107
+ ```ts
108
+ // convex/http.ts
109
+ import { httpRouter } from "convex/server";
110
+ import { components } from "./_generated/api";
111
+ import { Vapi } from "convex-vapi";
112
+
113
+ const vapi = new Vapi(components.convexVapi, {
114
+ apiKey: process.env.VAPI_API_KEY!,
115
+ webhookSecret: process.env.VAPI_WEBHOOK_SECRET!,
116
+ });
117
+
118
+ const http = httpRouter();
119
+
120
+ http.route({
121
+ path: "/webhooks/vapi",
122
+ method: "POST",
123
+ handler: vapi.webhookHandler,
124
+ });
125
+
126
+ export default http;
127
+ ```
128
+
129
+ ### 4. Configure the webhook on your assistant
130
+
131
+ Set the assistant's `serverUrl` to
132
+ `https://<your-deployment>.convex.site/webhooks/vapi` and its `server.secret` to
133
+ the same value as `VAPI_WEBHOOK_SECRET`, either in the Vapi dashboard or via the
134
+ API:
135
+
136
+ ```sh
137
+ curl -X PATCH https://api.vapi.ai/assistant/<assistant-id> \
138
+ -H "Authorization: Bearer $VAPI_API_KEY" \
139
+ -H "Content-Type: application/json" \
140
+ -d '{"serverUrl": "https://<your-deployment>.convex.site/webhooks/vapi", "server": {"secret": "whsec_..."}}'
141
+ ```
142
+
143
+ ### 5. Initialize the client
144
+
145
+ ```ts
146
+ // convex/example.ts
147
+ import { action, query } from "./_generated/server";
148
+ import { components } from "./_generated/api";
149
+ import { Vapi } from "convex-vapi";
150
+ import { v } from "convex/values";
151
+
152
+ const vapi = new Vapi(components.convexVapi, {
153
+ apiKey: process.env.VAPI_API_KEY!,
154
+ webhookSecret: process.env.VAPI_WEBHOOK_SECRET!,
155
+ });
156
+
157
+ export const listCallsByAssistant = query({
158
+ args: { assistantId: v.string() },
159
+ handler: async (ctx, args) => {
160
+ return await vapi.listCallsByAssistant(ctx, args);
161
+ },
162
+ });
163
+ ```
164
+
165
+ ## Setup
166
+
167
+ The component needs no schema changes in your app — its tables (`calls`,
168
+ `webhookEvents`) live entirely inside the component's own isolated schema. All
169
+ you need is the webhook mounted (step 3) and secret configured (step 4), plus a
170
+ `Vapi` client instance wherever you call its methods.
171
+
172
+ Assistants and phone numbers are managed entirely in the Vapi dashboard or API —
173
+ this component tracks calls, not the assistants that make them.
174
+
175
+ ## Usage
176
+
177
+ ### Place an outbound call
178
+
179
+ ```ts
180
+ export const dial = action({
181
+ args: {
182
+ assistantId: v.string(),
183
+ phoneNumberId: v.string(),
184
+ customerNumber: v.string(),
185
+ },
186
+ handler: async (ctx, args) => {
187
+ return await vapi.createCall(ctx, args);
188
+ },
189
+ });
190
+ ```
191
+
192
+ Returns `{ callId, status }` and immediately records the call in Convex — you
193
+ don't have to wait for the first webhook to see it in a query.
194
+
195
+ ### Refresh a call on demand
196
+
197
+ ```ts
198
+ export const sync = action({
199
+ args: { callId: v.string() },
200
+ handler: async (ctx, args) => {
201
+ await vapi.refreshCall(ctx, args);
202
+ return null;
203
+ },
204
+ });
205
+ ```
206
+
207
+ Fetches the call directly from the Vapi API and re-records it — useful as a
208
+ fallback if a webhook delivery was missed, or to pull in the
209
+ transcript/recording immediately after a call ends without waiting on the
210
+ webhook queue.
211
+
212
+ ### Read calls reactively
213
+
214
+ ```tsx
215
+ const calls = useQuery(api.example.listCallsByAssistant, {
216
+ assistantId: "assistant_...",
217
+ });
218
+ ```
219
+
220
+ Every `status-update` and `end-of-call-report` webhook event patches a row, so
221
+ this query re-renders live as a call progresses from `queued` through `ringing`,
222
+ `in-progress`, and `ended` — no polling.
223
+
224
+ ## API Reference
225
+
226
+ ### Actions (need `ctx` from an action)
227
+
228
+ | Method | Description |
229
+ | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
230
+ | `createCall(ctx, { assistantId, phoneNumberId, customerNumber })` | Places an outbound call via the REST API and records it. Returns `{ callId, status }`. |
231
+ | `refreshCall(ctx, { callId })` | Re-fetches a call from the Vapi API and re-records its current state, including transcript/summary/recording if the call has ended. |
232
+
233
+ ### Queries (work from actions, queries, or mutations)
234
+
235
+ | Method | Description |
236
+ | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
237
+ | `getCall(ctx, { callId })` | Fetch one call by its Vapi call id. |
238
+ | `listCallsByAssistant(ctx, { assistantId, limit? })` | Most recently updated calls for an assistant, newest first. |
239
+ | `getStats(ctx)` | Aggregate counts across every call and webhook event this deployment has recorded: `{ callCount, endedCount, liveCount, webhookEventCount }`. |
240
+ | `listRecentCalls(ctx, { limit? })` | Most recently updated calls across every assistant, newest first. |
241
+ | `listRecentWebhookEvents(ctx, { limit? })` | Most recent webhook deliveries across every call, newest first. |
242
+
243
+ ### Webhook
244
+
245
+ | Property | Description |
246
+ | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
247
+ | `webhookHandler` | An `httpAction` that verifies, deduplicates, and processes `status-update` and `end-of-call-report` webhook deliveries. Mount it at any route. |
248
+
249
+ ## Type Reference
250
+
251
+ ```ts
252
+ type VapiOptions = {
253
+ apiKey: string; // private API key from the Vapi dashboard
254
+ webhookSecret: string; // matches the assistant's server.secret
255
+ };
256
+
257
+ type CreateCallArgs = {
258
+ assistantId: string;
259
+ phoneNumberId: string;
260
+ customerNumber: string; // E.164 format, e.g. "+11231231234"
261
+ };
262
+
263
+ type Call = {
264
+ callId: string;
265
+ assistantId?: string;
266
+ phoneNumberId?: string;
267
+ customerNumber?: string;
268
+ status: string; // "queued" | "ringing" | "in-progress" | "forwarding" | "ended" | ...
269
+ endedReason?: string;
270
+ transcript?: string;
271
+ summary?: string;
272
+ recordingUrl?: string;
273
+ cost?: number;
274
+ startedAt?: number;
275
+ endedAt?: number;
276
+ createdAt: number;
277
+ updatedAt: number;
278
+ };
279
+ ```
280
+
281
+ ## Webhook Events
282
+
283
+ The webhook handler processes two of Vapi's server message types (all others are
284
+ recorded for idempotency but otherwise ignored — see Limitations):
285
+
286
+ - **`status-update`** — updates the call's `status` field (`queued`, `ringing`,
287
+ `in-progress`, `forwarding`, `ended`, ...) as the call progresses.
288
+ - **`end-of-call-report`** — the final summary sent once a call ends; records
289
+ `endedReason`, `transcript`, `summary`, `recordingUrl`, and `cost`.
290
+
291
+ Every request is checked against the `X-Vapi-Secret` header with a constant-time
292
+ comparison against your configured `webhookSecret` before anything is written.
293
+ Vapi does not include a per-delivery id in its webhook headers, so duplicate
294
+ detection here hashes the raw request body with SHA-256 — an identical payload
295
+ delivered twice is treated as a duplicate and skipped.
296
+
297
+ Calls are recorded with a merge, not a blind overwrite: fields missing from the
298
+ current event (e.g. `transcript` on a mid-call `status-update`) fall back to
299
+ whatever was already stored, so a later, sparser event can never blank out data
300
+ an earlier, richer one recorded.
301
+
302
+ ## Database Schema
303
+
304
+ ```ts
305
+ calls: {
306
+ callId: string; // indexed: by_callId
307
+ assistantId?: string; // indexed: by_assistantId
308
+ phoneNumberId?: string;
309
+ customerNumber?: string;
310
+ status: string;
311
+ endedReason?: string;
312
+ transcript?: string;
313
+ summary?: string;
314
+ recordingUrl?: string;
315
+ cost?: number;
316
+ startedAt?: number;
317
+ endedAt?: number;
318
+ createdAt: number;
319
+ updatedAt: number;
320
+ }
321
+
322
+ webhookEvents: {
323
+ eventId: string; // indexed: by_eventId — SHA-256 hex of the raw payload
324
+ eventType: string; // message.type
325
+ callId?: string;
326
+ payload: string; // raw JSON body, for auditing/replay
327
+ receivedAt: number;
328
+ }
329
+ ```
330
+
331
+ This schema lives entirely inside the component's isolated namespace — it will
332
+ never collide with tables in your app's own `convex/schema.ts`.
333
+
334
+ ## Authentication
335
+
336
+ Vapi supports several ways to authenticate outgoing webhook requests. This
337
+ component implements the original and simplest one: a shared secret you set on
338
+ the assistant's `server.secret` field, which Vapi echoes back verbatim in the
339
+ `X-Vapi-Secret` header on every request — verified here with a constant-time
340
+ string comparison. Vapi's newer configurable-HMAC system (a separate Custom
341
+ Credential resource with a selectable algorithm and signature header) is not
342
+ implemented, since it requires provisioning that Credential outside of the
343
+ assistant's own configuration; if you need it, verify the signature yourself
344
+ before the request reaches this component's `webhookHandler`.
345
+
346
+ ## Example App
347
+
348
+ The `example/` app is a full interactive demo, not just a form:
349
+
350
+ - **Calls** — place a call (with a live-updating list for that assistant,
351
+ reactively reflected the instant the action call returns — no waiting on the
352
+ webhook), and a stats bar showing total/live/ended calls plus webhook
353
+ deliveries, visible from every tab.
354
+ - **Webhooks** — a live, expandable feed of every raw delivery to
355
+ `/webhooks/vapi`, so you can watch signature verification and deduplication
356
+ happen in real time.
357
+ - **History** — every call this component has ever recorded, newest first,
358
+ across every assistant, with status, cost, duration, and an expandable
359
+ transcript.
360
+ - A sidebar **Activity** console logging every action call this demo makes, with
361
+ its result or error.
362
+
363
+ Run it from the repo root (not `example/`):
364
+
365
+ ```sh
366
+ npm install --legacy-peer-deps
367
+ npx convex env set VAPI_API_KEY your-private-api-key
368
+ npx convex env set VAPI_WEBHOOK_SECRET whsec_...
369
+ npm run dev
370
+ ```
371
+
372
+ Then point a test assistant's `serverUrl` at
373
+ `https://<your-dev-deployment>.convex.site/webhooks/vapi` (see
374
+ [Quick Start](#quick-start)) to see live deliveries land in the Webhooks tab.
375
+
376
+ ## Testing
377
+
378
+ ```sh
379
+ npm run test
380
+ npm run typecheck
381
+ ```
382
+
383
+ Tests use [`convex-test`](https://www.npmjs.com/package/convex-test) and cover
384
+ `recordCall`'s merge-on-upsert behavior (confirming a sparser later event never
385
+ blanks a richer earlier one, and that a late, out-of-order `status-update` can
386
+ never regress a call back out of `ended`), `listCallsByAssistant` scoping by
387
+ assistant, webhook idempotency via `checkAndRecordEvent`, and the dashboard
388
+ queries (`getStats`, `listRecentCalls`, `listRecentWebhookEvents`) used by the
389
+ example app.
390
+
391
+ ## Limitations
392
+
393
+ - Only `status-update` and `end-of-call-report` message types update the `calls`
394
+ table. `transcript` (partial live captions), `conversation-update`,
395
+ `speech-update`, `tool-calls`, and other real-time message types are accepted
396
+ (and recorded in `webhookEvents` for auditing) but not persisted to `calls` —
397
+ storing every partial transcript chunk would be high-volume and is better
398
+ handled with your own streaming UI if you need live captions.
399
+ - Assistant and phone number management (creating, updating, or listing
400
+ assistants) is out of scope — this component tracks calls, not the resources
401
+ that place them.
402
+ - Live in-call control (e.g. programmatically ending an active call, or
403
+ transferring it) is not implemented; only `createCall` (start) and
404
+ `refreshCall` (poll) are provided.
405
+ - Rate limits are Vapi's own — this component does not implement its own rate
406
+ limiting or backoff.
407
+
408
+ ## Troubleshooting
409
+
410
+ **Webhook returns 401** — the `X-Vapi-Secret` header didn't match. Confirm the
411
+ assistant's `server.secret` field is set to exactly the same value as
412
+ `VAPI_WEBHOOK_SECRET` (a `PATCH /assistant/{id}` call, or the dashboard's
413
+ assistant settings, both work).
414
+
415
+ **Webhook returns 400 "Missing X-Vapi-Secret header"** — the assistant's
416
+ `serverUrl` is set but `server.secret` isn't configured, so Vapi isn't sending
417
+ the header at all.
418
+
419
+ **Calls never appear in queries** — confirm the assistant's `serverUrl` points
420
+ at your deployment's `.convex.site` domain (not `.convex.cloud`), and that it's
421
+ reachable publicly (Convex HTTP actions are public by default, so this is
422
+ usually a typo in the URL).
423
+
424
+ **Transcript/summary are missing after a call ends** — these only arrive with
425
+ the `end-of-call-report` message, sent shortly after the call ends; call
426
+ `refreshCall` if you need them sooner than the webhook arrives, or if a delivery
427
+ was dropped.
428
+
429
+ ## Contributing
430
+
431
+ See [CONTRIBUTING.md](./CONTRIBUTING.md).
432
+
433
+ ## Changelog
434
+
435
+ See [CHANGELOG.md](./CHANGELOG.md).
436
+
437
+ <!-- END: Include on https://convex.dev/components -->
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=_ignore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_ignore.d.ts","sourceRoot":"","sources":["../../../src/client/_generated/_ignore.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // This is only here so convex-test can detect a _generated folder
3
+ //# sourceMappingURL=_ignore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_ignore.js","sourceRoot":"","sources":["../../../src/client/_generated/_ignore.ts"],"names":[],"mappings":";AAAA,kEAAkE"}