Webhook.ts overview
WorkOS webhooks: the WorkOS-Signature header (t=<ms>,v1=<hex>, an HMAC-SHA256 of <t>.<raw body> under the endpoint’s secret) is checked in constant time and within a tolerance before the body is parsed. Verify the raw body as received: a body parsed and re-serialized no longer matches.
Since v0.1.0
Exports Grouped by Category
constants
DEFAULT_TOLERANCE_MS
How far a signature’s timestamp may be from now, in milliseconds (the WorkOS SDK’s default).
Signature
declare const DEFAULT_TOLERANCE_MS: 180000
Since v0.1.0
SIGNATURE_HEADER
The header WorkOS signs a webhook in.
Signature
declare const SIGNATURE_HEADER: "workos-signature"
Since v0.1.0
errors
WebhookVerificationError (class)
A webhook that failed verification. reason is for the server log; answer the request 400 whatever it is.
Signature
declare class WebhookVerificationError
Since v0.1.0
models
EventHandlers (interface)
What a verified event does here. session.revoked records the session as revoked, so its sealed cookie stops validating before its access token expires; onUserDeleted runs for user.deleted (the app removes what it keeps for the user). Every event is handed to onEvent.
Signature
export interface EventHandlers {
readonly onUserDeleted?: (userId: string) => Effect.Effect<void, Auth.Errors.AuthError>
readonly onEvent?: (event: WorkOSEvent) => Effect.Effect<void, Auth.Errors.AuthError>
}
Since v0.1.0
WorkOSEvent (type alias)
A verified WorkOS event. The events this package acts on are decoded (known: true); every other event, or one whose data did not decode, keeps its data as it came (known: false).
Signature
type WorkOSEvent =
| {
readonly id: string
readonly event: "session.created" | "session.revoked"
readonly data: WireSession
readonly createdAt: string
readonly known: true
}
| {
readonly id: string
readonly event: "user.created" | "user.updated" | "user.deleted"
readonly data: WireUser
readonly createdAt: string
readonly known: true
}
| {
readonly id: string
readonly event: string
readonly data: unknown
readonly createdAt: string
readonly known: false
}
Since v0.1.0
services
WorkOSEvents (class)
Acts on verified WorkOS events.
Signature
declare class WorkOSEvents
Since v0.1.0
utils
typeEvent
Type a decoded event by its name, keeping an event whose data does not decode as unknown rather than refusing it.
Signature
declare const typeEvent: (event: WireEvent) => WorkOSEvent
Since v0.1.0
verification
verify
Verify a webhook and decode its event. header is the WorkOS-Signature value (None when absent), rawBody the body exactly as received.
Signature
declare const verify: (
rawBody: string,
header: Option.Option<string>,
secret: Redacted.Redacted,
tolerance?: number
) => Effect.Effect<WorkOSEvent, WebhookVerificationError | Auth.Errors.AuthError, Mac.Mac>
Since v0.1.0