Giter Club home page Giter Club logo

deriv-api-ts's Introduction

deriv-api-ts

A typescript wrapper around Deriv API.

Installation

Yarn

yarn add deriv-api-ts

NPM

npm install deriv-api-ts

Usage

Endpoints without authentication

import { DerivAPIWrapper } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(appId);
const websiteStatus = await api.websiteStatus();

console.log(websiteStatus);

// You will have intellisense on the return types...
console.log(websiteStatus.api_call_limits.max_requests_outcome.hourly);

Endpoints with authentication

You can authenticate whenever you want by calling authorize function. When you call an endpoint that needs authentication (e.g. profitTable), it will first check if the session is already authenticated. If not, it will try to authenticate with the token that is passed either to the constructor or to the last call of authorize function. If no token is found, an error will be thrown.

import { DerivAPIWrapper } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(appId);

const authResult = await api.authorize('YOUR API TOKEN');

OR

import { DerivAPIWrapper } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(appId, 'YOUR API TOKEN');

Types

@deriv/api-types package is used internally to provide support for types.

import { DerivAPIWrapper, Exception, Types as binaryTypes } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(appId, 'YOUR API TOKEN');
const params: binaryTypes.ProfitTableRequest = {
    contract_type : [ 'CALL' ],
    profit_table  : 1,
};

api.profitTable(params)
   .then((profitTableResult: binaryTypes.ProfitTable) =>
   {
       // Your code
   })
   .catch((err: Exception) =>
   {
	   console.log(`Code: ${ errObj.code } Message: ${ errObj.message } Details: ${ JSON.stringify(errObj.getParams()) }`);
   })
   // Closing the websocket connection gracefully.
   .finally(() => api.disconnect());

Error Handling

import { DerivAPIWrapper, Exception, Types } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(1234, 'YOUR API TOKEN');
const params: Types.ProfitTableRequest = {
    contract_type : [],
    profit_table  : 1,
};

api.profitTable(params)
   .then((profitTableResult) =>
   {
       // Your code
   })
   .catch((err: Exception) =>
   {
	   console.log(`Code: ${ errObj.code } Message: ${ errObj.message } Details: ${ JSON.stringify(errObj.getParams()) }`);
   })
    // Closing the websocket connection gracefully.
   .finally(() => api.disconnect());

OR

import { DerivAPIWrapper, Exception, Types } from 'deriv-api-ts';

const appId = 1234;
const api = new DerivAPIWrapper(1234, 'YOUR API TOKEN');
const params: Types.ProfitTableRequest = {
    contract_type : [],
    profit_table  : 1,
};

try
{
    const profitTableResult = await api.profitTable(params);
}
catch (err)
{
    const errObj = err as Exception;
    console.log(`Code: ${ errObj.code } Message: ${ errObj.message } Details: ${ JSON.stringify(errObj.getParams()) }`);
}
finally
{
    // Closing the websocket connection gracefully.
    api.disconnect();
}

deriv-api-ts's People

Contributors

information-security avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.