@deriv/deriv-api

中文文档
1.0.15发布于 2024/02/13MITCommonJS近 30 天下载 1.3万

Websocket API for Deriv applications

原始使用文档
文档目录8 节
  1. Requirement:
  2. Installing Deriv API Library
  3. Installing Websocket (ws) library
  4. Basic library
  5. ES6 modules
  6. CommonJS modules
  7. Using the HTML script tag
  8. Creating a WebSocket connection

DerivAPI

Async Websocket API for deriv-app

Code Coverage: codecov

Requirement:

Node.js (v12.18.0 or higher is recommended)

Installation

Installing Deriv API Library

We need to install the Deriv API library if we want to connect to Deriv's websocket.

NPM

npm install @deriv/deriv-api

yarn

yarn add @deriv/deriv-api

Installing Websocket (ws) library

In order to make a websocket connection, we need the websocket (ws) library.

NPM

npm install ws

yarn

yarn add ws

Usage

Basic library

The basic library is a lighter version of the Deriv API, suitable for apps that want to directly deal with API calls.

It can either be imported as a separate module (to reduce the final bundle size) or if the DerivAPI library is already imported, can be accessed using api.basic.

const WebSocket = require('ws');
const DerivAPI = require('@deriv/deriv-api/dist/DerivAPI');

// app_id 1089 is for testing, create your own app_id and use it here.
// go to api.deriv.com to register your own app.
const connection = new WebSocket('wss://ws.derivws.com/websockets/v3?app_id=1089');
const api        = new DerivAPI({ connection });
const basic = api.basic;

basic.ping().then(console.log);

ES6 modules

Basic API

// Smaller bundle size, dealing only with the low-level library
import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic.js';
import WebSocket from 'ws';

// app_id 1089 is for testing, create your own app_id and use it here.
// go to api.deriv.com to register your own app.
const connection = new WebSocket('wss://ws.derivws.com/websockets/v3?app_id=1089');
const api        = new DerivAPIBasic({ connection });

api.ping().then(console.log);

CommonJS modules

Basic API

// Smaller bundle size, dealing only with the low-level library
const WebSocket = require('ws');
const DerivAPIBasic = require('@deriv/deriv-api/dist/DerivAPIBasic');

// app_id 1089 is for testing, create your own app_id and use it here.
// go to api.deriv.com to register your own app.
const connection = new WebSocket('wss://ws.derivws.com/websockets/v3?app_id=1089');
const api        = new DerivAPIBasic({ connection });

api.ping().then(console.log);

Using the HTML script tag

Basic API

<script src="https://unpkg.com/@deriv/deriv-api/dist/DerivAPIBasic.js"></script>
<script>
    const api = new DerivAPIBasic({ 
        endpoint: 'ws.derivws.com',
        app_id: 1089 /* 1089 is a default test app_id, replace with your own app_id */,
        lang: 'EN' 
    });
    
    api.ping().then(console.log);
</script>

Creating a WebSocket connection

There are two ways to establish a connection:

  1. Use a previously opened connection:

    const connection = new WebSocket('wss://ws.derivws.com/websockets/v3?app_id=YOUR_APP_ID');
    const api        = new DerivAPI({ connection });
    
  2. Pass the arguments needed to create a connection:

    const api = new DerivAPI({ endpoint: 'ws.derivws.com', app_id: /* your app_id */, lang: 'EN' });
    

Documentation

Wiki

There is a short tutorial about how to use Deriv API in the wiki page.

API reference

The complete API reference is hosted here

The above reference in one page: DerivAPI reference

Development

git clone https://github.com/binary-com/deriv-api
cd deriv-api
npm install

To run the tests

npm test

Run tests automatically on source code edits

npm run devel

Run linter

npm run syntax

Run all tests (lint + js tests)

npm run test_all

Prettify the code (done automatically on commit)

npm run prettify

Generate documentations

npm run docs

Regenerate docs automatically on source code edit

Needs inotify to work.

npm run devel_docs

Serve docs on localhost and update on source code changes

npm run serve_docs

Generate html version of the docs and publish it to gh-pages

npm run gh-pages

Build the package

npm run build

Run examples

DERIV_TOKEN=YourTokenHere npm run examples

版本与发布记录

@deriv/deriv-api 共发布 20 个版本,首次发布于 2019/09/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.15latest2024/02/132 年前
1.0.142024/02/122 年前
1.0.132023/07/033 年前
1.0.122023/04/193 年前
1.0.112022/05/264 年前
1.0.102022/03/144 年前

其他发布通道:beta → 1.0.4-beta.0。

关于 @deriv/deriv-api 的常见问题

@deriv/deriv-api 是什么?

Websocket API for Deriv applications

如何安装 @deriv/deriv-api?

在项目目录运行 npm install @deriv/deriv-api,或使用 pnpm add / yarn add / bun add。

@deriv/deriv-api 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/deriv__deriv-api 是否存在。

@deriv/deriv-api 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@deriv/deriv-api 可以商用吗?

@deriv/deriv-api 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deriv/deriv-api 的下载量有多少?

根据 npm 官方下载统计,@deriv/deriv-api 在 2026/10/12 – 2026/11/10 这 30 天内被下载 12,778 次,日均约 426 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deriv/deriv-api 还在维护吗?

最新版本 1.0.15 发布于 2024/02/13(2 年前);至今共发布 20 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。