@swapkit/api-sdk-typescript 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/index.cjs +1018 -0
- package/dist/index.d.cts +2224 -0
- package/dist/index.d.ts +2224 -0
- package/dist/index.js +969 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 SwapKit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# @swapkit/api-sdk-typescript
|
|
2
|
+
|
|
3
|
+
Type-safe TypeScript SDK for the [SwapKit API](https://docs.swapkit.dev). Auto-generated from OpenAPI specs with named domain types and full request/response typing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @swapkit/api-sdk-typescript
|
|
9
|
+
# or
|
|
10
|
+
bun add @swapkit/api-sdk-typescript
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { configureSwapKit, SwapKitService } from "@swapkit/api-sdk-typescript";
|
|
17
|
+
|
|
18
|
+
// Configure once at startup
|
|
19
|
+
configureSwapKit({ apiKey: "your-api-key" });
|
|
20
|
+
|
|
21
|
+
// Use named service methods — fully typed
|
|
22
|
+
const { data: providers } = await SwapKitService.getProviders();
|
|
23
|
+
|
|
24
|
+
const { data: quote, error } = await SwapKitService.getQuote({
|
|
25
|
+
body: {
|
|
26
|
+
sellAsset: "ETH.ETH",
|
|
27
|
+
buyAsset: "BTC.BTC",
|
|
28
|
+
sellAmount: "1",
|
|
29
|
+
sourceAddress: "0x...",
|
|
30
|
+
destinationAddress: "bc1...",
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (error) {
|
|
35
|
+
console.error("Quote failed:", error);
|
|
36
|
+
} else {
|
|
37
|
+
console.log(`Got ${quote.routes.length} routes for quote ${quote.quoteId}`);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
### `configureSwapKit(options)`
|
|
44
|
+
|
|
45
|
+
| Option | Type | Default | Description |
|
|
46
|
+
|--------|------|---------|-------------|
|
|
47
|
+
| `apiKey` | `string` | **(required)** | Your SwapKit API key |
|
|
48
|
+
| `baseUrl` | `string` | `https://api.swapkit.dev` | API base URL override |
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// Production (default)
|
|
52
|
+
configureSwapKit({ apiKey: "sk_live_..." });
|
|
53
|
+
|
|
54
|
+
// Staging
|
|
55
|
+
configureSwapKit({
|
|
56
|
+
apiKey: "sk_test_...",
|
|
57
|
+
baseUrl: "https://dev-api.swapkit.dev",
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## SwapKitService
|
|
62
|
+
|
|
63
|
+
All API methods are available as static methods on the `SwapKitService` class:
|
|
64
|
+
|
|
65
|
+
| Method | HTTP | Endpoint |
|
|
66
|
+
|--------|------|----------|
|
|
67
|
+
| `getProviders()` | GET | `/providers` |
|
|
68
|
+
| `getTokens({ query? })` | GET | `/tokens` |
|
|
69
|
+
| `getSwapTo({ query })` | GET | `/swapTo` |
|
|
70
|
+
| `getSwapFrom({ query })` | GET | `/swapFrom` |
|
|
71
|
+
| `getQuote({ body })` | POST | `/v3/quote` |
|
|
72
|
+
| `getSwap({ body })` | POST | `/v3/swap` |
|
|
73
|
+
| `trackTransaction({ body })` | POST | `/track` |
|
|
74
|
+
| `screenAddress({ body })` | POST | `/screen` |
|
|
75
|
+
| `getGasPrices()` | GET | `/gas` |
|
|
76
|
+
| `getGasHistory({ query? })` | GET | `/gas/history` |
|
|
77
|
+
| `getCachedPrice({ body })` | POST | `/price/cached-price` |
|
|
78
|
+
| `createBrokerChannel({ body })` | POST | `/chainflip/broker/channel` |
|
|
79
|
+
|
|
80
|
+
### Examples
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Get available providers
|
|
84
|
+
const { data: providers } = await SwapKitService.getProviders();
|
|
85
|
+
|
|
86
|
+
// Get tokens for a provider
|
|
87
|
+
const { data: tokens } = await SwapKitService.getTokens({
|
|
88
|
+
query: { provider: "THORCHAIN" },
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Get available buy assets for a sell asset
|
|
92
|
+
const { data: buyAssets } = await SwapKitService.getSwapTo({
|
|
93
|
+
query: { sellAsset: "ETH.ETH" },
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Get a quote
|
|
97
|
+
const { data, error } = await SwapKitService.getQuote({
|
|
98
|
+
body: {
|
|
99
|
+
sellAsset: "ETH.ETH",
|
|
100
|
+
buyAsset: "BTC.BTC",
|
|
101
|
+
sellAmount: "1",
|
|
102
|
+
slippage: 3,
|
|
103
|
+
providers: ["THORCHAIN"],
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Execute a swap
|
|
108
|
+
const { data: swap } = await SwapKitService.getSwap({
|
|
109
|
+
body: {
|
|
110
|
+
routeId: route.routeId,
|
|
111
|
+
sourceAddress: "0xabc...",
|
|
112
|
+
destinationAddress: "bc1xyz...",
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Track a transaction
|
|
117
|
+
const { data: status } = await SwapKitService.trackTransaction({
|
|
118
|
+
body: { hash: "0xabc...", chainId: "1" },
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Screen an address
|
|
122
|
+
const { data: screen } = await SwapKitService.screenAddress({
|
|
123
|
+
body: { addresses: ["0xabc..."], chains: ["ETH"] },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Get gas prices
|
|
127
|
+
const { data: gas } = await SwapKitService.getGasPrices();
|
|
128
|
+
|
|
129
|
+
// Get cached token prices
|
|
130
|
+
const { data: prices } = await SwapKitService.getCachedPrice({
|
|
131
|
+
body: { tokens: [{ identifier: "ETH.ETH" }], metadata: true },
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Full Example: Quote-to-Swap Flow
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { configureSwapKit, SwapKitService } from "@swapkit/api-sdk-typescript";
|
|
139
|
+
|
|
140
|
+
configureSwapKit({ apiKey: "your-api-key" });
|
|
141
|
+
|
|
142
|
+
// 1. Get a quote
|
|
143
|
+
const { data: quote } = await SwapKitService.getQuote({
|
|
144
|
+
body: {
|
|
145
|
+
sellAsset: "ETH.ETH",
|
|
146
|
+
buyAsset: "BTC.BTC",
|
|
147
|
+
sellAmount: "1",
|
|
148
|
+
sourceAddress: "0xYourAddress...",
|
|
149
|
+
destinationAddress: "bc1YourBtcAddress...",
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (!quote) throw new Error("Quote failed");
|
|
154
|
+
|
|
155
|
+
// 2. Pick the best route
|
|
156
|
+
const route = quote.routes.find((r) => r.meta.tags.includes("RECOMMENDED")) ?? quote.routes[0];
|
|
157
|
+
|
|
158
|
+
// 3. Execute the swap
|
|
159
|
+
const { data: swap } = await SwapKitService.getSwap({
|
|
160
|
+
body: {
|
|
161
|
+
routeId: route.routeId,
|
|
162
|
+
sourceAddress: "0xYourAddress...",
|
|
163
|
+
destinationAddress: "bc1YourBtcAddress...",
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// 4. Track the transaction
|
|
168
|
+
const { data: status } = await SwapKitService.trackTransaction({
|
|
169
|
+
body: { hash: "0xBroadcastedTxHash...", chainId: "1" },
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
console.log(`Status: ${status?.status}`);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Direct Function Access
|
|
176
|
+
|
|
177
|
+
You can also import SDK functions directly instead of using the service class:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { postV3Quote, postV3Swap, postTrack } from "@swapkit/api-sdk-typescript";
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
### Regenerate types
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
bun run extract:sdk
|
|
189
|
+
LOCAL_SPECS_DIR=/tmp/swapkit-specs bun run generate:sdk
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Run tests
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
bun run test:sdk
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Build for publishing
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
bun run build:sdk
|
|
202
|
+
```
|