Skip to content

layerswap/layerswap-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@layerswap/sdk

NPM version npm bundle size

Lightweight TypeScript client for the Layerswap API v2. Zero dependencies, uses native fetch.

Features

  • Zero production dependencies
  • Works on Node 18+, Deno, Bun, browsers, Cloudflare Workers
  • Full TypeScript types for all requests and responses
  • Covers all 14 Layerswap API v2 endpoints
  • ESM and CommonJS support

Installation

npm install @layerswap/sdk

Quick Start

import { LayerSwapApi } from '@layerswap/sdk';

const api = new LayerSwapApi({
  apiKey: 'your-api-key',
});

// Get a quote
const quote = await api.getQuote({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
  amount: 0.1,
});

console.log(quote.receive_amount); // 0.099...

API Reference

Configuration

const api = new LayerSwapApi({
  apiKey: 'your-api-key',       // required
  baseUrl: 'https://api.layerswap.io', // optional, default shown
});

Route Discovery

// Get all available networks
const networks = await api.getNetworks();
const evmNetworks = await api.getNetworks({ networkTypes: ['evm'] });

// Get source networks/tokens for a destination
const sources = await api.getSources({
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
});

// Get destination networks/tokens for a source
const destinations = await api.getDestinations({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
});

Quotes & Limits

// Get transfer limits
const limits = await api.getLimits({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
  amount: 1,
});
console.log(limits.min_amount, limits.max_amount);

// Get a quote
const quote = await api.getQuote({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
  amount: 0.1,
});
console.log(quote.receive_amount, quote.total_fee);

// Get detailed quote with routing info
const detailed = await api.getDetailedQuote({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
  amount: 0.1,
});

Swap Lifecycle

// Create a swap
const swap = await api.createSwap({
  sourceNetwork: 'ETHEREUM_MAINNET',
  sourceToken: 'ETH',
  destinationNetwork: 'STARKNET_MAINNET',
  destinationToken: 'ETH',
  amount: 0.1,
  destinationAddress: '0x...',
});
console.log(swap.swap.id, swap.deposit_actions);

// Get swap details
const details = await api.getSwap('swap-id');

// Get swap by transaction hash
const byTx = await api.getSwapByTransactionHash('0x...');

// List swaps for an address
const swaps = await api.getSwaps({ address: '0x...' });

// Get deposit actions
const actions = await api.getDepositActions('swap-id', '0xsourceAddress');

// Speed up deposit detection
await api.speedUpDeposit('swap-id', '0xtransactionHash');

Status

// Health check
await api.health();

// Transaction status
const status = await api.getTransactionStatus({
  network: 'ETHEREUM_MAINNET',
  transactionId: '0x...',
});

Error Handling

All API errors throw LayerSwapApiError with statusCode, errorCode, and message:

import { LayerSwapApi, LayerSwapApiError } from '@layerswap/sdk';

try {
  await api.getQuote({ /* ... */ });
} catch (err) {
  if (err instanceof LayerSwapApiError) {
    console.log(err.statusCode); // 400, 403, 404, etc.
    console.log(err.errorCode);  // e.g. "BAD_REQUEST"
    console.log(err.message);
  }
}

Types

All types are exported and available for use:

import type {
  LsNetwork,
  LsToken,
  LsRoute,
  LsQuote,
  LsSwap,
  LsSwapResponse,
  LsDepositAction,
  LsLimits,
  LsDetailedQuote,
  LsTransactionStatus,
  LayerSwapQuoteRequest,
  LayerSwapCreateRequest,
} from '@layerswap/sdk';

License

Apache-2.0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors