x402 icon indicating copy to clipboard operation
x402 copied to clipboard

intercept/buffer responses in flask middleware

Open phdargen opened this issue 1 month ago • 2 comments

Description

Flask middleware sends response to client immediately when route handler is called, if settlement fails resource was obtained for free.

This is solved similar as in the x402-express middleware by intercepting response methods, buffering all calls and replaying after successful settlement.

Tests

Tested with flask server example + forcing settle_response.success = False in the flask middleware.

Client output before: { report: { temperature: 70, weather: 'sunny' } }

Client output after:

{
  x402Version: 1,
  accepts: [
    {
      scheme: 'exact',
      network: 'base-sepolia',
      maxAmountRequired: '1000',
      resource: 'http://localhost:4021/weather',
      description: '',
      mimeType: '',
      outputSchema: [Object],
      payTo: '0x1c47E9C085c2B7458F5b6C16cCBD65A65255a9f6',
      maxTimeoutSeconds: 60,
      asset: '0x036CbD53842c5426634e7929541eC2318f3dCF7e',
      extra: [Object]
    }
  ],
  error: 'Settle failed: Unknown error'
}

Checklist

  • [x] I have formatted and linted my code
  • [x] All new and existing tests pass
  • [x] My commits are signed (required for merge) -- you may need to rebase if you initially pushed unsigned commits

phdargen avatar Nov 27 '25 16:11 phdargen

✅ Heimdall Review Status

Requirement Status More Info
Reviews 1/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

cb-heimdall avatar Nov 27 '25 16:11 cb-heimdall

@phdargen is attempting to deploy a commit to the Coinbase Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Nov 27 '25 16:11 vercel[bot]