bitscrunch-sdk v1.0.1
BitsCrunch SDK
The BitsCrunch JavaScript SDK provides a simple and secure interface to interact with the BitsCrunch API. It includes functionality for signing requests, generating secure headers, and querying the network across different API versions.
Features
- 🚀 Supports multiple API versions:
/api/v1,/api/v2, and future versions. - 🔐 Automatic signing: Ensures secure communication with the server by signing requests.
- 🔧 Flexible query support: Handle query parameters and request bodies seamlessly.
- 🛠️ Built-in error handling: Provides meaningful error messages for debugging.
Installation
Install the SDK using npm:
npm install bitscrunch-sdkUsage
1. Initialize the SDK
To begin using the SDK, you need:
- An access key JSON file generated from the decentralization interface app.bitscrunch.com.
- The desired API version (
/api/v1or/api/v2).
Example: APIv1
const APIv1 = require('bitscrunch-sdk/apiv1')
const accessKey = require('./access-key.json')
const apiV1 = new APIv1(accessKey)Example: APIv2
const APIv2 = require('bitscrunch-sdk/apiv2')
const accessKey = require('./access-key.json')
const apiV2 = new APIv2(accessKey)2. Make a Request
GET Request Example
;(async () => {
try {
const response = await apiV1.request('GET', '/market/metrics', {
currency: 'usd',
blockchain: 1,
metrics: 'marketcap',
time_range: '24h',
include_washtrade: 'true',
})
console.log('Response:', response)
} catch (error) {
console.error('Error:', error.message)
}
})()Best Practices
1. Keep Your Access Key Secure
Store your access-key.json file securely and avoid committing it to source control.
2. Reuse the SDK Instance
Avoid creating multiple SDK instances for the same API version. Reuse a single instance:
const apiV1 = new APIv1(accessKey)
// Reuse `apiV1` for multiple requests3. Handle Errors Gracefully
Use try-catch blocks to manage API errors and display meaningful messages to the user.
4. Normalize Path Inputs
Always pass normalized paths (e.g., "/market/metrics"). The SDK will automatically handle leading/trailing slashes.
5. Use the Correct API Version
Ensure you're using the correct class (APIv1, APIv2, etc.) for the desired API version.
Error Handling
The SDK provides meaningful error messages for common scenarios:
Invalid Access Key
Error: Access key, base URL, and API prefix are required to initialize the SDK.Invalid Request
Error: HTTP 400: Invalid request parameters.Server Errors
Error: HTTP 500: Internal Server Error.Contributing
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request with a clear description of your changes.