parseHeader (Webhooks)

stripe-node · integration, webhooks, signature-verification, header-parsing, payments

Parses a webhook header string into timestamp and signatures, supporting custom signature schemes. Essential for verifying incoming webhook payloads.

  function parseHeader(
    header: WebhookHeader,
    scheme?: string
  ): WebhookParsedHeader | null {
    if (typeof header !== 'string') {
      return null;
    }
    scheme = scheme || signature.EXPECTED_SCHEME;

    return header.split(',').reduce<WebhookParsedHeader>(
      (accum, item) => {
        const kv = item.split('=');

        if (kv[0] === 't') {
          accum.timestamp = parseInt(kv[1], 10);
        }

        if (kv[0] === scheme) {
          accum.signatures.push(kv[1]);
        }

        return accum;
      },
      {
        timestamp: -1,
        signatures: [],
      }
    );
  }

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key