Demo Code
npm install viemexport TRADING_API_KEY="your-api-key"
export TRADING_API_SECRET="your-api-secret"
export TRADING_USER_PRIVATE_KEY="0x..." # self-submit / One Click user walletAPI client with HMAC
import crypto from "node:crypto";
const BASE_URL = "https://mainnet-api.monday.trade/rwa/trading";
type HttpMethod = "GET" | "POST" | "DELETE";
interface TradingApiConfig {
apiKey: string;
apiSecret: string;
chainId: number;
productType: "MondayTrade";
}
function canonicalUri(path: string, query: Record<string, string | string[] | undefined> = {}): string {
const pairs: string[] = [];
for (const key of Object.keys(query).sort()) {
const value = query[key];
if (value === undefined || value === null) continue;
if (Array.isArray(value)) {
for (const item of value) pairs.push(`${key}=${item}`);
} else {
pairs.push(`${key}=${value}`);
}
}
return pairs.length === 0 ? path : `${path}?${pairs.join("&")}`;
}
function signTradingApi(
apiSecret: string,
method: HttpMethod,
path: string,
query: Record<string, string | string[] | undefined>,
timestamp: number,
nonce: string,
rawBody: string,
): string {
const uri = canonicalUri(path, query);
const payload = [method, uri, String(timestamp), nonce, rawBody].join("\n");
return crypto.createHmac("sha256", apiSecret).update(payload, "utf8").digest("hex");
}
async function tradingApiRequest<T>(
config: TradingApiConfig,
method: HttpMethod,
path: string,
options: {
query?: Record<string, string | string[] | undefined>;
body?: unknown;
} = {},
): Promise<T> {
const query = options.query ?? {};
const rawBody = options.body === undefined ? "" : JSON.stringify(options.body);
const timestamp = Date.now();
const nonce = crypto.randomUUID();
const signature = signTradingApi(config.apiSecret, method, path, query, timestamp, nonce, rawBody);
const url = new URL(`${BASE_URL}${canonicalUri(path, query)}`);
const response = await fetch(url, {
method,
headers: {
"content-type": "application/json",
"x-api-key": config.apiKey,
"x-api-ts": String(timestamp),
"x-api-nonce": nonce,
"x-api-sign": signature,
"x-api-chain-id": String(config.chainId),
"x-api-p": config.productType,
},
body: method === "GET" || method === "DELETE" ? undefined : rawBody,
});
const json = await response.json() as { code?: number; errMsg?: string; data?: T };
if (json.code !== 200) {
throw new Error(json.errMsg || `API error ${json.code}`);
}
return json.data as T;
}Self-submit market buy (viem)
Deposit with ERC-20 approve (viem)
Order with deposit (self-submit)
Stock deposit and withdrawal (self-submit)
One Click enable (viem signTypedData)
One Click order (via /send)
/send)Query portfolio
Query corporate actions
Last updated

