Function Signature
Description
Execute a token swap on supported chains. This method handles the entire swap process including finding the best route and executing the transaction.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
swapsApiKey | String | Yes | Your swaps API key |
chainId | String | Yes | The chain ID in CAIP-2 format |
buyToken | String | Yes | Token address to buy |
sellToken | String | Yes | Token address to sell |
amount | String | Yes | Amount to sell (in smallest unit) |
Returns
PortalSwapResult - An object containing:
| Property | Type | Description |
|---|---|---|
success | bool | Whether the swap was successful |
transactionHash | String? | The transaction hash |
Example
Native Token Address
Use the following address for native tokens (ETH, MATIC, etc.):Errors
| Code | Description |
|---|---|
NOT_INITIALIZED | Portal was not initialized |
SWAP_FAILED | The swap operation failed |
INSUFFICIENT_BALANCE | Not enough tokens to swap |
INVALID_API_KEY | The swaps API key is invalid |