Skip to content
LogoLogo

Mppx.prepareRequest

Prepare a request-bound payment

Follows safe pre-payment redirects and prepares the exact HTTP request that returned a Challenge.

Usage

client.ts
import { ,  } from 'mppx/client'
import {  } from 'viem/accounts'
 
const  = .({
  : [
    ({
      : (
        '0x0123456789012345678901234567890123456789012345678901234567890123',
      ),
    }),
  ],
  : false,
})
 
const  = await .('https://api.example.com/checkout', {
  : .({ : 'pro' }),
  : { 'Content-Type': 'application/json' },
  : 'POST',
})
 
.(..)
.(.)
 
const  = await .()
 
.(.)
200

prepareRequest sends the request without creating a Credential, follows pre-payment redirects, and stops at the payment-required response. Use it when you need to inspect or approve payment terms while keeping the Credential bound to the request that produced the Challenge.

The method rejects HTTPS downgrade redirects, removes credentials and sensitive headers on cross-origin redirects, and applies standard redirect method changes. pay creates the Credential at most once and sends it only to the prepared URL with redirects disabled.

With request-local selection

Override the configured payment preferences for one request.

client.ts
import { ,  } from 'mppx/client'
import type {  } from 'viem'
 
declare const : 
 
const  = .({
  : [({  })],
  : false,
})
 
const  = await .(
  'https://api.example.com/paid',
  ,
  {
    : 'tempo/charge;q=1, tempo/session;q=0',
    : 5,
    : () => .(({  }) =>
      .. !== '0'
    ),
  },
)

Return type

type PreparedRequest = Readonly<{
  challenge: Challenge
  challenges: readonly Challenge[]
  createCredential: (context?: Context) => Promise<string>
  method: Method.Client
  pay: (context?: Context) => Promise<Response>
  redirects: readonly Readonly<{
    from: string
    status: number
    to: string
  }>[]
  request: Request
  response: Response
  setCredential: (request: RequestInit, credential: string) => RequestInit
}>

challenge

The selected immutable Challenge.

challenges

All supported Challenges extracted from the payment-required response.

createCredential

Creates the selected Challenge's Credential. Repeated or concurrent calls return the same promise.

method

An immutable snapshot of the configured client method selected for the Challenge.

pay

Creates and sends the Credential to the prepared request. The paid request uses redirect: 'manual', so the Credential doesn't follow a redirect to another URL.

redirects

Immutable pre-payment redirect records with each source URL, status code, and destination URL.

request

The exact request that returned the payment-required response.

response

The payment-required response returned for request.

setCredential

Returns a request initializer with the Credential attached through the protocol that produced the Challenge.

Parameters

init (optional)

  • Type: RequestInit

Request options for the initial HTTP request.

input

  • Type: RequestInfo | URL

URL or request to prepare.

options (optional)

  • Type: { acceptPayment?: string | readonly AcceptPayment.Entry[]; maxRedirects?: number; orderChallenges?: OrderChallenges }

Request-local Challenge selection and redirect limits.

  • acceptPayment overrides the configured payment preferences.
  • maxRedirects sets the maximum pre-payment redirects. Defaults to 20.
  • orderChallenges filters or sorts supported Challenge candidates.