Skip to main content

JavaScript SDK

The @ramp-orchestrator/sdk package provides programmatic control over the embeddable widget.

Installation

npm install @ramp-orchestrator/sdk
# or
yarn add @ramp-orchestrator/sdk
# or
pnpm add @ramp-orchestrator/sdk

Quick Start

import { RampWidget } from '@ramp-orchestrator/sdk';

const widget = new RampWidget({
  apiKey: 'your_api_key',
  projectId: 'your_project_id',
  externalUserId: 'user_123',
  containerId: 'widget-container',
});

widget.mount();

RampWidget Class

Constructor

new RampWidget(config: RampWidgetConfig)

Configuration

interface RampWidgetConfig {
  // Required
  apiKey: string;
  projectId: string;
  externalUserId: string;

  // Container (SDK mode)
  containerId?: string;

  // Mode
  mode?: 'buy' | 'sell';

  // Pre-fill options
  defaultCrypto?: string;
  defaultFiat?: string;
  defaultAmount?: string;
  walletAddress?: string;

  // Customization
  theme?: ThemeConfig;
  locale?: string;

  // Behavior
  closeOnComplete?: boolean;
  closeDelay?: number;
  animations?: boolean;

  // Currency restrictions
  allowedCryptoCurrencies?: string[];
  allowedFiatCurrencies?: string[];
}

Methods

mount()

Mount the widget to the DOM.
widget.mount();

unmount()

Remove the widget from the DOM.
widget.unmount();

refresh()

Refresh the widget (useful after session timeout).
widget.refresh();

setUser(externalUserId)

Change the current user.
widget.setUser('new_user_456');

setAmount(amount)

Programmatically set the amount.
widget.setAmount('250.00');

setCrypto(currency)

Change the selected cryptocurrency.
widget.setCrypto('ETH');

setFiat(currency)

Change the selected fiat currency.
widget.setFiat('EUR');

setWallet(address)

Set the destination wallet.
widget.setWallet('0x742d35Cc6634C0532925a3b844Bc9e7595f5bE21');

goToStep(step)

Navigate to a specific step.
widget.goToStep('amount'); // or 'confirm', 'wallet', 'payment'

getState()

Get the current widget state.
const state = widget.getState();
console.log(state);
// {
//   step: 'amount',
//   amount: '100',
//   crypto: 'ETH',
//   fiat: 'USD',
//   quote: { ... },
//   customer: { ... }
// }

Event Handling

on(event, handler)

Subscribe to widget events.
widget.on('orderComplete', (order) => {
  console.log('Order completed:', order);
});

off(event, handler)

Unsubscribe from events.
const handler = (order) => console.log(order);
widget.on('orderComplete', handler);
widget.off('orderComplete', handler);

once(event, handler)

Subscribe to an event once.
widget.once('ready', () => {
  console.log('Widget is ready');
});

TypeScript Support

The SDK includes full TypeScript definitions:
import { RampWidget, RampWidgetConfig, Order, Quote } from '@ramp-orchestrator/sdk';

const config: RampWidgetConfig = {
  apiKey: process.env.RAMP_API_KEY!,
  projectId: process.env.RAMP_PROJECT_ID!,
  externalUserId: userId,
  containerId: 'widget',
};

const widget = new RampWidget(config);

widget.on('orderComplete', (order: Order) => {
  console.log(`Order ${order.id} completed`);
});

widget.on('quoteGenerated', (quote: Quote) => {
  console.log(`Quote: ${quote.sourceAmount} ${quote.sourceCurrency} = ${quote.targetAmount} ${quote.targetCurrency}`);
});

UMD Build

For non-bundled usage:
<script src="https://cdn.yourdomain.com/sdk/ramp-widget.umd.js"></script>
<script>
  const widget = new RampOrchestrator.RampWidget({
    apiKey: 'your_api_key',
    projectId: 'your_project_id',
    externalUserId: 'user_123',
    containerId: 'widget-container',
  });

  widget.mount();
</script>

Error Handling

import { RampWidget, RampError } from '@ramp-orchestrator/sdk';

const widget = new RampWidget(config);

widget.on('error', (error: RampError) => {
  switch (error.code) {
    case 'INIT_FAILED':
      console.error('Widget failed to initialize');
      break;
    case 'NETWORK_ERROR':
      console.error('Network error - retrying...');
      widget.refresh();
      break;
    case 'SESSION_EXPIRED':
      console.error('Session expired');
      widget.refresh();
      break;
    case 'INVALID_CONFIG':
      console.error('Invalid configuration:', error.message);
      break;
    default:
      console.error('Unknown error:', error);
  }
});

Browser Support

BrowserVersion
Chrome80+
Firefox75+
Safari13+
Edge80+

Bundle Size

BuildSize
ESM (minified)~15KB
UMD (minified)~18KB
Gzipped~5KB

Examples

Complete Integration

import { RampWidget } from '@ramp-orchestrator/sdk';

class CryptoPurchaseManager {
  private widget: RampWidget | null = null;

  init(userId: string) {
    this.widget = new RampWidget({
      apiKey: process.env.RAMP_API_KEY,
      projectId: process.env.RAMP_PROJECT_ID,
      externalUserId: userId,
      containerId: 'ramp-widget',
      defaultCrypto: 'ETH',
    });

    this.setupEventHandlers();
    this.widget.mount();
  }

  private setupEventHandlers() {
    this.widget.on('ready', () => {
      console.log('Widget ready');
    });

    this.widget.on('quoteGenerated', (quote) => {
      analytics.track('Quote Generated', { amount: quote.sourceAmount });
    });

    this.widget.on('orderComplete', async (order) => {
      await this.handleOrderComplete(order);
    });

    this.widget.on('error', (error) => {
      errorReporter.capture(error);
    });
  }

  private async handleOrderComplete(order) {
    // Track conversion
    analytics.track('Purchase Complete', {
      orderId: order.id,
      amount: order.sourceAmount,
      crypto: order.targetCurrency,
    });

    // Update UI
    showSuccessMessage('Purchase complete!');
  }

  destroy() {
    this.widget?.unmount();
    this.widget = null;
  }
}