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
Parameter | Type | Description |
---|---|---|
apiKey | string | Your CashOnRails API key obtained from the dashboard |
publicKey | string | Your CashOnRails public key obtained from the dashboard |
Optional Configuration
Option | Type | Default | Description |
---|---|---|---|
environment | string | 'TEST' | API environment (‘TEST’ or ‘LIVE’) |
timeout | number | 30000 | Request timeout in milliseconds |
baseUrl | string | undefined | Custom API base URL (if needed) |
headers | object | {} | Additional headers to include in API requests |
retryCount | number | 3 | Number of times to retry failed requests |
retryDelay | number | 1000 | Delay 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
- Never hardcode API keys in your source code
- Use environment variables for sensitive credentials
- Implement proper error handling for API requests
- Set appropriate timeouts based on your application needs
- Use the TEST environment during development and testing
Next Steps
Now that you’ve configured your client, you can explore the various services: