Node.js Backend Integration
How to call Comet Engine from a Node.js/TypeScript service.
Axios Client
typescript
import axios, { AxiosInstance } from "axios";
class CometClient {
private http: AxiosInstance;
constructor(baseURL: string, apiKey: string, apiSecret: string) {
this.http = axios.create({
baseURL,
timeout: 30_000,
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-API-Secret": apiSecret,
},
});
}
async createWallet(userId: number) {
const { data } = await this.http.post("/api/v1/wallets/create", {
userId,
});
return data;
}
async getBalance(userId: number) {
const { data } = await this.http.get("/api/v1/wallets/balance", {
params: { userId },
});
return data;
}
async mintTokens(
symbol: string,
userId: number,
amountBase: string,
externalId: string
) {
const { data } = await this.http.post(
`/api/v1/assets/${symbol.toLowerCase()}/mint`,
{ userId, amountBase, externalId }
);
return data;
}
async swapQuote(from: string, to: string, amountIn: string) {
const { data } = await this.http.get("/api/v1/swap/quote", {
params: { from, to, amountIn },
});
return data;
}
async swapTokens(
userId: number,
from: string,
to: string,
amountIn: string
) {
const { data } = await this.http.post("/api/v1/swap/tokens", {
userId,
from,
to,
amountIn,
});
return data;
}
async listPools() {
const { data } = await this.http.get("/api/v1/swap/pools");
return data;
}
}Usage
typescript
const comet = new CometClient(
"http://localhost:8100",
"your-api-key",
"your-api-secret"
);
// Create wallet (tenant is inferred from API key)
const wallet = await comet.createWallet(12345);
// Mint 100 USDT
await comet.mintTokens("USDT", 12345, "100000000", "mint-001");
// Swap 50 USDT → IMC
const result = await comet.swapTokens(12345, "USDT", "IMC", "50000000");
console.log(`Swap tx: ${result.txHash}`);TypeScript Types
typescript
interface CometResponse<T> {
status: "success" | "error";
data?: T;
error?: string;
}
interface Wallet {
address: string;
chains: string[];
existing?: boolean;
message?: string;
}
interface Balance {
address: string;
nativeWei: string;
[key: string]: string; // token balances: usdtBaseUnits, etc.
}
interface SwapQuote {
from: string;
to: string;
amountIn: string;
amounts: string[];
amountOut: string;
}
interface PoolInfo {
token0: string;
token1: string;
pair: string;
reserve0: string;
reserve1: string;
}