> ## Documentation Index
> Fetch the complete documentation index at: https://ifdocdemo.lumivescence.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Pay from code

> Pay MPP and x402 endpoints from your own application with the InFlow buyer SDKs for Python, Go, Rust, and Node.js.

Pay MPP and x402 endpoints from your own application with a buyer SDK for Node.js, Python, Go, or Rust. Each SDK pays from the InFlow balance of the API key account when the seller offers it. The x402 buyer prefers the `balance` scheme, then `exact`. For the other ways to pay, see [How the agent pays](/agents/pay-for-a-resource#how-the-agent-pays).

## Before you start

| Item | Detail |
| - | - |
| Account | An InFlow account and an API key. A Developer account can buy. A Seller account can also buy. |
| Environment variable | `INFLOW_API_KEY`. |
| Environment | The examples use the sandbox. The value must match the dashboard of the key. |

When a payment needs [owner approval](/concepts/payments#human-approval), it waits until the owner approves it.

## Pay an endpoint

<Tabs>
  <Tab title="Python">
    ### MPP

    ```bash theme={null}
    pip install 'inflowpay[mpp]'
    ```

    The payment transport answers the `402` challenge for each request that `httpx` sends.

    ```python theme={null}
    import os

    import httpx

    from inflowpay import ClientOptions
    from inflowpay.mpp.buyer import BuyerMethod, payment_transport


    async def buy(url: str) -> bytes:
        async with BuyerMethod(
            ClientOptions(environment="sandbox", api_key=os.environ["INFLOW_API_KEY"])
        ) as method:
            async with httpx.AsyncClient(transport=payment_transport([method])) as http:
                response = await http.get(url)
                response.raise_for_status()
                return response.content
    ```

    ### x402

    ```bash theme={null}
    pip install 'inflowpay[x402]'
    ```

    ```python theme={null}
    import asyncio
    import os

    import httpx
    from x402.http.clients.httpx import x402AsyncTransport

    from inflowpay import ClientOptions
    from inflowpay.x402.buyer import Buyer


    async def main():
        async with (
            await Buyer.create(
                ClientOptions(api_key=os.environ["INFLOW_API_KEY"], environment="sandbox")
            ) as buyer,
            httpx.AsyncClient(transport=x402AsyncTransport(buyer), follow_redirects=False) as http,
        ):
            response = await http.get(os.environ["PAID_RESOURCE_URL"])
            response.raise_for_status()
            print(response.text)


    asyncio.run(main())
    ```

    The Python SDK also supports MPP buyers over MCP. Install `inflowpay[mpp,mcp]`.
  </Tab>

  <Tab title="Go">
    ```bash theme={null}
    go get github.com/inflowpayai/inflow-go@v0.3.1
    ```

    The Go buyer method `Do` sends the request, pays a `402` challenge, and sends one paid retry. To use the challenge yourself, use `Fulfil` for MPP or `Sign` for x402.

    ### MPP

    `Fulfil` turns an MPP challenge into a credential.

    ```go theme={null}
    client, err := buyer.New(buyer.Options{
        Options: inflow.Options{
            Environment: inflow.Sandbox,
            APIKey: os.Getenv("INFLOW_API_KEY"),
        },
    })
    if err != nil {
        return err
    }
    credential, err := client.Fulfil(ctx, challenge, buyer.PaymentOptions{})
    ```

    ### x402

    `Sign` turns the x402 payment requirements into a signed payment.

    ```go theme={null}
    client, err := buyer.New(buyer.Options{
        Options: inflow.Options{
            Environment: inflow.Sandbox,
            APIKey: os.Getenv("INFLOW_API_KEY"),
        },
    })
    if err != nil {
        return err
    }
    payment, err := client.Sign(ctx, required, buyer.SignOptions{})
    ```
  </Tab>

  <Tab title="Rust">
    ### MPP

    `inflow-mpp-buyer` uses an explicit credential workflow. The application prepares the payment, shows the approval to the owner, and waits for the credential.

    ```rust theme={null}
    use inflow_mpp_buyer::{
        Authentication, Buyer, CancellationToken, ClientOptions, Environment,
        PaymentChallenge, PaymentOptions, WaitOptions, Credential,
    };

    async fn approve(challenge: &PaymentChallenge) -> Result<Credential, Box<dyn std::error::Error>> {
        let buyer = Buyer::new(ClientOptions {
            environment: Environment::Sandbox,
            authentication: Authentication::ApiKey(std::env::var("INFLOW_API_KEY")?),
            ..Default::default()
        })?;
        let cancellation = CancellationToken::new();
        let payment = buyer.prepare(challenge, PaymentOptions::default(), &cancellation).await?;
        // The application can display these identifiers while approval is pending.
        let approval_id = payment.approval_id();
        let transaction_id = payment.transaction_id();
        let credential = payment.wait(WaitOptions::default()).await?;
        Ok(credential)
    }
    ```

    The MPP buyer supports InFlow charges, InFlow subscriptions, Tempo charges, and Visa CARD charges. For x402, use `inflow-x402-buyer`.
  </Tab>

  <Tab title="Node.js">
    ```bash theme={null}
    pnpm add @inflowpayai/mpp-buyer mppx@^0.8.17
    pnpm add @inflowpayai/x402-buyer @x402/core
    ```

    The `inflow-node` repository has runnable buyer examples. `mpp-buyer-fetch` replaces the global `fetch` with a payment-aware wrapper that pays a `402` response.

    First, start `mpp-seller-express` or `mpp-seller-hono` in a different terminal. The buyer requests `http://localhost:3000/api/widgets`.

    ```bash theme={null}
    cd examples/mpp-buyer-fetch
    cp .env.example .env
    # fill in INFLOW_API_KEY from https://sandbox.inflowpay.ai
    pnpm install
    pnpm start
    ```

    The other buyer examples are `mpp-buyer-manual`, `x402-buyer-fetch`, and `x402-buyer-axios`. `x402-buyer-x402-evm` and `x402-buyer-x402-svm` use only the x402 foundation packages.
  </Tab>
</Tabs>

## Rules for buyer code

* Never log the API key or a payment credential.
* Save the transaction ID as soon as you get it. You need it to check or resume a payment.
* Show the approval to the owner while the payment waits.
* Stripe acceptance is for sellers only. The InFlow buyer SDKs do not create Stripe tokens.

## Related pages

<CardGroup cols={2}>
  <Card title="Pay with the CLI" icon="credit-card" href="/agents/pay-for-a-resource">
    Pay an endpoint from the command line.
  </Card>

  <Card title="SDKs" icon="cube" href="/reference/sdk-packages">
    Read the packages, versions, and features of each SDK.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.