VeilSwap
DocsSDK

SDK

Official VeilSwap SDK for JavaScript/TypeScript applications

Installation

npm install @veilswap/sdk
# or
yarn add @veilswap/sdk
# or
pnpm add @veilswap/sdk

Quick Start

import { VeilSwap } from '@veilswap/sdk'

// Initialize client (no API key needed for basic usage)
const veilswap = new VeilSwap()

// Get available currencies
const currencies = await veilswap.getCurrencies()
console.log(currencies)

// Get exchange estimate
const estimate = await veilswap.getEstimate({
  fromCurrency: 'btc',
  toCurrency: 'xmr',
  fromAmount: 0.1
})
console.log(`You will receive: ${estimate.toAmount} XMR`)

// Create an exchange
const exchange = await veilswap.createExchange({
  fromCurrency: 'btc',
  toCurrency: 'xmr',
  fromAmount: 0.1,
  address: 'your_xmr_address',
  refundAddress: 'your_btc_address' // optional
})

console.log(`Send BTC to: ${exchange.depositAddress}`)
console.log(`Exchange ID: ${exchange.id}`)

// Check status
const status = await veilswap.getExchangeStatus(exchange.id)
console.log(`Status: ${status.status}`)

TypeScript Support

The SDK is written in TypeScript and includes full type definitions.

import { 
  VeilSwap, 
  Currency, 
  Estimate, 
  Exchange, 
  ExchangeStatus 
} from '@veilswap/sdk'

const veilswap = new VeilSwap()

// All responses are fully typed
const currencies: Currency[] = await veilswap.getCurrencies()

const estimate: Estimate = await veilswap.getEstimate({
  fromCurrency: 'btc',
  toCurrency: 'eth',
  fromAmount: 1
})

const exchange: Exchange = await veilswap.createExchange({
  fromCurrency: 'btc',
  toCurrency: 'eth',
  fromAmount: 1,
  address: '0x...'
})

const status: ExchangeStatus = await veilswap.getExchangeStatus(exchange.id)

React Hook

For React applications, we provide a convenient hook:

import { useVeilSwap } from '@veilswap/sdk/react'

function SwapComponent() {
  const { 
    currencies, 
    estimate, 
    exchange,
    createExchange,
    isLoading,
    error 
  } = useVeilSwap()

  const handleSwap = async () => {
    const result = await createExchange({
      fromCurrency: 'btc',
      toCurrency: 'xmr',
      fromAmount: 0.1,
      address: 'xmr_address'
    })
    console.log('Deposit to:', result.depositAddress)
  }

  return (
    <button onClick={handleSwap} disabled={isLoading}>
      {isLoading ? 'Creating...' : 'Create Swap'}
    </button>
  )
}

Error Handling

import { VeilSwap, VeilSwapError } from '@veilswap/sdk'

const veilswap = new VeilSwap()

try {
  const exchange = await veilswap.createExchange({
    fromCurrency: 'btc',
    toCurrency: 'xmr',
    fromAmount: 0.0001, // Too small
    address: 'invalid_address'
  })
} catch (error) {
  if (error instanceof VeilSwapError) {
    console.error(`Error code: ${error.code}`)
    console.error(`Message: ${error.message}`)
    
    switch (error.code) {
      case 'AMOUNT_TOO_SMALL':
        console.log('Please increase the amount')
        break
      case 'INVALID_ADDRESS':
        console.log('Check the destination address')
        break
      default:
        console.log('An error occurred')
    }
  }
}