# payForRequirement

> Helper to execute full payment flow for an x402 payment challenge.

```ts
import { payForRequirement } from 'x402-avm-js';
```

```ts
export async function payForRequirement(
  paymentRequired: PaymentRequired,
  payerAddress: string,
  algod: algosdk.Algodv2,
  signTxns: (txns: Uint8Array[], indexes: number[]) => Promise<(Uint8Array | null)[]>,
  controls: SpendControls = {}
): Promise<string>;
```

Helper to execute full payment flow for an x402 payment challenge.

Automates the complete client payment workflow:
1. Fetches current suggested transaction parameters from the algod client.
2. Matches the requirement against the active algod network using `selectRequirement`.
3. Builds the exact AVM atomic transaction group via `buildExactAvmGroup`.
4. Calls `signTxns` for the transaction indexes requiring payer signature.
5. Assembles signed and unsigned transactions and encodes the `PAYMENT-SIGNATURE` header.

## Parameters

- `paymentRequired` (`PaymentRequired`, required):
The PaymentRequired challenge from HTTP 402 response.

- `payerAddress` (`string`, required):
Algorand address of the paying account.

- `algod` (`algosdk.Algodv2`, required):
Algodv2 client instance for the active network.

- `signTxns` (`true`): Promise<(Uint8Array | null)[]>, controls: SpendControls = &#123;&#125;" required>
Callback function that signs unsigned transaction byte arrays for specified indexes.

## Returns

- `returns` (`true`): ">
Promise resolving to the base64-encoded `PAYMENT-SIGNATURE` header string.

## Throws

- `NoSupportedRequirementError`: If `accepts` is empty.
- `NetworkMismatchError`: If no offer is for the algod's network.
- `SpendLimitExceededError`: If the amount is above the cap.
- `PaymentNotAllowedError`: If the asset, network or recipient is not allowed. Errors from algod or from `signTxns` propagate unchanged.

## Example

```ts
const paymentSignature = await payForRequirement(
  challenge,
  activeAddress,
  algod,
  async (txns, indexes) => {
    return await signTransactions(txns, indexes);
  }
);
```

Source: [src/index.ts](https://github.com/x402-avm/x402-avm-js/blob/main/src/index.ts#L732)

Source: https://x402-avm.github.io/reference/js/pay-for-requirement
