Skip to content

Examples

Complete Transfer Flow Example

javascript
import { createInstance, CreateTransfer, GetTransfer, MarkAsSuccess } from '@mbanq/core-sdk-js';

// Initialize the client
const client = createInstance({ 
  baseUrl: 'https://api.cloud.mbanq.com', 
  tenantId: 'your-tenant-id' 
});

await client.connect({
  credential: {
    client_id: 'your-client-id',
    client_secret: 'your-client-secret',
    username: 'your-username',
    password: 'your-password',
    grant_type: 'password'
  }
});

// Create an ACH transfer
const achTransfer = await client.request(CreateTransfer({
  amount: 1500,
  currency: 'USD',
  paymentRail: 'ACH',
  paymentType: 'CREDIT',
  debtor: {
    name: 'Alice Corporation',
    identifier: '111222333',
    accountType: 'CHECKING',
    agent: {
      name: 'First National Bank',
      identifier: '021000021'
    }
  },
  creditor: {
    name: 'Bob Enterprises',
    identifier: '444555666',
    accountType: 'CHECKING',
    agent: {
      name: 'Second Federal Bank',
      identifier: '121000248'
    }
  },
  reference: ['Invoice-2025-001']
}));

console.log('Created transfer:', achTransfer.id);

// Get transfer details
const transfer = await client.request(GetTransfer(achTransfer.id));
console.log('Transfer status:', transfer.status);

// Mark transfer as successful
if (transfer.status === 'EXECUTION_PROCESSING') {
  await client.request(MarkAsSuccess(transfer.externalId, 'ACH'));
  console.log('Transfer marked as successful');
}

Error Handling Example

javascript
import { isSdkError, CreateTransfer } from '@mbanq/core-sdk-js';

try {
  const transfer = await client.request(CreateTransfer({
    amount: 100,
    currency: 'USD',
    paymentRail: 'ACH',
    paymentType: 'CREDIT',
    debtor: {
      name: 'Test User',
      identifier: '123456789',
      accountType: 'CHECKING',
      agent: { name: 'Test Bank', identifier: '021000021' }
    },
    creditor: {
      name: 'Recipient',
      identifier: '987654321',
      accountType: 'CHECKING',
      agent: { name: 'Recipient Bank', identifier: '121000248' }
    }
  }));
} catch (error) {
  if (isSdkError(error)) {
    switch (error.classification) {
      case 'VALIDATION_ERROR':
        console.log('Invalid payment data:', error.message);
        error.details?.validationErrors?.forEach(ve => {
          console.log(`  ${ve.field}: ${ve.message}`);
        });
        break;
      case 'AUTHENTICATION_ERROR':
        console.log('Authentication failed, re-connecting...');
        await client.connect(credentials);
        break;
      case 'AUTHORIZATION_ERROR':
        if (error.details?.graphqlCode === 'RESTRICTED') {
          console.log('2FA required, please verify');
          await client.twoFactorAuthentication({ token: '123456' });
        }
        break;
      default:
        console.log('Error:', error.message);
    }
  }
}

Client Operations Example

javascript
import { 
  createInstance, 
  GetClients, 
  GetClient, 
  CreateClient 
} from '@mbanq/core-sdk-js';

const client = createInstance({
  baseUrl: 'https://api.cloud.mbanq.com',
  tenantId: 'your-tenant-id'
});

await client.connect({
  credential: {
    client_id: 'your-client-id',
    client_secret: 'your-client-secret',
    username: 'your-username',
    password: 'your-password',
    grant_type: 'password'
  }
});

// List all clients
const clients = await client.request(GetClients({
  queryLimit: 100
}));

// Get specific client
const clientDetails = await client.request(GetClient(123));

// Create new client
const newClient = await client.request(CreateClient({
  displayName: 'John Doe',
  emailAddress: 'john@example.com',
  clientType: 'PERSONAL',
  locale: 'en_US',
  legalEntityType: 'INDIVIDUAL'
}));

Card Operations Example

javascript
import {
  createInstance,
  GetCards,
  CreateCard,
  ActivateCard,
  SuspendCard
} from '@mbanq/core-sdk-js';

const client = createInstance({
  baseUrl: 'https://api.cloud.mbanq.com',
  tenantId: 'your-tenant-id'
});

await client.connect({ /* credentials */ });

// Get all cards for a client
const cards = await client.request(GetCards({ clientId: 123 }));

// Create a new virtual card
const newCard = await client.request(CreateCard({
  clientId: 123,
  productId: 1,
  cardType: 'VIRTUAL',
  cardHolder: {
    name: 'John Doe',
    email: 'john@example.com'
  }
}));

// Activate the card
const activatedCard = await client.request(ActivateCard(newCard.id));

// Suspend the card
await client.request(SuspendCard(newCard.id));
console.log('Card suspended successfully');

With Middleware Example

javascript
import { 
  createInstance, 
  createLoggingMiddleware, 
  createMetricsMiddleware,
  CreatePayment 
} from '@mbanq/core-sdk-js';

// Custom metrics client
const metricsClient = {
  incrementCounter: (counterName) => {
    console.log(`[METRIC] ${counterName}`);
  },
  recordError: (error) => {
    console.error(`[ERROR] ${error.message}`);
  }
};

// Create client with multiple middleware
const client = createInstance({
  baseUrl: 'https://api.cloud.mbanq.com',
  tenantId: 'your-tenant-id',
  middlewares: [
    createLoggingMiddleware(console),
    createMetricsMiddleware(metricsClient)
  ]
});

await client.connect({ /* credentials */ });

// All requests will be logged and metrics tracked
const payment = await client.request(CreatePayment({
  amount: 500,
  currency: 'USD',
  paymentRail: 'ACH',
  paymentType: 'CREDIT',
  // ... other payment details
}));

Released under the MIT License.