Skip to content

feat: fetch --dry-run previews the price without paying#56

Merged
rolznz merged 5 commits into
masterfrom
feat/fetch-dry-run
Jul 22, 2026
Merged

feat: fetch --dry-run previews the price without paying#56
rolznz merged 5 commits into
masterfrom
feat/fetch-dry-run

Conversation

@reneaaron

@reneaaron reneaaron commented Jul 14, 2026

Copy link
Copy Markdown
Member

What

Adds fetch --dry-run: sends the request unpaid and reports the 402 challenge instead of settling it. Needs no wallet.

$ alby fetch --dry-run "https://www.l402apps.com/api/apis"
{
  "url": "https://www.l402apps.com/api/apis",
  "status": 402,
  "payment_required": true,
  "amount_in_sats": 10,
  "description": "L402 Apps — Get APIs directory"
}
  • Price in sats is extracted from wherever the protocol puts the lightning invoice: WWW-Authenticate (L402 ... invoice= / MPP Payment ... invoice=) or a lightning-native x402's base64 Payment-Required header.
  • When no lightning invoice is offered (e.g. a USDC-only x402 hit directly instead of through the l402.space bridge), the raw challenge is surfaced so the terms are still visible.
  • A free endpoint reports payment_required: false.
  • Payment flags (--max-amount/--currency/--unit/--network/--credentials) are rejected alongside --dry-run since a dry run never pays.

Why

From the review discussion on getAlby/payments-skill#28: prices listed on 402index.io can be missing, stale, or dynamic — the 402 challenge is the authoritative price at request time, but checking it previously meant hand-crafting a curl. Use case: comparing prices between two providers before paying.

Per that discussion this is deliberately not documented in the skill for now — the --max-amount default already guards automated flows.

Test

  • yarn test green (111 pass, 2 skipped); 5 new unit tests cover L402, MPP, base64-x402 extraction, no-lightning-challenge fallback, and free endpoints (using a real BOLT-11 so decoding is exercised).
  • Verified live against all three challenge shapes without paying: native L402 (10 sats), bridged x402 via l402.space (19 sats), lightning-native x402 (15 sats).

Summary by CodeRabbit

  • New Features

    • Added a --dry-run option to preview request pricing without making payments.
    • Reports Lightning invoice amounts, descriptions, payment challenges, and non-payment response statuses.
    • Supports pricing details from multiple payment challenge formats and response bodies.
  • Bug Fixes

    • Standardized request handling, including automatic JSON content types for applicable requests.

Per payments-skill#28 review: prices listed by 402index.io can be missing,
stale, or dynamic - the 402 challenge is the authoritative price at request
time, but checking it previously meant hand-crafting a curl. --dry-run sends
the request unpaid (no wallet needed) and reports the challenge: price in
sats when a lightning invoice is offered (L402/MPP WWW-Authenticate, or a
lightning-native x402 Payment-Required header), the raw challenge otherwise.
Payment flags are rejected alongside it since a dry run never pays.

Use case: comparing prices between providers before paying.

Verified live against all three challenge shapes: native L402 (10 sats),
bridged x402 via l402.space (19 sats), lightning-native x402 (15 sats).
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@rolznz, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 45 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9a676cdb-059b-4f5f-a40f-4da91e5236f7

📥 Commits

Reviewing files that changed from the base of the PR and between 46cd43b and ff7e19c.

📒 Files selected for processing (2)
  • src/test/fetch-dry-run.test.ts
  • src/tools/lightning/fetch.ts
📝 Walkthrough

Walkthrough

The fetch command adds a --dry-run mode that performs requests without a wallet, detects 402 payment challenges, extracts Lightning invoice pricing, and reports non-payment responses. Request option construction is shared with the existing payment flow and covered by new tests.

Changes

Fetch dry-run flow

Layer / File(s) Summary
Shared request option construction
src/tools/lightning/fetch.ts
Centralizes request method, JSON body, and default content-type handling for fetch402.
Dry-run response parsing
src/tools/lightning/fetch.ts, src/test/fetch-dry-run.test.ts
Adds dryRun402 and DryRun402Result, supporting Lightning invoice extraction from L402, MPP, x402, and response-body formats, with tests for invoice and non-402 responses.
Fetch CLI dry-run wiring
src/commands/fetch.ts
Adds --dry-run, passes request parameters to dryRun402, outputs its result, and skips payment-related processing.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant dryRun402
  participant HTTPServer
  participant Invoice
  CLI->>dryRun402: Submit URL, method, body, and headers
  dryRun402->>HTTPServer: Send request without wallet
  HTTPServer-->>dryRun402: Return status and payment challenge
  dryRun402->>Invoice: Parse extracted BOLT11 invoice
  Invoice-->>dryRun402: Return amount and description
  dryRun402-->>CLI: Output dry-run result
Loading

Possibly related PRs

  • getAlby/cli#7: Introduced the fetch and fetch402 pipeline extended by this dry-run flow.
  • getAlby/cli#46: Previously modified the fetch pipeline and parameter handling used by this change.
  • getAlby/cli#59: Also updates fetch402 recovery handling and fetch command payment-path wiring.

Suggested reviewers: rolznz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding fetch --dry-run to preview price information without paying.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/fetch-dry-run

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@rolznz
rolznz marked this pull request as ready for review July 22, 2026 06:28
rolznz and others added 3 commits July 22, 2026 16:12
Resolves conflicts with #49/#59: keep both dryRun402 and the payment
recovery helpers in tools/lightning/fetch.ts, union the imports in
commands/fetch.ts, and reject the new --resume flag alongside the other
payment flags when --dry-run is set.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The CLI can only pay lightning, so a challenge without a lightning
invoice was dead weight as a raw blob - point at the bridge URL that
makes the endpoint payable instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/commands/fetch.ts`:
- Around line 161-172: Change the dry-run branch in the fetch command to reject,
rather than silently ignore, any payment-related options: max-amount, currency,
unit, network, credentials, or resume. Add the validation before calling
dryRun402, report the invalid combination, and preserve the existing dry-run
behavior when none of those flags are supplied.

In `@src/tools/lightning/fetch.ts`:
- Around line 156-159: Update dryRun402 to enforce a request timeout by
supplying an AbortSignal or equivalent timeout through its fetch options,
matching the timeout behavior used by the paying path via fetch402Lib. Preserve
the existing URL, request construction, and response handling while ensuring
unresponsive targets cannot hang indefinitely.
- Around line 168-178: Update the invoice handling in dryRun402 around
extractLightningInvoice and new Invoice so malformed BOLT11 decoding is caught
with try/catch; only return the payment-required result after successful
decoding, and fall through to the existing raw-challenge fallback when
construction fails.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1512f2e0-4d57-470a-ae96-ddf67b399ffb

📥 Commits

Reviewing files that changed from the base of the PR and between 48cbcea and 46cd43b.

📒 Files selected for processing (3)
  • src/commands/fetch.ts
  • src/test/fetch-dry-run.test.ts
  • src/tools/lightning/fetch.ts

Comment thread src/commands/fetch.ts
Comment thread src/tools/lightning/fetch.ts
Comment thread src/tools/lightning/fetch.ts
…code

The BOLT11 pattern can match invoice-looking garbage in a challenge
body; decoding it then threw and crashed the dry run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@rolznz
rolznz merged commit 42d902c into master Jul 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants