1.0.1 • Published 1 year ago
@deriv-com/api-client v1.0.1
Deriv API Client
A lightweight, zero dependency wrapper around the WebSockets Web API and the native Node WebSocket (Node V22 and above) that is fully typed to Deriv's Backend WebSocket (Deriv WS API Explorer).
Requirements
- Node version 22 and above (With native WebSocket API): for running it on the Backend or a Node runner
- For browser - generally all is supported
Getting Started
Using the Library
You can start by installing the library via the following commands:
npm
npm i @deriv-com/api-clientpnpm
pnpm install @deriv-com/api-clientyarn
yarn add @deriv-com/api-clientconst derivAPI = new DerivAPIClient();
const response = await derivAPI.send({ name: 'get_account_status' });
console.log(response);Contributing to the Library
Starting the Development Server
This project comes with a sandbox served with HMR. In it you have a sample code which you can test your changes. To run the sandbox run:
npm run devFunctionalities
- Conversion of Request/Response Deriv WS Calls to JavaScript Promises
- Deduping Subscriptions via Internal Subscription Tracking
- Separating data handlers (
onData) with WebSocket message streams - there will always be only one subscription per payload to Deriv WS BE - Asynchronous queuing calls when WebSocket calls are
connecting,disconnectorreconnecting - Connection keep alive mechanism
- Allow error handling via callbacks by forwarding the generic Deriv WS
response.errorproperty - Fully typed safe endpoints - Typed payload based on endpoint names
Core Functions
Send
async send({ name, payload })The send method is for request/response Deriv WS call that do not have subscriptions. These types of call will only return one response for every call you make.
name- typesafe to all deriv WS request/response endpoint names (TSocketEndpointNames).payload- typesafe to the expected payload (mapped to thename). Default payload will always be set to{ [name]: 1 }. For example for theget_account_statuscall, if no payload is passed will result in the following payload sent to the BE
{
"get_account_status": 1
}