Skip to content

Commit 799e47c

Browse files
committed
docs: remove example code for verifying webhook signatures
1 parent e939c5d commit 799e47c

1 file changed

Lines changed: 2 additions & 92 deletions

File tree

README.md

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -759,100 +759,10 @@ These authentication parameters can be used in client-side upload forms to secur
759759

760760
The ImageKit SDK provides utilities to verify webhook signatures for secure event handling. This ensures that webhook requests are actually coming from ImageKit and haven't been tampered with.
761761

762-
### Verifying webhook signatures
763-
764-
```go
765-
package main
766-
767-
import (
768-
"fmt"
769-
"io"
770-
"log"
771-
"net/http"
772-
"os"
773-
774-
"github.com/imagekit-developer/imagekit-go/v2"
775-
"github.com/imagekit-developer/imagekit-go/v2/option"
776-
)
777-
778-
func main() {
779-
client := imagekit.NewClient(
780-
option.WithPrivateKey("your_private_key"),
781-
option.WithWebhookSecret("whsec_..."), // Copy from ImageKit dashboard
782-
)
783-
784-
// Webhook handler with proper request body handling
785-
http.HandleFunc("/webhook", func(w http.ResponseWriter, req *http.Request) {
786-
// Limit request body size to prevent abuse (64KB should be sufficient for most webhooks)
787-
const MaxBodyBytes = int64(65536)
788-
req.Body = http.MaxBytesReader(w, req.Body, MaxBodyBytes)
789-
790-
// Read the raw webhook payload
791-
payload, err := io.ReadAll(req.Body)
792-
if err != nil {
793-
fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err)
794-
w.WriteHeader(http.StatusServiceUnavailable)
795-
return
796-
}
797-
798-
// Verify and unwrap webhook payload
799-
event, err := client.Webhooks.Unwrap(payload, req.Header)
800-
if err != nil {
801-
fmt.Fprintf(os.Stderr, "Invalid webhook signature or malformed payload: %v\n", err)
802-
w.WriteHeader(http.StatusUnauthorized)
803-
return
804-
}
805-
806-
fmt.Printf("Verified webhook event: %s\n", event.Type)
807-
808-
// Handle different event types with full type safety
809-
switch event.Type {
810-
case "video.transformation.accepted":
811-
videoEvent := event.AsVideoTransformationAcceptedEvent()
812-
fmt.Printf("Video transformation accepted: %s\n", videoEvent.Data.Asset.URL)
813-
// Debugging: Track transformation requests
814-
// handleVideoTransformationAccepted(videoEvent)
815-
816-
case "video.transformation.ready":
817-
videoEvent := event.AsVideoTransformationReadyEvent()
818-
fmt.Printf("Video transformation ready: %s\n", videoEvent.Data.Transformation.Output.URL)
819-
// Update your database/CMS to show the transformed video
820-
// handleVideoTransformationReady(videoEvent)
821-
822-
case "video.transformation.error":
823-
videoEvent := event.AsVideoTransformationErrorEvent()
824-
fmt.Printf("Video transformation error: %s\n", videoEvent.Data.Transformation.Error.Reason)
825-
// Log error and check your origin/URL endpoint settings
826-
// handleVideoTransformationError(videoEvent)
827-
828-
case "upload.pre-transform.success":
829-
uploadEvent := event.AsUploadPreTransformSuccessEvent()
830-
fmt.Printf("Pre-transform success: %s\n", uploadEvent.Data.FileID)
831-
// File uploaded and pre-transformation completed
832-
// handleUploadPreTransformSuccess(uploadEvent)
833-
834-
case "upload.post-transform.success":
835-
postEvent := event.AsUploadPostTransformSuccessEvent()
836-
fmt.Printf("Post-transform success: %s\n", postEvent.Data.Name)
837-
// Additional transformation completed
838-
// handleUploadPostTransformSuccess(postEvent)
839-
840-
// Handle other event types as needed
841-
default:
842-
fmt.Printf("Unhandled event type: %s\n", event.Type)
843-
}
844-
845-
w.WriteHeader(http.StatusOK)
846-
})
847-
848-
// Start the server
849-
fmt.Println("Webhook server listening on :8080")
850-
log.Fatal(http.ListenAndServe(":8080", nil))
851-
}
852-
```
853-
854762
For detailed information about webhook setup, signature verification, and handling different webhook events, refer to the [ImageKit webhook documentation](https://imagekit.io/docs/webhooks#verify-webhook-signature).
855763

764+
## Advanced Usage
765+
856766
### Errors
857767

858768
When the API returns a non-success status code, we return an error with type

0 commit comments

Comments
 (0)