> For the complete documentation index, see [llms.txt](https://docs.agentipy.fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.agentipy.fun/tools/trade-token-swap.md).

# Trade (Token Swap)

The `Trade` tool, part of AgentiPy, provides functionalities for executing token swaps on the Solana blockchain using the Jupiter Aggregator. It allows users to swap one SPL token (or SOL) for another by fetching optimal routes and transaction details from Jupiter's API.

## Functionality

This tool is implemented in the `TradeManager` class.

### Key Methods:

* `trade(agent: SolanaAgentKit, output_mint: Pubkey, input_amount: float, input_mint: Pubkey = TOKENS["USDC"], slippage_bps: int = DEFAULT_OPTIONS["SLIPPAGE_BPS"]) -> str`:
  * Takes a `SolanaAgentKit` instance, the `output_mint` (Pubkey of the token to receive), `input_amount` (amount of the input token to swap, in UI units like SOL or USDC quantity), `input_mint` (Pubkey of the token to send, defaults to USDC), and `slippage_bps` (slippage tolerance in basis points, default is 300 = 3%).
  * Fetches a swap quote from the Jupiter API (`/quote`) based on the input parameters. This quote provides the best route and expected output amount.
  * Requests the serialized swap transaction from the Jupiter API (`/swap`) using the obtained quote and the agent's wallet address.
  * Signs the received `VersionedTransaction` with the agent's wallet.
  * Sends and confirms the transaction on the Solana network.
  * Returns the transaction signature as a string.

##

**Important Note:** This tool performs on-chain financial transactions. Using it with real assets carries risk, including potential loss of funds due to market volatility, slippage, or smart contract interactions. Always test thoroughly on Devnet with small amounts if possible, and understand the tokens and protocols you are interacting with.

## Source Code

You can find the source code for this tool on GitHub: <https://github.com/niceberginc/agentipy/blob/main/agentipy/tools/trade.py>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.agentipy.fun/tools/trade-token-swap.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
