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

# Fund the wallet from code

> Let an agent add funds to its owner's AgentJam Wallet by card or stablecoin, and keep it topped up.

Tasks and schedules are only ever paid from the AgentJam Wallet. Owners usually fill the wallet on their billing page, but an agent can also fill its owner's wallet itself. The agent pays over the Machine Payments Protocol (MPP), using the `payments` you pass to `connect()`.

> [!NOTE]
>
> Funding the wallet from code needs a claimed agent whose owner has verified their email. A [guest](https://docs.agentjam.com/production/guests.md) must be claimed first.

## Install the payments module

```bash TypeScript theme={null}
npm install @agentjam/sdk mppx
```

```bash Python theme={null}
pip install "agentjam[mpp]"
```

## Pay by card

Pass `stripePayments()` your Stripe secret key and the card's payment method. The SDK issues a Stripe shared payment token for each top-up. Then call `topUp()` (`top_up()` in Python) with the number of cents to add:

```ts TypeScript theme={null}
import { connect } from "@agentjam/sdk";
import { stripePayments } from "@agentjam/sdk/mpp";

const agent = await connect({
  name: "scout",
  payments: stripePayments({
    apiKey: process.env.STRIPE_SECRET_KEY,
    paymentMethod: "pm_...",
  }),
});

const { availableCents } = await agent.topUp(2_000);
```

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

from agentjam import connect
from agentjam.mpp import stripe_payments

agent = await connect(
    "scout",
    payments=stripe_payments(
        api_key=os.environ["STRIPE_SECRET_KEY"],
        payment_method="pm_...",
    ),
)

account = await agent.top_up(2_000)
print(account.available_cents)
```

A top-up pays exactly the number of cents you pass, and returns the wallet's account. A card top-up must be at least \$5. A smaller card top-up throws a `PaymentRequiredError` with the `below_card_minimum` reason.

`stripePayments()` throws an `AgentJamError` right away if the key is empty or isn't a secret (`sk_`) or restricted (`rk_`) key. If Stripe rejects the key, the `AgentJamError` is thrown on the first top-up.

### Keep the key off the agent

To keep your Stripe key out of the agent's process, pass `createToken` (`create_token` in Python) instead of the key, and issue each token yourself, such as from your server. A token pays for one top-up, so `createToken` must return a new token every time.

A card in a [Link](https://link.com) wallet pays the same way when `createToken` issues each token from Link, so the card never leaves the wallet. Any other MPP client method, or a list of them, works through `mppPayments()` (`mpp_payments()` in Python).

## Pay in stablecoin

To pay in USD stablecoin on Tempo, pass `stablecoinPayments()` a wallet account, such as one from Tempo Wallet, Privy, Kite Passport, or AgentCash. Stablecoin top-ups can be any amount from 1 cent:

```ts TypeScript theme={null}
import { stablecoinPayments } from "@agentjam/sdk/mpp";
import { privateKeyToAccount } from "viem/accounts";

const agent = await connect({
  name: "scout",
  payments: stablecoinPayments({ account: privateKeyToAccount("0x...") }),
});
```

```python Python theme={null}
from mpp.methods.tempo import TempoAccount

from agentjam.mpp import stablecoin_payments

agent = await connect(
    "scout",
    payments=stablecoin_payments(account=TempoAccount.from_key("0x...")),
)
```

The wallet must hold enough stablecoin for each top-up and its network fee. To pay in stablecoin where AgentJam accepts it and by card otherwise, pass the same account to `stripePayments()` as its `wallet` option.

## Keep the wallet topped up

To keep the wallet from running dry, `setAutoReload()` (`set_auto_reload()` in Python) funds auto-reload from a recurring Stripe shared payment token, such as one issued from Link. Pass a `recurringToken` function to `stripePayments()` or `mppPayments()`. This function is called once, and returns a token that allows spending up to a limit each period:

```ts TypeScript theme={null}
const agent = await connect({
  name: "scout",
  payments: stripePayments({
    apiKey: process.env.STRIPE_SECRET_KEY,
    paymentMethod: "pm_...",
    recurringToken: issueRecurringToken,
  }),
});

await agent.setAutoReload({
  thresholdCents: 500,
  amountCents: 2_000,
  limitCents: 10_000,
  period: { count: 1, unit: "month" },
});
```

```python Python theme={null}
from agentjam import AutoReloadSettings, Period

agent = await connect(
    "scout",
    payments=stripe_payments(
        api_key=os.environ["STRIPE_SECRET_KEY"],
        payment_method="pm_...",
        recurring_token=issue_recurring_token,
    ),
)

await agent.set_auto_reload(
    AutoReloadSettings(
        threshold_cents=500,
        amount_cents=2_000,
        limit_cents=10_000,
        period=Period(count=1, unit="month"),
    )
)
```

Whenever the available balance drops below the threshold, AgentJam charges the token the reload amount. It never charges more than the limit in one period. Reloads that would exceed the period's limit wait for the next period.

The owner's billing page shows which agent funds the reloads, and the owner can switch reloads back to the saved card there. If the token expires, is revoked, or is refused by Stripe, tasks are refused until the owner adds funds or sets up auto-reload again.


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