/stripe-event-types

TypeScript typings for Stripe webhook events

Primary LanguageTypeScriptMIT LicenseMIT

Stripe Event Types

npm Build

This provides TypeScript typings for Stripe webhook events to strongly type the type and data.object fields. These types are automatically generated by scraping Stripe's documentation for types of events.

Why is this needed? The typings included in the stripe library are lacking in this respect. The type for type is a string instead of a string literal and the type for data.object is {} which requires casting each usage of it. This can lead to mistakes in your implementation that could easily be caught with stronger types.

Typed Webhook Event

Installation

Install the package with:

npm install stripe-event-types
# or
yarn add stripe-event-types

Version compatability

Note If you are using stripe version 13.11 or greater, you don't need the stripe-event-types library because the typings have been added to the stripe library.

stripe-event-types version stripe version
3 13.5 - 13.10
2 11
1 10

Usage

Webhook event

When constructing the webhook event, cast it to Stripe.DiscriminatedEvent to strongly type the type and data.object fields:

+/// <reference types="stripe-event-types" />

const event = stripe.webhooks.constructEvent(
  request.body,
  request.headers['stripe-signature'],
  'whsec_test'
-);
+) as Stripe.DiscriminatedEvent;

Event type

The Stripe.DiscriminatedEvent.Type type is a string literal of all event types:

/// <reference types="stripe-event-types" />

const type: Stripe.DiscriminatedEvent.Type = "charge.succeeded";