Skip to content

Add SendGrid webhook event models and signature verification#29

Open
thoven87 wants to merge 16 commits into
vapor-community:mainfrom
thoven87:main
Open

Add SendGrid webhook event models and signature verification#29
thoven87 wants to merge 16 commits into
vapor-community:mainfrom
thoven87:main

Conversation

@thoven87
Copy link
Copy Markdown
Contributor

Introduces comprehensive models for SendGrid webhook event types, including delivery, engagement, and account status change events. Adds ECDSA signature verification for webhook security, updates Package.swift to include swift-crypto, and provides extensive documentation and tests for event parsing and verification.

Closes #28

Introduces comprehensive models for SendGrid webhook event types, including delivery, engagement, and account status change events. Adds ECDSA signature verification for webhook security, updates Package.swift to include swift-crypto, and provides extensive documentation and tests for event parsing and verification.
@thoven87 thoven87 mentioned this pull request Oct 22, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 22, 2025

Codecov Report

❌ Patch coverage is 43.99261% with 303 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.63%. Comparing base (cae5b09) to head (89a4830).

Files with missing lines Patch % Lines
Sources/SendGridKit/SendGridWebhookClient.swift 16.50% 172 Missing ⚠️
Sources/SendGridKit/Models/WebhookEvent.swift 62.09% 94 Missing ⚠️
...es/SendGridKit/Webhook+SignatureVerification.swift 66.07% 19 Missing ⚠️
...ources/SendGridKit/Models/WebhookEvent+Input.swift 25.00% 18 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main      #29       +/-   ##
===========================================
- Coverage   86.86%   57.63%   -29.23%     
===========================================
  Files          12       16        +4     
  Lines         274      786      +512     
===========================================
+ Hits          238      453      +215     
- Misses         36      333      +297     
Files with missing lines Coverage Δ
Sources/SendGridKit/SendGridClient.swift 100.00% <100.00%> (ø)
...ources/SendGridKit/Models/WebhookEvent+Input.swift 25.00% <25.00%> (ø)
...es/SendGridKit/Webhook+SignatureVerification.swift 66.07% <66.07%> (ø)
Sources/SendGridKit/Models/WebhookEvent.swift 62.09% <62.09%> (ø)
Sources/SendGridKit/SendGridWebhookClient.swift 16.50% <16.50%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fpseverino
Copy link
Copy Markdown
Member

So this basically only adds structs that a Vapor/Hummingbird server should decode and methods to verify their signature, right?

@thoven87
Copy link
Copy Markdown
Contributor Author

So this basically only adds structs that a Vapor/Hummingbird server should decode and methods to verify their signature, right?

I just noticed that I am missing the received case. I will fix this shortly along with the CRUD operations https://www.twilio.com/docs/sendgrid/api-reference/webhooks/create-an-event-webhook

Introduces methods for managing SendGrid Event Webhooks (create, test, retrieve, update, delete, signature verification, public key retrieval) and Parse Webhook settings/statistics in SendGridClient. Also updates SendGridError to include url and id fields for improved error context, and fixes the base API URL to support new endpoints.
Introduces SendGridReceivedEvent to handle inbound email 'received' events, updates SendGridWebhookEvent and related enums for the new event type, and adds models for event webhook input, settings, signature verification, and parse webhook statistics. Also adds sgContentType to engagement events and improves documentation for uniqueArgs.
Added unit tests to verify decoding of SendGrid 'received' webhook events, including support for both Unix and ISO 8601 timestamps, additional fields, and v3_payload_details. Also updated existing test data to use generic test values for consistency.
Updated several public method signatures in SendGridClient to use multiline formatting for parameters, improving readability and consistency. Also adjusted body encoding and decoding calls to multiline style.
Changed the expected sgEventId value in the delivered event decoding test to 'test_delivered_event_id' for consistency or improved test clarity.
@thoven87
Copy link
Copy Markdown
Contributor Author

So this basically only adds structs that a Vapor/Hummingbird server should decode and methods to verify their signature, right?

@fpseverino all the failing tests are related to the email validation API

Copy link
Copy Markdown
Member

@fpseverino fpseverino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very well designed API, great work!
You tested it with real SendGrid API keys, right?

Comment thread README.md Outdated
Comment thread README.md
Comment thread Sources/SendGridKit/Models/WebhookEvent.swift
Comment thread Sources/SendGridKit/Models/WebhookEvent.swift Outdated
Comment thread Sources/SendGridKit/Models/WebhookEvent.swift Outdated
Comment thread Sources/SendGridKit/Models/WebhookEvent.swift Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to split this file into smaller ones, you should create a subfolder (inside the "Models" one) called "Webhooks" or something like that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to split this file into smaller ones, you should create a subfolder (inside the "Models" one) called "Webhooks" or something like that

I don't know how much smaller you want. I split the file into two categories, Input and Response

Comment thread Sources/SendGridKit/Webhook+SignatureVerification.swift Outdated
Comment thread Sources/SendGridKit/Webhook+SignatureVerification.swift Outdated
Comment thread Sources/SendGridKit/SendGridClient.swift Outdated
@thoven87
Copy link
Copy Markdown
Contributor Author

A very well designed API, great work!

You tested it with real SendGrid API keys, right?

Yes!

thoven87 and others added 10 commits October 27, 2025 21:17
Co-authored-by: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com>
Co-authored-by: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com>
Co-authored-by: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com>
Co-authored-by: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com>
Co-authored-by: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com>
Moved all Event Webhook-related API methods from SendGridClient to a new SendGridWebhookClient for better separation of concerns. Added public initializers to model structs for easier instantiation. Updated tests to use the new SendGridWebhookClient.
Standardizes property names in SendGrid event models by renaming fields such as asmGroupId to asmGroupID, sgEventId to sgEventID, and similar fields to consistently use 'ID' suffix capitalization. Updates all related CodingKeys, initializers, encoders/decoders, and test cases to match the new naming convention for improved clarity and Swift API guidelines compliance.
Extracted EventWebhookInput and SendGridTestWebhookInput structs from WebhookEvent.swift into a new file WebhookEvent+Input.swift for better code organization and separation of concerns.
Introduces detailed .docc documentation for SendGridKit webhook types, including EventWebhookInput, SendGridTestWebhookInput, SendGridWebhookClient, SendGridWebhookEvent, and signature verification. Updates the main documentation index to reference new webhook types and corrects a minor typo in SendGridWebhookClient's initializer comment. Also updates the README to use Vapor for webhook examples and clarifies body handling.
@thoven87 thoven87 requested a review from fpseverino December 13, 2025 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Webhook

2 participants