Faremeter API / middleware/src / common
common
Type Aliases
CommonMiddlewareArgs
CommonMiddlewareArgs = object
Defined in: packages/middleware/src/common.ts:340
Common configuration arguments shared by all middleware implementations.
Properties
accepts
accepts: (Defined in: packages/middleware/src/common.ts:344 Payment requirements this endpoint accepts. Can be flat or nested arrays.RelaxedRequirements|RelaxedRequirements[])[]
cacheConfig?
Defined in: packages/middleware/src/common.ts:346 Optional cache configuration for payment requirements responses.optionalcacheConfig:createPaymentRequiredResponseCacheOpts
facilitatorURL
facilitatorURL: string
Defined in: packages/middleware/src/common.ts:342
URL of the facilitator service.
supportedVersions?
Defined in: packages/middleware/src/common.ts:348 Which x402 protocol versions to support.optionalsupportedVersions:SupportedVersionsConfig
createPaymentRequiredResponseCacheOpts
createPaymentRequiredResponseCacheOpts =Defined in: packages/middleware/src/common.ts:787 Configuration options for the payment requirements response cache.AgedLRUCacheOpts&object
Type declaration
disable?
If true, disables caching entirely.optionaldisable:boolean
HandleMiddlewareRequestArgs
HandleMiddlewareRequestArgs<Defined in: packages/middleware/src/common.ts:412 Arguments for the core middleware request handler. Framework-specific middleware implementations adapt their request/response objects to this interface.MiddlewareResponse> =Omit<CommonMiddlewareArgs,"supportedVersions"> &object
Type declaration
body()
body: (Handler function called when a valid payment is received.context) =>Promise<MiddlewareResponse|undefined>
Parameters
context
MiddlewareBodyContext<MiddlewareResponse>
Returns
Promise<MiddlewareResponse | undefined>
fetch?
Optional custom fetch function for facilitator requests.optionalfetch: typeoffetch
getHeader()
getHeader: (Function to retrieve a request header value.key) =>string|undefined
Parameters
key
string
Returns
string | undefined
getPaymentRequiredResponse
getPaymentRequiredResponse: typeof getPaymentRequiredResponse
Function to fetch v1 payment requirements from the facilitator.
getPaymentRequiredResponseV2?
Optional function to fetch v2 payment requirements from the facilitator.optionalgetPaymentRequiredResponseV2: typeofgetPaymentRequiredResponseV2
resource
resource: string
The resource URL being accessed.
sendJSONResponse()
sendJSONResponse: (Function to send a JSON response with optional headers.status,body?,headers?) =>MiddlewareResponse
Parameters
status
PossibleStatusCodes
body?
PossibleJSONResponse
headers?
Record<string, string>
Returns
MiddlewareResponse
setResponseHeader()?
Optional function to set a response header.optionalsetResponseHeader: (key,value) =>void
Parameters
key
string
value
string
Returns
void
supportedVersions
supportedVersions:Resolved supported versions configuration.Required<SupportedVersionsConfig>
Type Parameters
MiddlewareResponse
MiddlewareResponse = unknown
MiddlewareBodyContext
MiddlewareBodyContext<Defined in: packages/middleware/src/common.ts:403 Context provided to the middleware body handler. Use protocolVersion to discriminate between v1 and v2 request types.MiddlewareResponse> =MiddlewareBodyContextV1<MiddlewareResponse> |MiddlewareBodyContextV2<MiddlewareResponse>
Type Parameters
MiddlewareResponse
MiddlewareResponse
MiddlewareBodyContextV1
MiddlewareBodyContextV1<Defined in: packages/middleware/src/common.ts:379 Context provided to the middleware body handler for v1 protocol requests. Contains payment information and functions to verify or settle the payment.MiddlewareResponse> =object
Type Parameters
MiddlewareResponse
MiddlewareResponse
Properties
paymentPayload
paymentPayload: x402PaymentPayloadV1
Defined in: packages/middleware/src/common.ts:382
paymentRequirements
paymentRequirements: x402PaymentRequirementsV1
Defined in: packages/middleware/src/common.ts:381
protocolVersion
protocolVersion: 1
Defined in: packages/middleware/src/common.ts:380
settle()
settle: () =>Defined in: packages/middleware/src/common.ts:383Promise<SettleResultV1<MiddlewareResponse>>
Returns
Promise<SettleResultV1<MiddlewareResponse>>
verify()
verify: () =>Defined in: packages/middleware/src/common.ts:384Promise<VerifyResultV1<MiddlewareResponse>>
Returns
Promise<VerifyResultV1<MiddlewareResponse>>
MiddlewareBodyContextV2
MiddlewareBodyContextV2<Defined in: packages/middleware/src/common.ts:391 Context provided to the middleware body handler for v2 protocol requests. Contains payment information and functions to verify or settle the payment.MiddlewareResponse> =object
Type Parameters
MiddlewareResponse
MiddlewareResponse
Properties
paymentPayload
paymentPayload: x402PaymentPayload
Defined in: packages/middleware/src/common.ts:394
paymentRequirements
paymentRequirements: x402PaymentRequirements
Defined in: packages/middleware/src/common.ts:393
protocolVersion
protocolVersion: 2
Defined in: packages/middleware/src/common.ts:392
settle()
settle: () =>Defined in: packages/middleware/src/common.ts:395Promise<SettleResultV2<MiddlewareResponse>>
Returns
Promise<SettleResultV2<MiddlewareResponse>>
verify()
verify: () =>Defined in: packages/middleware/src/common.ts:396Promise<VerifyResultV2<MiddlewareResponse>>
Returns
Promise<VerifyResultV2<MiddlewareResponse>>
RelaxedRequirements
RelaxedRequirements =Defined in: packages/middleware/src/common.ts:154Partial<x402PaymentRequirementsV1>
RelaxedRequirementsV2
RelaxedRequirementsV2 =Defined in: packages/middleware/src/common.ts:155Partial<x402PaymentRequirements>
SettleResult
SettleResult<Defined in: packages/middleware/src/common.ts:359MiddlewareResponse> =SettleResultV1<MiddlewareResponse> |SettleResultV2<MiddlewareResponse>
Type Parameters
MiddlewareResponse
MiddlewareResponse
SettleResultV1
SettleResultV1<Defined in: packages/middleware/src/common.ts:351MiddlewareResponse> = {facilitatorResponse:x402SettleResponseV1;success:true; } | {errorResponse:MiddlewareResponse;success:false; }
Type Parameters
MiddlewareResponse
MiddlewareResponse
SettleResultV2
SettleResultV2<Defined in: packages/middleware/src/common.ts:355MiddlewareResponse> = {facilitatorResponse:x402SettleResponse;success:true; } | {errorResponse:MiddlewareResponse;success:false; }
Type Parameters
MiddlewareResponse
MiddlewareResponse
SupportedVersionsConfig
SupportedVersionsConfig = object
Defined in: packages/middleware/src/common.ts:308
Configuration for which x402 protocol versions the middleware supports.
At least one version must be enabled.
Properties
x402v1?
Defined in: packages/middleware/src/common.ts:310 Support x402 v1 protocol (JSON body responses, X-PAYMENT header). Default: trueoptionalx402v1:boolean
x402v2?
Defined in: packages/middleware/src/common.ts:312 Support x402 v2 protocol (PAYMENT-REQUIRED header, PAYMENT-SIGNATURE header). Default: falseoptionalx402v2:boolean
VerifyResult
VerifyResult<Defined in: packages/middleware/src/common.ts:371MiddlewareResponse> =VerifyResultV1<MiddlewareResponse> |VerifyResultV2<MiddlewareResponse>
Type Parameters
MiddlewareResponse
MiddlewareResponse
VerifyResultV1
VerifyResultV1<Defined in: packages/middleware/src/common.ts:363MiddlewareResponse> = {facilitatorResponse:x402VerifyResponseV1;success:true; } | {errorResponse:MiddlewareResponse;success:false; }
Type Parameters
MiddlewareResponse
MiddlewareResponse
VerifyResultV2
VerifyResultV2<Defined in: packages/middleware/src/common.ts:367MiddlewareResponse> = {facilitatorResponse:x402VerifyResponse;success:true; } | {errorResponse:MiddlewareResponse;success:false; }
Type Parameters
MiddlewareResponse
MiddlewareResponse
Functions
createPaymentRequiredResponseCache()
createPaymentRequiredResponseCache(Defined in: packages/middleware/src/common.ts:801 Creates a cached wrapper around payment requirements fetching functions. The cache reduces load on the facilitator by reusing recent responses for identical requirements.opts):object
Parameters
opts
createPaymentRequiredResponseCacheOpts = {}
Cache configuration options
Returns
Object containing cached getPaymentRequiredResponse functionsgetPaymentRequiredResponse()
getPaymentRequiredResponse: (Fetches v1 payment requirements from the facilitator’s /accepts endpoint.args) =>Promise<{accepts:object[];error:string;x402Version:number; }>
Parameters
args
getPaymentRequiredResponseArgs
Arguments including facilitator URL and accepted payment types
Returns
Promise<{ accepts: object[]; error: string; x402Version: number; }>
The validated payment required response from the facilitator
getPaymentRequiredResponseV2()
getPaymentRequiredResponseV2: (Fetches v2 payment requirements from the facilitator’s /accepts endpoint.args) =>Promise<{accepts:object[];error?:string;extensions?:object;resource: {description?:string;mimeType?:string;url:string; };x402Version:2; }>
Parameters
args
getPaymentRequiredResponseV2Args
Arguments including facilitator URL, resource info, and accepted payment types
Returns
Promise<{ accepts: object[]; error?: string; extensions?: object; resource: { description?: string; mimeType?: string; url: string; }; x402Version: 2; }>
The validated v2 payment required response from the facilitator
findMatchingPaymentRequirements()
findMatchingPaymentRequirements(Defined in: packages/middleware/src/common.ts:117 Finds the payment requirement that matches the client’s v1 payment payload.accepts,payload):undefined| {asset:string;description:string;extra?:object;maxAmountRequired:string;maxTimeoutSeconds:number;mimeType?:string;network:string;outputSchema?:object;payTo:string;resource:string;scheme:string; }
Parameters
accepts
object[]
Array of accepted payment requirements from the facilitator
payload
The client’s payment payloadasset?
string
network
string
payload
object
scheme
string
x402Version
number
Returns
undefined | { asset: string; description: string; extra?: object; maxAmountRequired: string; maxTimeoutSeconds: number; mimeType?: string; network: string; outputSchema?: object; payTo: string; resource: string; scheme: string; }
The matching requirement, or undefined if no match found
findMatchingPaymentRequirementsV2()
findMatchingPaymentRequirementsV2(Defined in: packages/middleware/src/common.ts:131 Finds the payment requirement that matches the client’s v2 payment payload.accepts,payload):undefined| {amount:string;asset:string;extra?:object;maxTimeoutSeconds:number;network:string;payTo:string;scheme:string; }
Parameters
accepts
object[]
Array of accepted payment requirements from the facilitator
payload
The client’s v2 payment payloadaccepted
{amount: string; asset: string; extra?: object; maxTimeoutSeconds: number; network: string; payTo: string; scheme: string; }
accepted.amount
string
accepted.asset
string
accepted.extra?
object
accepted.maxTimeoutSeconds
number
accepted.network
string
accepted.payTo
string
accepted.scheme
string
extensions?
object
payload
object
resource?
{description?: string; mimeType?: string; url: string; }
resource.description?
string
resource.mimeType?
string
resource.url
string
x402Version
2
Returns
undefined | { amount: string; asset: string; extra?: object; maxTimeoutSeconds: number; network: string; payTo: string; scheme: string; }
The matching requirement, or undefined if no match found
gateGetPaymentRequiredResponse()
gateGetPaymentRequiredResponse(Defined in: packages/middleware/src/common.ts:143 Validates that a facilitator response is successful, throwing if not.res):void
Parameters
res
Response
The Response from the facilitator
Returns
void
getPaymentRequiredResponse()
getPaymentRequiredResponse(Defined in: packages/middleware/src/common.ts:222 Fetches v1 payment requirements from the facilitator’s /accepts endpoint.args):Promise<{accepts:object[];error:string;x402Version:number; }>
Parameters
args
getPaymentRequiredResponseArgs
Arguments including facilitator URL and accepted payment types
Returns
Promise<{ accepts: object[]; error: string; x402Version: number; }>
The validated payment required response from the facilitator
getPaymentRequiredResponseV2()
getPaymentRequiredResponseV2(Defined in: packages/middleware/src/common.ts:270 Fetches v2 payment requirements from the facilitator’s /accepts endpoint.args):Promise<{accepts:object[];error?:string;extensions?:object;resource: {description?:string;mimeType?:string;url:string; };x402Version:2; }>
Parameters
args
getPaymentRequiredResponseV2Args
Arguments including facilitator URL, resource info, and accepted payment types
Returns
Promise<{ accepts: object[]; error?: string; extensions?: object; resource: { description?: string; mimeType?: string; url: string; }; x402Version: 2; }>
The validated v2 payment required response from the facilitator
handleMiddlewareRequest()
handleMiddlewareRequest<Defined in: packages/middleware/src/common.ts:452 Core middleware request handler that processes x402 payment flows. This function handles both v1 and v2 protocol versions, validates payment headers, communicates with the facilitator, and delegates to the body handler when payment is valid.MiddlewareResponse>(args):Promise<undefined|MiddlewareResponse>
Type Parameters
MiddlewareResponse
MiddlewareResponse
Parameters
args
HandleMiddlewareRequestArgs<MiddlewareResponse>
Handler arguments including framework-specific adapters
Returns
Promise<undefined | MiddlewareResponse>
The middleware response, or undefined if the body handler should continue
resolveSupportedVersions()
resolveSupportedVersions(Defined in: packages/middleware/src/common.ts:320 Resolve and validate supported versions config. Returns resolved config with defaults applied. Throws if configuration is invalid.config?):Required<SupportedVersionsConfig>
Parameters
config?
SupportedVersionsConfig
Returns
Required<SupportedVersionsConfig>