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

# 交易执行（EVM + Solana）

> 使用先前的报价或内联构建在 EVM 或 Solana 上执行代币兑换。

<Note>
  使用 `NATIVE` 关键字于 `fromToken`/`toToken` 来交换原生资产（ETH、SOL 等）。需要 `Idempotency-Key` 头。仅 Solana 主网。对于 EVM，如兑换因 allowance 错误失败，请先批准代币。
</Note>

<ResponseExample>
  ```json 200 theme={null}
  { "id": "tx_...", "status": "SUBMITTED", "hash": "0x...", "explorerUrl": "https://solscan.io/tx/...", "chainName": "Solana" }
  ```
</ResponseExample>


## OpenAPI

````yaml POST /trade/swap
openapi: 3.1.0
info:
  title: AFK Crypto API
  description: >-
    AFK Crypto - Non-custodial wallet automation. Simple REST APIs for EVM and
    Solana.
  version: 1.0.0
servers:
  - url: https://api.afkcrypto.com/v1
    description: Production
security:
  - apiKeyAuth: []
paths:
  /trade/swap:
    post:
      summary: Execute trade via aggregator (EVM + Solana)
      description: Execute a token swap on EVM or Solana using inline build.
      parameters:
        - name: Idempotency-Key
          in: header
          required: true
          schema:
            type: string
            format: uuid
          description: >-
            Provide a unique Idempotency-Key per user to ensure single
            submission.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TradeSwapRequest'
      responses:
        '200':
          description: Submission accepted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SubmissionResponse'
          headers:
            x-request-id:
              $ref: '#/components/headers/XRequestId'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
          headers:
            x-request-id:
              $ref: '#/components/headers/XRequestId'
        '403':
          description: Forbidden
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
          headers:
            x-request-id:
              $ref: '#/components/headers/XRequestId'
        '429':
          description: Rate limited
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
          headers:
            x-request-id:
              $ref: '#/components/headers/XRequestId'
            Retry-After:
              schema:
                type: integer
              description: Seconds until next window
        default:
          $ref: '#/components/responses/ErrorResponse'
components:
  schemas:
    TradeSwapRequest:
      oneOf:
        - $ref: '#/components/schemas/EvmTradeSwapInlineRequest'
        - $ref: '#/components/schemas/SolanaTradeSwapInlineRequest'
    SubmissionResponse:
      type: object
      properties:
        id:
          type: string
          description: AFK transaction id for the submitted action.
        status:
          type: string
          enum:
            - SUBMITTED
          description: Submission status.
        hash:
          type:
            - string
            - 'null'
          description: >-
            On-chain transaction hash/signature (may be null at submission
            time).
        explorerUrl:
          type:
            - string
            - 'null'
          description: >-
            Direct link to the transaction on a chain explorer. Null until a
            hash/signature exists.
        chainName:
          type: string
          description: >-
            Human-readable chain name for display (e.g., Base, Ethereum,
            Solana).
      required:
        - id
        - status
        - hash
        - explorerUrl
        - chainName
    ApiError:
      type: object
      properties:
        error:
          type: string
          description: Human-readable error message.
        code:
          type: string
          enum:
            - FORBIDDEN
            - UNAUTHORIZED
            - RATE_LIMITED
            - INVALID_INPUT
            - WALLET_NOT_FOUND
            - USER_NOT_LINKED
            - WALLET_NOT_DELEGATED
            - WALLET_ID_REQUIRED
            - CHAIN_UNKNOWN
            - IDEMPOTENT_BODY_MISMATCH
            - IDEMPOTENT_REPLAY
            - PRIVY_RPC_ERROR
            - SEND_FAILED
            - SERVICE_UNAVAILABLE
            - QUOTE_FAILED
            - NOT_FOUND
            - INSUFFICIENT_FUNDS
            - INTERNAL_ERROR
          description: Stable error code for programmatic handling.
        requestId:
          type:
            - string
            - 'null'
          description: Unique request identifier (also in x-request-id header).
      required:
        - error
    EvmTradeSwapInlineRequest:
      title: EVM
      type: object
      properties:
        chain:
          type: string
          description: EVM chain slug.
        from:
          type:
            - string
            - 'null'
          description: >-
            Optional sender wallet; defaults to user's default wallet for the
            chain.
        fromToken:
          type: string
          description: Token or native address (0xEeee... for native)
        toToken:
          type: string
          description: Output token address (0x...).
        amount:
          type: string
          description: Amount in wei (decimal or 0x-hex supported for EVM).
        slippage:
          type: number
          description: Percentage (e.g., 1 = 1%). Optional; defaults to 1%.
          default: 1
        includeGas:
          type:
            - boolean
            - 'null'
          description: Include gas estimates in the build (optional).
        protocols:
          type:
            - string
            - 'null'
          description: Comma-separated allowlist of protocols (optional).
        excludeProtocols:
          type:
            - string
            - 'null'
          description: Comma-separated blocklist of protocols (optional).
      required:
        - chain
        - fromToken
        - toToken
        - amount
    SolanaTradeSwapInlineRequest:
      title: Solana
      type: object
      properties:
        chain:
          type: string
          description: Solana chain slug (solana, solana-testnet, solana-devnet).
        from:
          type:
            - string
            - 'null'
          description: Optional sender public key; improves route calculation.
        fromToken:
          type: string
          description: Input mint address.
        toToken:
          type: string
          description: Output mint address.
        amount:
          type: string
          description: Amount in base units (SPL token units).
        slippage:
          type: number
          description: Percentage (e.g., 1 = 1%). Optional; defaults to 1%.
          default: 1
      required:
        - chain
        - fromToken
        - toToken
        - amount
  headers:
    XRequestId:
      description: Unique request identifier
      schema:
        type: string
  responses:
    ErrorResponse:
      description: Error envelope
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiError'
      headers:
        x-request-id:
          $ref: '#/components/headers/XRequestId'
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key

````