Skip to content

Repository files navigation

@swapkit/api-sdk-typescript

Type-safe TypeScript SDK for the SwapKit API. Auto-generated from OpenAPI specs with named domain types and full request/response typing.

Installation

npm install @swapkit/api-sdk-typescript
# or
bun add @swapkit/api-sdk-typescript

For the bleeding-edge build tracking the API's develop branch, install the nightly tag:

npm install @swapkit/api-sdk-typescript@nightly

Quick Start

import{configureSwapKit,SwapKitService}from"@swapkit/api-sdk-typescript";// Configure once at startupconfigureSwapKit({apiKey: "your-api-key"});// Use named service methods — fully typedconst{data: providers}=awaitSwapKitService.getProviders();const{data: quote, error }=awaitSwapKitService.getQuote({body: {sellAsset: "ETH.ETH",buyAsset: "BTC.BTC",sellAmount: "1",sourceAddress: "0x...",destinationAddress: "bc1...",},});if(error){console.error("Quote failed:",error);}else{console.log(`Got ${quote.routes.length} routes for quote ${quote.quoteId}`);}

Configuration

configureSwapKit(options)

OptionTypeDefaultDescription
apiKeystring(required)Your SwapKit API key
baseUrlstringhttps://api.swapkit.devAPI base URL override
// Production (default)configureSwapKit({apiKey: "sk_live_..."});// StagingconfigureSwapKit({apiKey: "sk_test_...",baseUrl: "https://dev-api.swapkit.dev",});

SwapKitService

All API methods are available as static methods on the SwapKitService class:

MethodHTTPEndpoint
getProviders()GET/providers
getTokens({ query? })GET/tokens
getSwapTo({ query })GET/swapTo
getSwapFrom({ query })GET/swapFrom
getQuote({ body })POST/v3/quote
getSwap({ body })POST/v3/swap
trackTransaction({ body })POST/track
screenAddress({ body })POST/screen
getGasPrices()GET/gas
getGasHistory({ query? })GET/gas/history
getCachedPrice({ body })POST/price/cached-price
createBrokerChannel({ body })POST/chainflip/broker/channel

Examples

// Get available providersconst{data: providers}=awaitSwapKitService.getProviders();// Get tokens for a providerconst{data: tokens}=awaitSwapKitService.getTokens({query: {provider: "THORCHAIN"},});// Get available buy assets for a sell assetconst{data: buyAssets}=awaitSwapKitService.getSwapTo({query: {sellAsset: "ETH.ETH"},});// Get a quoteconst{ data, error }=awaitSwapKitService.getQuote({body: {sellAsset: "ETH.ETH",buyAsset: "BTC.BTC",sellAmount: "1",slippage: 3,providers: ["THORCHAIN"],},});// Execute a swapconst{data: swap}=awaitSwapKitService.getSwap({body: {routeId: route.routeId,sourceAddress: "0xabc...",destinationAddress: "bc1xyz...",},});// Track a transactionconst{data: status}=awaitSwapKitService.trackTransaction({body: {hash: "0xabc...",chainId: "1"},});// Screen an addressconst{data: screen}=awaitSwapKitService.screenAddress({body: {addresses: ["0xabc..."],chains: ["ETH"]},});// Get gas pricesconst{data: gas}=awaitSwapKitService.getGasPrices();// Get cached token pricesconst{data: prices}=awaitSwapKitService.getCachedPrice({body: {tokens: [{identifier: "ETH.ETH"}],metadata: true},});

Full Example: Quote-to-Swap Flow

import{configureSwapKit,SwapKitService}from"@swapkit/api-sdk-typescript";configureSwapKit({apiKey: "your-api-key"});// 1. Get a quoteconst{data: quote}=awaitSwapKitService.getQuote({body: {sellAsset: "ETH.ETH",buyAsset: "BTC.BTC",sellAmount: "1",sourceAddress: "0xYourAddress...",destinationAddress: "bc1YourBtcAddress...",},});if(!quote)thrownewError("Quote failed");// 2. Pick the best routeconstroute=quote.routes.find((r)=>r.meta.tags.includes("RECOMMENDED"))??quote.routes[0];// 3. Execute the swapconst{data: swap}=awaitSwapKitService.getSwap({body: {routeId: route.routeId,sourceAddress: "0xYourAddress...",destinationAddress: "bc1YourBtcAddress...",},});// 4. Track the transactionconst{data: status}=awaitSwapKitService.trackTransaction({body: {hash: "0xBroadcastedTxHash...",chainId: "1"},});console.log(`Status: ${status?.status}`);

Direct Function Access

You can also import SDK functions directly instead of using the service class:

import{postV3Quote,postV3Swap,postTrack}from"@swapkit/api-sdk-typescript";

Development

Regenerate types

bun run extract:sdk
LOCAL_SPECS_DIR=/tmp/swapkit-specs bun run generate:sdk

Run tests

bun run test:sdk

Build for publishing

bun run build:sdk

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages