Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c83ed87

Browse files
committed
feat: add webhook management commands
1 parent d4264bf commit c83ed87

1 file changed

Lines changed: 290 additions & 0 deletions

File tree

cmd/webhook.go

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
/*
2+
Copyright © 2025 Vapi, Inc.
3+
4+
Licensed under the MIT License (the "License");
5+
you may not use this file except in compliance with the License.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13+
THE SOFTWARE.
14+
15+
Authors:
16+
17+
Dan Goosewin <[email protected]>
18+
*/
19+
package cmd
20+
21+
import (
22+
"fmt"
23+
24+
"github.com/AlecAivazis/survey/v2"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
// Manage webhook endpoints and configurations
29+
var webhookCmd = &cobra.Command{
30+
Use: "webhook",
31+
Short: "Manage Vapi webhook endpoints",
32+
Long: `Manage webhook endpoints and configurations for receiving real-time events from Vapi.
33+
34+
Webhooks enable your applications to:
35+
- Receive real-time call events and updates
36+
- Handle function calls from voice assistants
37+
- Get transcript and conversation data
38+
- Monitor system events and errors
39+
- Implement custom business logic triggers`,
40+
}
41+
42+
var listWebhookCmd = &cobra.Command{
43+
Use: "list",
44+
Short: "List all webhook endpoints",
45+
Long: `Display all configured webhook endpoints with their URLs, events, and status.`,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
fmt.Println("🔗 Listing webhook endpoints...")
48+
fmt.Println()
49+
fmt.Println("Webhook endpoints provide real-time event delivery for:")
50+
fmt.Println("- Call events (started, ended, forwarded)")
51+
fmt.Println("- Conversation events (transcript, messages)")
52+
fmt.Println("- Function calls and tool executions")
53+
fmt.Println("- System events and error notifications")
54+
fmt.Println()
55+
fmt.Println("View and manage webhooks via:")
56+
fmt.Println("- Vapi Dashboard: https://dashboard.vapi.ai/webhooks")
57+
fmt.Println("- API Endpoint: GET /webhooks")
58+
fmt.Println()
59+
fmt.Println("For local webhook testing, use: vapi listen --forward-to <your-endpoint>")
60+
61+
return nil
62+
},
63+
}
64+
65+
var getWebhookCmd = &cobra.Command{
66+
Use: "get [webhook-id]",
67+
Short: "Get details of a specific webhook",
68+
Long: `Retrieve the complete configuration of a webhook including URL, events, and delivery settings.`,
69+
Args: cobra.ExactArgs(1),
70+
RunE: func(cmd *cobra.Command, args []string) error {
71+
webhookID := args[0]
72+
73+
fmt.Printf("🔍 Getting webhook details for ID: %s\n", webhookID)
74+
fmt.Println()
75+
fmt.Println("Webhook details include:")
76+
fmt.Println("- Endpoint URL and authentication")
77+
fmt.Println("- Subscribed event types")
78+
fmt.Println("- Delivery settings and retry policies")
79+
fmt.Println("- Success/failure statistics")
80+
fmt.Println("- Recent delivery logs")
81+
fmt.Println()
82+
fmt.Println("View webhook details via:")
83+
fmt.Printf("- Dashboard: https://dashboard.vapi.ai/webhooks/%s\n", webhookID)
84+
fmt.Println("- API: GET /webhooks/{id}")
85+
86+
return nil
87+
},
88+
}
89+
90+
var createWebhookCmd = &cobra.Command{
91+
Use: "create [url]",
92+
Short: "Create a new webhook endpoint",
93+
Long: `Create a new webhook endpoint to receive Vapi events.
94+
95+
If no URL is provided, you'll be prompted to enter it interactively.`,
96+
Args: cobra.MaximumNArgs(1),
97+
RunE: func(cmd *cobra.Command, args []string) error {
98+
var webhookURL string
99+
100+
// Get URL from argument or prompt
101+
if len(args) > 0 {
102+
webhookURL = args[0]
103+
} else {
104+
urlPrompt := &survey.Input{
105+
Message: "Enter webhook URL:",
106+
Help: "Example: https://your-app.com/api/webhooks/vapi",
107+
}
108+
if err := survey.AskOne(urlPrompt, &webhookURL); err != nil {
109+
return fmt.Errorf("webhook creation canceled: %w", err)
110+
}
111+
}
112+
113+
fmt.Printf("🔗 Creating webhook for: %s\n", webhookURL)
114+
fmt.Println()
115+
fmt.Println("Webhook creation involves configuring:")
116+
fmt.Println("- Target endpoint URL and authentication")
117+
fmt.Println("- Event types to subscribe to")
118+
fmt.Println("- Retry policies and timeout settings")
119+
fmt.Println("- Request headers and security")
120+
fmt.Println()
121+
fmt.Println("Common webhook events:")
122+
fmt.Println("- call.started / call.ended")
123+
fmt.Println("- transcript events")
124+
fmt.Println("- function-call events")
125+
fmt.Println("- status-update events")
126+
fmt.Println()
127+
fmt.Println("Create and configure webhooks via:")
128+
fmt.Println("- Vapi Dashboard: https://dashboard.vapi.ai/webhooks")
129+
fmt.Println("- API: POST /webhooks")
130+
fmt.Println()
131+
fmt.Println("💡 Tip: Use 'vapi listen' to test webhook delivery locally")
132+
fmt.Printf(" Example: vapi listen --forward-to %s\n", webhookURL)
133+
134+
return nil
135+
},
136+
}
137+
138+
var updateWebhookCmd = &cobra.Command{
139+
Use: "update [webhook-id]",
140+
Short: "Update an existing webhook",
141+
Long: `Update the configuration of an existing webhook.
142+
143+
This includes modifying the URL, event subscriptions, and delivery settings.`,
144+
Args: cobra.ExactArgs(1),
145+
RunE: func(cmd *cobra.Command, args []string) error {
146+
webhookID := args[0]
147+
148+
fmt.Printf("📝 Update webhook: %s\n", webhookID)
149+
fmt.Println()
150+
fmt.Println("Webhook updates can include:")
151+
fmt.Println("- Endpoint URL changes")
152+
fmt.Println("- Event subscription modifications")
153+
fmt.Println("- Authentication and header updates")
154+
fmt.Println("- Retry policy adjustments")
155+
fmt.Println("- Timeout and delivery settings")
156+
fmt.Println()
157+
fmt.Println("Update via the Vapi dashboard:")
158+
fmt.Printf("https://dashboard.vapi.ai/webhooks/%s\n", webhookID)
159+
fmt.Println()
160+
fmt.Println("Or use the Vapi API: PATCH /webhooks/{id}")
161+
162+
return nil
163+
},
164+
}
165+
166+
var deleteWebhookCmd = &cobra.Command{
167+
Use: "delete [webhook-id]",
168+
Short: "Delete a webhook endpoint",
169+
Long: `Permanently delete a webhook endpoint. This will stop all event deliveries to this endpoint.`,
170+
Args: cobra.ExactArgs(1),
171+
RunE: func(cmd *cobra.Command, args []string) error {
172+
webhookID := args[0]
173+
174+
// Require explicit confirmation for destructive actions
175+
var confirmDelete bool
176+
prompt := &survey.Confirm{
177+
Message: fmt.Sprintf("Are you sure you want to delete webhook %s? This will stop all event deliveries.", webhookID),
178+
Default: false,
179+
}
180+
181+
if err := survey.AskOne(prompt, &confirmDelete); err != nil {
182+
return fmt.Errorf("deletion canceled: %w", err)
183+
}
184+
185+
if !confirmDelete {
186+
fmt.Println("Deletion canceled.")
187+
return nil
188+
}
189+
190+
fmt.Printf("🗑️ Delete webhook: %s\n", webhookID)
191+
fmt.Println()
192+
fmt.Println("Webhook deletion will:")
193+
fmt.Println("- Stop all event deliveries to this endpoint")
194+
fmt.Println("- Remove the webhook configuration")
195+
fmt.Println("- Clear delivery history and logs")
196+
fmt.Println()
197+
fmt.Println("Delete webhook via:")
198+
fmt.Printf("- Dashboard: https://dashboard.vapi.ai/webhooks/%s\n", webhookID)
199+
fmt.Println("- API: DELETE /webhooks/{id}")
200+
201+
return nil
202+
},
203+
}
204+
205+
var testWebhookCmd = &cobra.Command{
206+
Use: "test [webhook-id]",
207+
Short: "Test a webhook endpoint",
208+
Long: `Send a test event to a webhook endpoint to verify it's working correctly.
209+
210+
This helps validate webhook configuration and endpoint availability.`,
211+
Args: cobra.ExactArgs(1),
212+
RunE: func(cmd *cobra.Command, args []string) error {
213+
webhookID := args[0]
214+
215+
fmt.Printf("🧪 Testing webhook: %s\n", webhookID)
216+
fmt.Println()
217+
fmt.Println("Webhook testing verifies:")
218+
fmt.Println("- Endpoint accessibility and response")
219+
fmt.Println("- Authentication and security")
220+
fmt.Println("- Event payload processing")
221+
fmt.Println("- Response handling and status codes")
222+
fmt.Println()
223+
fmt.Println("Test webhooks via:")
224+
fmt.Println("- Vapi Dashboard testing interface")
225+
fmt.Println("- API: POST /webhooks/{id}/test")
226+
fmt.Println()
227+
fmt.Printf("Direct link: https://dashboard.vapi.ai/webhooks/%s?tab=test\n", webhookID)
228+
fmt.Println()
229+
fmt.Println("💡 For local testing, use 'vapi listen' to forward")
230+
fmt.Println(" webhook events to your development environment.")
231+
232+
return nil
233+
},
234+
}
235+
236+
var eventsWebhookCmd = &cobra.Command{
237+
Use: "events",
238+
Short: "List available webhook event types",
239+
Long: `Display all available webhook event types that you can subscribe to.`,
240+
RunE: func(cmd *cobra.Command, args []string) error {
241+
fmt.Println("📡 Available webhook event types...")
242+
fmt.Println()
243+
244+
fmt.Println("📞 Call Events:")
245+
fmt.Println(" • call.started - Call initiation and setup")
246+
fmt.Println(" • call.ended - Call completion and summary")
247+
fmt.Println(" • call.forwarded - Call transfer events")
248+
fmt.Println(" • call.recording-started - Recording begins")
249+
fmt.Println(" • call.recording-stopped - Recording ends")
250+
fmt.Println()
251+
252+
fmt.Println("💬 Conversation Events:")
253+
fmt.Println(" • transcript - Real-time speech-to-text")
254+
fmt.Println(" • message - Assistant and user messages")
255+
fmt.Println(" • status-update - Call status changes")
256+
fmt.Println(" • speech-started - User begins speaking")
257+
fmt.Println(" • speech-ended - User stops speaking")
258+
fmt.Println()
259+
260+
fmt.Println("🔧 Function Events:")
261+
fmt.Println(" • function-call - Tool/function executions")
262+
fmt.Println(" • function-result - Tool execution results")
263+
fmt.Println(" • tool-call-start - Tool call initiation")
264+
fmt.Println(" • tool-call-complete - Tool call completion")
265+
fmt.Println()
266+
267+
fmt.Println("🔔 System Events:")
268+
fmt.Println(" • error - System and call errors")
269+
fmt.Println(" • warning - System warnings")
270+
fmt.Println(" • hang - Call hang-up events")
271+
fmt.Println(" • dtmf - Keypad input events")
272+
fmt.Println()
273+
274+
fmt.Println("Configure events at: https://dashboard.vapi.ai/webhooks")
275+
fmt.Println("Event Documentation: https://docs.vapi.ai/webhooks")
276+
277+
return nil
278+
},
279+
}
280+
281+
func init() {
282+
rootCmd.AddCommand(webhookCmd)
283+
webhookCmd.AddCommand(listWebhookCmd)
284+
webhookCmd.AddCommand(getWebhookCmd)
285+
webhookCmd.AddCommand(createWebhookCmd)
286+
webhookCmd.AddCommand(updateWebhookCmd)
287+
webhookCmd.AddCommand(deleteWebhookCmd)
288+
webhookCmd.AddCommand(testWebhookCmd)
289+
webhookCmd.AddCommand(eventsWebhookCmd)
290+
}

0 commit comments

Comments
 (0)