FG
๐Ÿ”Œ APIs & SDKsStripe

Better Access to Response Headers

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score51%
51%

Problem

On a successful API request the Stripe Node SDK doesn't provide a natural way of accessing response headers which can be useful in the case you're interested in response metadata (such as `Idempotency-Key` and `Request-ID`. For example today it is possible to do this but it is a little bit awkward since response metadata is exposed through an event emitter interface, while the response body is exposed through a callback interface: [code block] It would be a much better experience if this data was passed in the callback like so: [code block] My idea is that we add headers, statusCode and known Stripe headers such as `idempotencyKey` to go along with `requestId` to StripeResource here Thoughts?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Enhance Stripe Node SDK to Include Response Headers in Callbacks

Medium Risk

The current implementation of the Stripe Node SDK separates response body handling via callbacks and response metadata handling via event emitters. This design leads to an awkward experience for developers who need to access both response data and headers simultaneously, particularly for important metadata like 'Idempotency-Key' and 'Request-ID'.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Modify StripeResource to Include Headers

    Update the StripeResource class to include response headers in the callback function. This will involve modifying the existing callback signature to accept an additional parameter for headers.

    typescript
    callback(null, responseBody, { headers, statusCode, idempotencyKey, requestId });
  2. 2

    Update API Call Methods

    Adjust all API call methods within the SDK that utilize the callback pattern to ensure they pass the new headers and status code to the callback. This will ensure consistency across the SDK.

    typescript
    this.request('POST', '/endpoint', data, (err, responseBody) => { callback(err, responseBody, { headers: response.headers, statusCode: response.statusCode, idempotencyKey: response.headers['Idempotency-Key'], requestId: response.headers['Request-ID'] }); });
  3. 3

    Update Documentation

    Revise the SDK documentation to reflect the changes made to the callback interface. Include examples demonstrating how to access the new response headers and status code.

    typescript
    // Example usage:
    apiCall((err, responseBody, { headers, statusCode, idempotencyKey, requestId }) => {
      console.log(headers);
    });
  4. 4

    Run Unit Tests

    Create or update unit tests to verify that the new headers and status code are correctly passed to the callback. Ensure that existing tests are not broken by the changes.

    typescript
    expect(callback).toHaveBeenCalledWith(null, responseBody, { headers, statusCode, idempotencyKey, requestId });

Validation

Confirm the fix by running the updated SDK and making a sample API call. Check that the callback receives the response body along with headers and status code as expected. Additionally, run all unit tests to ensure they pass without errors.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

stripepaymentsapi