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

# Swap Currency

> Swaps funds from one currency to another at the current exchange rate.

Swap funds from one currency to another at the current exchange rate. Use a `rateToken` from the `/wallet/rates` endpoint to lock in a specific rate.

## Example Request

```bash theme={null}
curl -X POST "https://api.rolla.xyz/api/v1/external/wallet/swap" \
  -H "X-API-Key: your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "fromCurrency": "USD",
    "toCurrency": "NGN",
    "fromAmount": 1000,
    "description": "Convert USD to NGN",
    "rateToken": "rt_abc123def456"
  }'
```

## Example Response

```json theme={null}
{
  "status": 200,
  "success": true,
  "message": "Swap completed successfully",
  "data": {
    "transaction": {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "transaction_type": "swap",
      "status": "completed",
      "description": "Convert USD to NGN",
      "external_reference": null,
      "fee_amount": 0,
      "source_amount": 1000,
      "destination_amount": 1405399,
      "source_currency": "USD",
      "destination_currency": "NGN",
      "exchange_rate": 1405.3992,
      "created_at": "2024-01-15T10:30:00.000Z",
      "updated_at": "2024-01-15T10:30:05.000Z"
    }
  }
}
```

<Warning>
  The `fromCurrency` and `toCurrency` must be different. If you don't provide a `rateToken`, the swap will execute at the current market rate which may differ from the last quoted rate.
</Warning>


## OpenAPI

````yaml POST /wallet/swap
openapi: 3.1.0
info:
  title: Rolla Developer API
  description: >-
    API for programmatic transaction management on the Rolla platform. Monetary
    `amount` fields use the smallest unit of the referenced currency unless an
    endpoint says otherwise (e.g. NGN = kobo, USD = cents, where **100 cents =
    USD 1.00**).
  version: 1.0.0
servers:
  - url: https://api.rolla.xyz/api/v1/external
    description: Production server
security:
  - apiKeyAuth: []
paths:
  /wallet/swap:
    post:
      summary: Swap Currency
      description: Swaps funds from one currency to another at the current exchange rate.
      operationId: swapCurrency
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SwapRequest'
      responses:
        '200':
          description: Swap completed successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  message:
                    type: string
                    example: Swap completed successfully
                  data:
                    type: object
                    properties:
                      transaction:
                        $ref: '#/components/schemas/Transaction'
                      fromAmount:
                        type: number
                        description: Amount debited from source currency
                        example: 1000
                      toAmount:
                        type: number
                        description: Amount credited to destination currency
                        example: 1550000
                      rate:
                        type: number
                        description: Exchange rate applied
                        example: 1550
        '400':
          description: Bad request - Invalid currencies or insufficient balance
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized - Invalid or missing API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    SwapRequest:
      type: object
      required:
        - fromCurrency
        - toCurrency
        - fromAmount
      properties:
        fromCurrency:
          type: string
          description: Source currency code
          minLength: 3
          maxLength: 3
          example: USD
        toCurrency:
          type: string
          description: Destination currency code (must differ from fromCurrency)
          minLength: 3
          maxLength: 3
          example: NGN
        fromAmount:
          type: number
          description: Amount to swap from source currency
          example: 1000
        description:
          type: string
          description: Optional description
          maxLength: 500
        rateToken:
          type: string
          description: Rate token from /wallet/rates to lock in a specific rate
    Transaction:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: Transaction identifier
        transaction_type:
          type: string
          enum:
            - deposit
            - withdrawal
            - swap
          description: Transaction type
        status:
          type: string
          enum:
            - pending
            - completed
            - failed
            - rejected
            - sent
            - processing
          description: Transaction status
        description:
          type: string
          description: Transaction description
        external_reference:
          type: string
          nullable: true
          description: External reference ID
        fee_amount:
          type: number
          description: >-
            Fee charged for this transaction. On a `fee` transaction this is `0`
            — the fee value is carried as that row's
            source_amount/destination_amount, so totalling fee_amount across
            transactions does not double-count it.
        fee_transaction_id:
          type: string
          format: uuid
          description: >-
            Where the fee is charged to a different account rather than deducted
            from this transaction, the id of the separate transaction it was
            booked as. Omitted when no such transaction exists.
        fee_reference:
          type: string
          description: >-
            Reference of that fee transaction, in the form `FEE-XXXXXXXX`.
            Omitted when no separate fee transaction exists.
          example: FEE-9J76UADV
        related_transaction_id:
          type: string
          format: uuid
          description: >-
            On a `fee` transaction, the id of the transaction the fee was
            charged for — the reverse of fee_transaction_id. Omitted on
            transactions that are not fees.
        related_reference:
          type: string
          description: >-
            Reference of that originating transaction. Omitted on transactions
            that are not fees.
        source_amount:
          type: number
          description: >-
            Amount debited from the source wallet. Normally amount + fee for a
            payout, but equal to the amount when the fee is charged to a
            separate account. Never derive the fee from this — read fee_amount.
        destination_amount:
          type: number
          description: Amount credited to the destination wallet
        source_currency:
          type: string
          description: Currency of the source wallet
        destination_currency:
          type: string
          description: Currency of the destination wallet
        created_at:
          type: string
          format: date-time
          description: Transaction creation timestamp
        updated_at:
          type: string
          format: date-time
          description: Last update timestamp
    Error:
      type: object
      properties:
        success:
          type: boolean
          example: false
        message:
          type: string
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
      description: Your Rolla API key

````