Skip to main content

SDK

The Torque JavaScript SDK provides a simple way to integrate Torque's multi-product checkout into your applications.

Quick Start

npm install torque-checkout
import { TorqueCheckout } from 'torque-checkout';

const torque = new TorqueCheckout({
businessId: 'your_business_id',
apiKey: 'your_api_key'
});

const { checkoutUrl } = await torque.generateCheckoutLink(cart, customerData);

Core Features

  • Checkout Management - Generate checkout links, manage orders
  • Webhook Integration - Real-time order updates with signature verification
  • Analytics - Business metrics and performance data

API Methods

Checkout Management

// Basic checkout link generation
const { checkoutUrl } = await torque.generateCheckoutLink(cart, customerData);

// With options
const { checkoutUrl } = await torque.generateCheckoutLink(cart, customerData, {
expiresIn: 3600, // 1 hour
redirectUrl: 'https://yourdomain.com/thank-you',
webhookUrl: 'https://yourdomain.com/webhooks/torque',
metadata: {
orderId: 'order_123',
source: 'website'
}
});

Retrieve Order

// Get order by ID
const order = await torque.getOrder('order_123');

// Get order with specific fields
const order = await torque.getOrder('order_123', {
fields: ['id', 'status', 'amount', 'customer']
});

List Orders

// Get all orders
const orders = await torque.listOrders();

// Get orders with filters
const orders = await torque.listOrders({
status: 'paid',
startDate: '2024-01-01',
endDate: '2024-12-31',
limit: 50,
offset: 0
});

Webhook Management

Create Webhook

// Create a new webhook
const webhook = await torque.createWebhook({
url: 'https://yourdomain.com/webhooks/torque',
events: ['order.created', 'order.paid', 'order.completed'],
secret: 'your_webhook_secret'
});

Verify Webhook Signature

// Verify webhook signature
const isValid = torque.verifyWebhookSignature(
payload,
signature,
secret
);

if (isValid) {
// Process webhook
const event = JSON.parse(payload);
console.log('Webhook event:', event);
}

Analytics

Get Analytics

// Get business analytics
const analytics = await torque.getAnalytics({
startDate: '2024-01-01',
endDate: '2024-12-31',
groupBy: 'day'
});

Error Handling

try {
const { checkoutUrl } = await torque.generateCheckoutLink(cart, customerData);
} catch (error) {
if (error.code === 'INVALID_CART') {
console.error('Invalid cart data:', error.message);
} else if (error.code === 'RATE_LIMITED') {
console.error('Rate limited, please try again later');
} else {
console.error('Unexpected error:', error.message);
}
}

Configuration

const torque = new TorqueCheckout({
businessId: 'your_business_id',
apiKey: 'your_api_key',
environment: 'sandbox', // or 'production'
timeout: 30000, // 30 seconds
retries: 3
});

Support

Need help with the SDK? Contact our support team at hello@torque.fi.