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

# Solana 兑换报价：Swap API

> 用 GET /rate 获取 Solana 代币兑换报价，返回预期输出数量和价格影响。请在兑换前使用，不要当作价格 API。

> [!NOTE]
>
> 本页介绍的是较早的 Solana Tracker Swap API（`swap-v2.solanatracker.io`）。它仍在运行，每笔兑换收取平台费。[Raptor](https://docs.solanatracker.io/cn/raptor/overview.md) 是另一款独立的兑换 API（`raptor.solanatracker.io`），目前免费，无需 API 密钥。

## 概览

rate 端点为代币兑换提供执行交易前的价格报价。用它在用户即将兑换时向其显示预期输出量和价格影响。

> [!WARNING]
>
> 此端点仅用于兑换前报价。不要将其用作免费的定价或市场数据 API。出于非兑换目的的过多请求可能导致速率限制。

## 用例

* **兑换前报价**:在执行兑换前获取准确定价
* **滑点计算**:向用户显示最低保证输出
* **价格影响检查**:验证交易规模是否可接受

## SDK 示例

```javascript JavaScript theme={null}
import { SolanaTracker } from 'solana-swap';

const tracker = new SolanaTracker('YOUR_API_KEY');

// Get rate quote before swap
const quote = await tracker.getRate({
  from: 'So11111111111111111111111111111111111111112', // SOL
  to: '4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R',
  amount: 1,
  slippage: 10
});

console.log(`Expected: ${quote.amountOut} tokens`);
console.log(`Minimum: ${quote.minAmountOut} tokens`);
console.log(`Price impact: ${(quote.priceImpact * 100).toFixed(2)}%`);
```

在这些 SDK 示例中,`slippage: 10` 表示 10%。

```python Python theme={null}
from solana_swap import SolanaTracker

tracker = SolanaTracker('YOUR_API_KEY')

# Get rate quote before swap
quote = tracker.get_rate(
    from_token='So11111111111111111111111111111111111111112',
    to_token='4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R',
    amount=1,
    slippage=10
)

print(f"Expected: {quote['amountOut']} tokens")
print(f"Minimum: {quote['minAmountOut']} tokens")
print(f"Price impact: {quote['priceImpact'] * 100:.2f}%")
```

```bash cURL theme={null}
curl -X GET "https://swap-v2.solanatracker.io/rate?from=So11111111111111111111111111111111111111112&to=4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R&amount=1&slippage=10"
```

## 响应字段

| 字段 | 类型 | 描述 |
| - | - | - |
| `amountIn` | number | 输入代币数量 |
| `amountOut` | number | 预期输出代币数量 |
| `minAmountOut` | number | 滑点后最低输出 |
| `currentPrice` | number | 当前市场价格 |
| `executionPrice` | number | 实际执行价格 |
| `priceImpact` | number | 价格影响(小数,0.05 = 5%) |
| `fee` | number | 交易费用 |
| `platformFee` | number | 平台费(lamports) |
| `platformFeeUI` | number | 平台费(SOL) |

### 示例响应

```json theme={null}
{
  "amountIn": 1,
  "amountOut": 9181.330823048,
  "minAmountOut": 9089.517514818,
  "currentPrice": 9181.330823048,
  "executionPrice": 9089.517514818,
  "priceImpact": 0.0334641736518774,
  "fee": 0.01,
  "baseCurrency": {
    "decimals": 9,
    "mint": "So11111111111111111111111111111111111111112"
  },
  "quoteCurrency": {
    "decimals": 9,
    "mint": "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"
  },
  "platformFee": 9000000,
  "platformFeeUI": 0.009
}
```

## 理解价格影响

价格影响显示您的交易使市场价格变动多少:

* **\< 1%** - 流动性极好,可放心交易
* **1-5%** - 大多数交易可接受
* **> 5%** - 考虑较小的交易、不同的路径或等待更多流动性

## 在兑换前验证

```javascript theme={null}
// Check rate before executing swap
async function validateAndSwap(swapParams) {
  // Get quote first
  const quote = await tracker.getRate({
    from: swapParams.from,
    to: swapParams.to,
    amount: swapParams.amount,
    slippage: swapParams.slippage
  });
  
  // Check price impact
  if (quote.priceImpact > 0.05) {
    throw new Error('Price impact too high');
  }
  
  // Execute swap
  return await tracker.swap(swapParams);
}
```

## 常见错误

| 错误 | 描述 | 解决方案 |
| - | - | - |
| `Invalid or missing token address` | 代币地址无效 | 验证代币地址 |
| `Invalid amount` | 金额无效 | 确保金额为正数 |
| `Invalid slippage tolerance` | 滑点不在 0-100 之间 | 将滑点设为 0-100 |
| `Unable to fetch pools` | 找不到流动性 | 代币可能缺少活跃池 |

## 下一步

> [card] [Swap 端点](https://docs.solanatracker.io/cn/swap-api/swap.md)
>
> 执行兑换交易

> [card] [JavaScript SDK](https://github.com/YZYLAB/solana-swap)
>
> 安装 SDK


## OpenAPI

````yaml openapi.json GET /rate
openapi: 3.1.0
info:
  title: Solana Tracker - Swap API
  description: >-
    The Solana Tracker Swap API offers seamless, zero-delay access to Solana's
    dynamic token marketplace. Trade tokens the moment they launch with our
    high-performance infrastructure. Supports Pump.fun, PumpSwap, Moonshot,
    Boop, Orca, Meteora, Raydium, and any pool supported by Jupiter.
  version: 2.0.0
  contact:
    name: Solana Tracker Support
    email: swap-api@solanatracker.io
    url: https://discord.com/invite/JH2e9rR9fc
  x-fees:
    standard: 0.5%
    highVolume: 0-0.5%
servers:
  - url: https://swap-v2.solanatracker.io
    description: Production server
security: []
tags:
  - name: Swap
    description: Token swap operations
  - name: Rate
    description: Rate and price quote operations
externalDocs:
  description: Full API Documentation
  url: https://docs.solanatracker.io/swap-api
paths:
  /rate:
    get:
      summary: Get Swap Quote
      description: Get live swap rates for any Solana token.
      operationId: getRate
      parameters:
        - name: from
          in: query
          required: true
          description: The base token address
          schema:
            type: string
            example: So11111111111111111111111111111111111111112
        - name: to
          in: query
          required: true
          description: The quote token address
          schema:
            type: string
            example: 4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R
        - name: amount
          in: query
          required: true
          description: >-
            The amount of the base token to convert (in native units, not
            lamports)
          schema:
            type: number
            example: 1
        - name: slippage
          in: query
          required: true
          description: The maximum acceptable slippage percentage
          schema:
            type: number
            minimum: 0
            maximum: 100
            example: 10
      responses:
        '200':
          description: Successful rate quote
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RateResponse'
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    RateResponse:
      type: object
      properties:
        amountIn:
          type: number
          description: The amount of the source token used for the conversion
          example: 1
        amountOut:
          type: number
          description: The amount of the destination token that would be received
          example: 9181.330823048
        minAmountOut:
          type: number
          description: The minimum amount of the destination token after applying slippage
          example: 9089.517514818
        currentPrice:
          type: number
          description: The current market price for the token pair
          example: 9181.330823048
        executionPrice:
          type: number
          description: The actual price at which the trade would be executed
          example: 9089.517514818
        priceImpact:
          type: number
          description: >-
            The difference between market price and execution price as a
            fraction
          example: 0.0334641736518774
        fee:
          type: number
          description: The trading fee charged for the transaction
          example: 0.01
        baseCurrency:
          type: object
          properties:
            decimals:
              type: integer
              description: Base token decimals
              example: 9
            mint:
              type: string
              description: Base token mint address
              example: So11111111111111111111111111111111111111112
        quoteCurrency:
          type: object
          properties:
            decimals:
              type: integer
              description: Quote token decimals
              example: 9
            mint:
              type: string
              description: Quote token mint address
              example: 4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R
        platformFee:
          type: integer
          description: The fee charged by the platform in lamports
          example: 9000000
        platformFeeUI:
          type: number
          description: The platform fee in SOL
          example: 0.009
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: Error message
          examples:
            - An internal error occurred
            - Invalid or missing 'from' or 'to' token address
            - Invalid amount
            - Invalid slippage tolerance (should be between 0 and 100%)
        details:
          type: string
          description: Additional error details
          examples:
            - Unable to fetch pools for token
            - Token has no pools
            - No pools with liquidity found

````

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