Skip to content

Add EventNotificationHandler#2498

Merged
xavdid-stripe merged 8 commits intobetafrom
DEVSDK-2822
Dec 16, 2025
Merged

Add EventNotificationHandler#2498
xavdid-stripe merged 8 commits intobetafrom
DEVSDK-2822

Conversation

@xavdid-stripe
Copy link
Copy Markdown
Contributor

@xavdid-stripe xavdid-stripe commented Nov 11, 2025

Why?

We've been designing a streamlined approach to handling incoming events that is easy to get right and hard to get wrong. This PR has the initial implementation of this new system.

NOTE: final names for classes / methods are still being gaveled, so don't worry about those for now. Weigh in on this doc if you have strong feelings there!

The only other pending item is to add a method to allow handling a webhook without verifying the signature. This is good for testing and for Event Bridge, which doesn't use the signature-based verification. Otherwise, this is ready for review.

What?

  • add EventHandler class
  • add event handler constructor on StripeClient
  • add tests
  • add types in like 3 placese
  • tweak eslint config

Example usage

const app = express();
const client = new Stripe(apiKey);
const handler = client.router(
  webhookSecret,
  async (unhandledEvent, client, details) => {
    // something...
  }
);

handler.on('v1.billing.meter.error_report_triggered', async (event) => {
  const meter = await event.fetchRelatedObject();
  console.log(`Billing Meter ${meter.display_name} had a problem`);
});

app.post(
  '/webhook',
  express.raw({type: 'application/json'}),
  async (req, res) => {
    const sig = req.headers['stripe-signature'] ?? '';
    handler.handle(req.body(), sig);
  }
);

app.listen(4242, () => console.log('Running on port 4242'));

See Also

@xavdid-stripe xavdid-stripe changed the base branch from master to beta November 15, 2025 18:12
@xavdid-stripe xavdid-stripe changed the title add "inverted" event handler add event handler class Nov 19, 2025
Comment thread .eslintrc.js
'no-restricted-properties': 'error',
'no-restricted-syntax': 'error',
'no-return-assign': 'error',
'no-return-await': 'error',
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.

Comment thread src/Types.d.ts
data: RequestData,
options: RequestOptions
) => Promise<any>;
parseEventNotification: (
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.

this is internal, it's just used for places in the codebase we call this method

Comment thread types/V2/EventMisc.d.ts Outdated
details: UnhandledNotificationDetails
) => Promise<void>;

class EventRouter {
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.

this type is user-facing

Comment thread types/index.d.ts Outdated
/**
* TODO: docs
*/
router: (
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.

this is user facing on the client

Comment thread src/stripe.core.ts Outdated

return eventNotification;
},
router(webhookSecret: string, onUnhandledHandler: any) {
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.

these types are internal, only used for our runtime code

@xavdid-stripe xavdid-stripe marked this pull request as ready for review November 20, 2025 00:07
@xavdid-stripe xavdid-stripe requested a review from a team as a code owner November 20, 2025 00:07
@xavdid-stripe xavdid-stripe removed the request for review from a team November 21, 2025 00:36
Comment thread src/stripe.core.ts Outdated
Comment thread types/index.d.ts Outdated
@jar-stripe jar-stripe mentioned this pull request Dec 8, 2025
@xavdid-stripe
Copy link
Copy Markdown
Contributor Author

@prathmesh-stripe this is ready for a final review!

@xavdid-stripe xavdid-stripe changed the title add event handler class Add EventNotificationHandler Dec 16, 2025
@xavdid-stripe xavdid-stripe merged commit 66acc4a into beta Dec 16, 2025
15 of 16 checks passed
@xavdid-stripe xavdid-stripe deleted the DEVSDK-2822 branch December 16, 2025 01:55
) => Promise<void>
) {}

// these types are duplicated in the manual types
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What was the reason for the duplicating the manual types for this function?

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.

iirc because these types were ignored and everything in types is what users actually saw. So I wanted to remind myself that any updates had to be made in two places

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants