The OFFER-HUB SDK provides a type-safe TypeScript client for integrating with the Orchestrator API. It handles authentication, retries, and provides typed errors for better error handling.
Installation
$ npm install @offerhub/sdk
Or with yarn:
Configuration
import { OfferHubSDK } from '@offerhub/sdk'; const sdk = new OfferHubSDK({ apiUrl: process.env.OFFERHUB_API_URL, // e.g., http://localhost:4000 apiKey: process.env.OFFERHUB_API_KEY, // e.g., ohk_live_xxx timeout: 30000, // Optional: request timeout (default: 30s) retryAttempts: 3, // Optional: retry count (default: 3)});
Add to your .env file:
OFFERHUB_API_URL=http://localhost:4000OFFERHUB_API_KEY=ohk_live_your_api_key_here
Never expose your API key in client-side code. The SDK is designed for server-to-server communication only.
Basic Usage
const user = await sdk.users.create({ externalUserId: 'your-user-123', email: 'buyer@example.com', type: 'BUYER' // or 'SELLER', 'BOTH'}); console.log('User ID:', user.id); // usr_abc123
const balance = await sdk.balance.get(user.id); console.log('Available:', balance.available); // "100.00"console.log('Reserved:', balance.reserved); // "0.00"
const order = await sdk.orders.create({ buyer_id: 'usr_buyer123', seller_id: 'usr_seller456', amount: '100.00', currency: 'USD', title: 'Logo Design', description: 'Design a logo for my startup'}); console.log('Order ID:', order.id); // ord_xyz789console.log('Status:', order.status); // ORDER_CREATED
Milestones are part of one order. List them with sdk.orders.getMilestones() and complete one by its milestone_ref using sdk.orders.completeMilestone() after the order reaches IN_PROGRESS:
const milestones = await sdk.orders.getMilestones(order.id); const completedMilestone = await sdk.orders.completeMilestone(order.id, 'design');console.log('Milestone status:', completedMilestone.status); // COMPLETED
// 1. Reserve fundsawait sdk.orders.reserve(order.id); // 2. Create escrow on blockchainawait sdk.orders.createEscrow(order.id); // 3. Fund the escrowawait sdk.orders.fundEscrow(order.id); // 4. Release to seller when work is doneawait sdk.orders.release(order.id, 'Work completed');
Available Resources
The SDK provides access to all Orchestrator resources:
Idempotency
For safe retries, use idempotency keys:
// Create SDK instance with idempotency keyconst idempotentSdk = sdk.withIdempotencyKey( '550e8400-e29b-41d4-a716-446655440000'); // This request can be safely retriedconst order = await idempotentSdk.orders.create({ buyer_id: 'usr_buyer', seller_id: 'usr_seller', amount: '100.00', title: 'Service'});
If you retry with the same idempotency key, you get the same response without creating a duplicate.
The key must be a UUID v4. It is optional, applies to POST, PUT, and
PATCH requests, and is scoped to the API key and request body. A replayed
response includes Idempotency-Replay: true; a different body with the same
key returns 409 IDEMPOTENCY_KEY_REUSED.
Error Handling
The SDK provides typed errors for precise error handling:
import { InsufficientFundsError, NotFoundError, ValidationError, InvalidTransitionError, OfferHubError} from '@offerhub/sdk'; try { await sdk.orders.reserve('ord_123');} catch (error) { if (error instanceof InsufficientFundsError) { console.error(`Need ${error.required}, have ${error.available}`); } else if (error instanceof NotFoundError) { console.error(`${error.resourceType} not found`); } else if (error instanceof ValidationError) { console.error('Validation errors:', error.errors); } else if (error instanceof InvalidTransitionError) { console.error(`Cannot transition from ${error.currentState}`); } else if (error instanceof OfferHubError) { console.error('API error:', error.message); }}
TypeScript Support
The SDK is fully typed. You get autocomplete and type checking:
import type { User, Order, Balance } from '@offerhub/sdk'; async function processOrder(user: User): Promise<Order> { const balance: Balance = await sdk.balance.get(user.id); if (parseFloat(balance.available) < 100) { throw new Error('Insufficient funds'); } return sdk.orders.create({ buyerId: user.id, sellerId: 'usr_seller', amount: '100.00', title: 'Service' });}
Next Steps
- Users Reference - User creation and AirTM linking (
sdk.users)
- Balance Reference - 9 balance management methods (
sdk.balance)
- Wallet Reference - Stellar wallet and deposit addresses (
sdk.wallet)
- Top-ups Reference - AirTM deposit workflows (
sdk.topups)
- Orders SDK Reference -
sdk.orders method-by-method reference
- Disputes SDK Reference -
sdk.disputes method-by-method reference
- Withdrawals SDK Reference -
sdk.withdrawals method-by-method reference
- Quick Start Guide - Full integration example
- API Reference - REST API documentation