Skip to main content

Overview

The requests module provides async functions for interacting with the Exchange Web REST API. All functions use axios for HTTP requests and return typed responses.

Base URL

const BASE_URL = "https://exchange.jogeshwar.xyz/backend/api/v1";

Market Data Functions

getDepth()

Retrieves the current order book depth for a specific market.
async function getDepth(market: string): Promise<Depth>
market
string
required
Market symbol (e.g., “SOL_USDCT”, “SOL_USDCT”)
return
Depth
Order book depth data
  • bids (array): Array of [price, quantity] tuples for buy orders
  • asks (array): Array of [price, quantity] tuples for sell orders
  • lastUpdateId (string): Sequential update identifier

Example

import { getDepth } from "@/utils/requests";

const depth = await getDepth("SOL_USDCT");
console.log("Top bid:", depth.bids[0]); // ["50000.00", "1.5"]
console.log("Top ask:", depth.asks[0]); // ["50001.00", "2.0"]

getTrades()

Retrieves recent trades for a specific market.
async function getTrades(market: string): Promise<Trade[]>
market
string
required
Market symbol (e.g., “SOL_USDCT”, “SOL_USDCT”)
return
Trade[]
Array of recent trade objects:
  • id (number): Unique trade identifier
  • isBuyerMaker (boolean): True if buyer initiated the trade
  • price (string): Trade execution price
  • quantity (string): Trade quantity
  • quoteQuantity (string): Quote asset quantity (price × quantity)
  • timestamp (number): Unix timestamp in milliseconds

Example

import { getTrades } from "@/utils/requests";

const trades = await getTrades("SOL_USDCT");
trades.forEach((trade) => {
  console.log(`${trade.quantity} @ ${trade.price}`);
});

getKlines()

Retrieves candlestick (K-line) data for a specific market and time range.
async function getKlines(
  market: string,
  interval: string,
  startTime: number
): Promise<KLine[]>
market
string
required
Market symbol (e.g., “SOL_USDCT”, “SOL_USDCT”)
interval
string
required
Candlestick interval (e.g., “1m”, “5m”, “1h”, “1d”)
startTime
number
required
Start time in Unix timestamp (milliseconds)
return
KLine[]
Array of candlestick data sorted by end time:
  • open (string): Opening price
  • high (string): Highest price
  • low (string): Lowest price
  • close (string): Closing price
  • volume (string): Base asset volume
  • quoteVolume (string): Quote asset volume
  • start (string): Candle start time
  • end (string): Candle end time
  • trades (string): Number of trades in this candle
The response is automatically sorted by end time in ascending order.

Example

import { getKlines } from "@/utils/requests";

const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;
const klines = await getKlines("SOL_USDCT", "1h", oneDayAgo);

klines.forEach((candle) => {
  console.log(`Open: ${candle.open}, Close: ${candle.close}`);
});

getTicker()

Retrieves 24-hour ticker data for a specific market.
async function getTicker(market: string): Promise<Ticker>
market
string
required
Market symbol (e.g., “SOL_USDCT”, “SOL_USDCT”)
return
Ticker
24-hour ticker statistics:
  • symbol (string): Market symbol
  • firstPrice (string): Price 24 hours ago
  • lastPrice (string): Current price
  • high (string): Highest price in 24h
  • low (string): Lowest price in 24h
  • priceChange (string): Absolute price change
  • priceChangePercent (string): Percentage price change
  • volume (string): Base asset volume
  • quoteVolume (string): Quote asset volume
  • trades (string): Number of trades
This function calls getTickers() internally and filters for the specific market.

Example

import { getTicker } from "@/utils/requests";

const ticker = await getTicker("SOL_USDCT");
console.log(`Current price: ${ticker.lastPrice}`);
console.log(`24h change: ${ticker.priceChangePercent}%`);

getTickers()

Retrieves 24-hour ticker data for all markets.
async function getTickers(): Promise<Ticker[]>
return
Ticker[]
Array of ticker objects for all available markets (see Ticker structure above)

Example

import { getTickers } from "@/utils/requests";

const tickers = await getTickers();
tickers.forEach((ticker) => {
  console.log(`${ticker.symbol}: ${ticker.lastPrice}`);
});

// Find top gainers
const topGainers = tickers
  .sort((a, b) => parseFloat(b.priceChangePercent) - parseFloat(a.priceChangePercent))
  .slice(0, 5);

Trading Functions

createOrder()

Places a new order on the exchange.
async function createOrder(order: CreateOrder): Promise<string>
order
CreateOrder
required
Order parameters:
  • market (string): Market symbol (e.g., “SOL_USDCT”)
  • side (string): Order side (“buy” or “sell”)
  • quantity (number): Order quantity in base asset
  • price (number): Limit price
  • userId (string): User identifier
return
string
Order creation response from the API

Example

import { createOrder } from "@/utils/requests";

const result = await createOrder({
  market: "SOL_USDCT",
  side: "buy",
  quantity: 0.1,
  price: 50000,
  userId: "user-123",
});

console.log("Order created:", result);
Ensure the user has sufficient balance before placing orders. The API will reject orders with insufficient funds.

User Management Functions

createUser()

Creates a new user account on the exchange.
async function createUser(): Promise<UserId>
return
UserId
User creation response:
  • status (string): Creation status
  • user_id (string): Newly created user identifier

Example

import { createUser } from "@/utils/requests";

const newUser = await createUser();
console.log(`New user created: ${newUser.user_id}`);

// Store user_id in local storage or state management
localStorage.setItem("userId", newUser.user_id);

Error Handling

All functions can throw axios errors. Implement proper error handling:
import { getTicker } from "@/utils/requests";
import axios from "axios";

try {
  const ticker = await getTicker("SOL_USDCT");
  console.log(ticker);
} catch (error) {
  if (axios.isAxiosError(error)) {
    console.error("API Error:", error.response?.data);
    console.error("Status:", error.response?.status);
  } else {
    console.error("Unexpected error:", error);
  }
}

Complete Usage Example

import {
  createUser,
  getTicker,
  getDepth,
  getTrades,
  getKlines,
  createOrder,
} from "@/utils/requests";

async function initializeTrading() {
  // Create user
  const user = await createUser();
  console.log("User ID:", user.user_id);

  // Get market overview
  const ticker = await getTicker("SOL_USDCT");
  console.log("Current price:", ticker.lastPrice);

  // Get order book
  const depth = await getDepth("SOL_USDCT");
  console.log("Best bid:", depth.bids[0]);
  console.log("Best ask:", depth.asks[0]);

  // Get recent trades
  const trades = await getTrades("SOL_USDCT");
  console.log(`Last ${trades.length} trades loaded`);

  // Get historical data
  const startTime = Date.now() - 24 * 60 * 60 * 1000;
  const klines = await getKlines("SOL_USDCT", "1h", startTime);
  console.log(`${klines.length} candles loaded`);

  // Place an order
  const order = await createOrder({
    market: "SOL_USDCT",
    side: "buy",
    quantity: 0.001,
    price: parseFloat(depth.bids[0][0]) - 100, // 100 below best bid
    userId: user.user_id,
  });
  console.log("Order placed:", order);
}

Type Definitions

For complete type definitions, see:
  • apps/web/src/utils/types.ts
  • Type interfaces: Depth, Trade, KLine, Ticker, CreateOrder, UserId

Source Location

apps/web/src/utils/requests.ts

Build docs developers (and LLMs) love