Lightweight TypeScript client for the Layerswap API v2. Zero dependencies, uses native fetch.
- 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
npm install @layerswap/sdkimport { 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...const api = new LayerSwapApi({
apiKey: 'your-api-key', // required
baseUrl: 'https://api.layerswap.io', // optional, default shown
});// 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',
});// 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,
});// 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');// Health check
await api.health();
// Transaction status
const status = await api.getTransactionStatus({
network: 'ETHEREUM_MAINNET',
transactionId: '0x...',
});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);
}
}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';Apache-2.0