Skip to main content

Installation

Install the SDK using your preferred package manager:
npm install @mobula_labs/sdk
# or
yarn add @mobula_labs/sdk
# or
pnpm add @mobula_labs/sdk
# or
bun add @mobula_labs/sdk

Quick Start

import { MobulaClient } from '@mobula_labs/sdk';

// Initialize the client
const client = new MobulaClient({
  apiKey: 'your-api-key', // Get one at https://admin.mobula.io
  debug: true, // Optional: Enable debug logging
});

// Fetch market data
const marketData = await client.fetchMarketData({
  asset: 'bitcoin',
});

console.log(marketData);

Configuration Options

interface MobulaClientOptions {
  apiKey?: string;         // Your Mobula API key
  restUrl?: string;        // Custom REST API URL
  wsUrl?: string;          // Custom WebSocket URL
  timeout?: number;        // Request timeout in ms (default: 10000)
  debug?: boolean;         // Enable debug logging (default: false)
}

REST API Methods

The SDK provides typed methods for all Mobula REST endpoints. All methods return fully typed responses.

Market Data

// Fetch market data for a single asset
const marketData = await client.fetchMarketData({
  asset: 'bitcoin',
});

// Fetch multiple assets
const multiData = await client.fetchMarketMultiData({
  assets: ['bitcoin', 'ethereum', 'solana'],
});

// Fetch market history
const history = await client.fetchMarketHistory({
  asset: 'bitcoin',
  interval: '1d',
  from: Math.floor(Date.now() / 1000) - 86400 * 30,
  to: Math.floor(Date.now() / 1000),
});

// Fetch OHLCV data
const ohlcv = await client.fetchMarketOHLCVHistory({
  asset: 'bitcoin',
  blockchain: 'ethereum',
  pair: 'WETH/USDC',
  interval: '1h',
  from: Math.floor(Date.now() / 1000) - 86400,
  to: Math.floor(Date.now() / 1000),
});

Asset Metadata

// Fetch single asset metadata
const metadata = await client.fetchMetadata({
  asset: 'bitcoin',
});

// Fetch multiple assets metadata
const multiMetadata = await client.fetchMultiMetadata({
  assets: 'bitcoin,ethereum',
});

Token Details

// Fetch token details
const tokenDetails = await client.fetchTokenDetails({
  asset: 'bitcoin',
  blockchain: 'ethereum',
});

// Fetch batch token details
const batchTokenDetails = await client.fetchTokenDetailsBatch({
  assets: ['bitcoin', 'ethereum'],
  blockchain: 'ethereum',
});

Portfolio & Wallet

// Fetch wallet portfolio
const portfolio = await client.fetchWalletPortfolio({
  wallet: '0xc1e42f862d202b4a0ed552c1145735ee088f6ccf',
});

// Fetch multi-portfolio
const multiPortfolio = await client.fetchMultiPortfolio({
  wallets: ['0x...', '0x...'],
});
// Search for assets
const searchResults = await client.search({
  query: 'bitcoin',
});

// Fast search
const fastSearch = await client.searchFast({
  query: 'btc',
});

Swap

// Get swap quote
const quote = await client.fetchSwapQuoting({
  chainId: 1,
  tokenIn: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
  amount: '1000000',
  walletAddress: '0x...',
  slippage: 0.5,
});

Error Handling

The SDK provides a custom MobulaError class for better error handling:
import { MobulaClient, MobulaError } from '@mobula_labs/sdk';

try {
  const data = await client.fetchMarketData({ asset: 'bitcoin' });
} catch (err: unknown) {
  if (err instanceof MobulaError) {
    console.error('Status:', err.status);
    console.error('Data:', err.data);
    console.error('Message:', err.message);
  }
}

Utility Functions

The SDK exports several utility functions:
import {
  formatCryptoPrice,
  formatTokenPrice,
  formatUSD,
  formatPercentage,
  buildExplorerUrl,
  buildNativeSymbol,
} from '@mobula_labs/sdk';

// Format prices
formatCryptoPrice(1234.5678);  // "$1,234.57"
formatTokenPrice(0.00012345); // "$0.000123"

// Format percentages
formatPercentage(0.1234);     // "+12.34%"

// Build explorer URL
buildExplorerUrl({
  blockchain: 'ethereum',
  address: '0x...',
});

// Get native symbol
buildNativeSymbol('ethereum'); // "ETH"

Full Documentation

For the complete SDK documentation including WebSocket support, visit: