Libraries and SDKsNode.jsClient Configuration

Client Configuration

This guide explains how to configure the CashOnRails client for your specific needs.

Basic Configuration

When initializing the CashOnRails client, you can provide various configuration options:

import { CashOnRailsClient } from 'cashonrails-node';
 
const client = new CashOnRailsClient('your_api_key', 'your_public_key', {
  environment: 'TEST',
  timeout: 60000,
  baseUrl: 'https://custom-api.cashonrails.com', // Optional
});

Configuration Options

Required Parameters

ParameterTypeDescription
apiKeystringYour CashOnRails API key obtained from the dashboard
publicKeystringYour CashOnRails public key obtained from the dashboard

Optional Configuration

OptionTypeDefaultDescription
environmentstring'TEST'API environment (‘TEST’ or ‘LIVE’)
timeoutnumber30000Request timeout in milliseconds
baseUrlstringundefinedCustom API base URL (if needed)
headersobject{}Additional headers to include in API requests
retryCountnumber3Number of times to retry failed requests
retryDelaynumber1000Delay between retries in milliseconds

Environment Settings

Test Environment

The test environment is designed for development and testing. In this mode:

  • No real transactions are processed
  • Test cards and accounts are provided
  • Webhooks are sent to your specified test endpoints
const testClient = new CashOnRailsClient('test_api_key', 'test_public_key', {
  environment: 'TEST'
});

Live Environment

The live environment processes real transactions with actual money:

const liveClient = new CashOnRailsClient('live_api_key', 'live_public_key', {
  environment: 'LIVE'
});

Advanced Configuration

Custom Headers

You can add custom headers to all API requests:

const client = new CashOnRailsClient('your_api_key', 'your_public_key', {
  headers: {
    'X-Custom-Header': 'custom-value',
    'User-Agent': 'YourAppName/1.0.0'
  }
});

Request Retries

Configure automatic retries for failed requests:

const client = new CashOnRailsClient('your_api_key', 'your_public_key', {
  retryCount: 5,
  retryDelay: 2000 // 2 seconds
});

Custom Base URL

If you need to connect to a custom API endpoint:

const client = new CashOnRailsClient('your_api_key', 'your_public_key', {
  baseUrl: 'https://api.your-custom-domain.com'
});

Timeout Settings

Adjust the request timeout for slow connections:

const client = new CashOnRailsClient('your_api_key', 'your_public_key', {
  timeout: 60000 // 60 seconds
});

Environment Variables

For better security, we recommend storing your API keys as environment variables:

const client = new CashOnRailsClient(
  process.env.CASHONRAILS_API_KEY,
  process.env.CASHONRAILS_PUBLIC_KEY,
  {
    environment: process.env.NODE_ENV === 'production' ? 'LIVE' : 'TEST'
  }
);

Configuration Best Practices

  1. Never hardcode API keys in your source code
  2. Use environment variables for sensitive credentials
  3. Implement proper error handling for API requests
  4. Set appropriate timeouts based on your application needs
  5. Use the TEST environment during development and testing

Next Steps

Now that you’ve configured your client, you can explore the various services: