Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 01be5b5

Browse files
authored
Merge pull request #38 from binary-com/cms/concepts/api-calls-anatomy/index
Create Concept “api-calls-anatomy/index”
2 parents b7b192b + b1326e6 commit 01be5b5

File tree

1 file changed

+132
-0
lines changed
  • docs/core-concepts/api-calls-anatomy

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: API Calls Anatomy
3+
hide_title: false
4+
draft: false
5+
sidebar_label: API Calls anatomy
6+
sidebar_position: 1
7+
tags:
8+
- concept
9+
keywords:
10+
- trading
11+
- concept
12+
description: Deriv API calls Anatomy
13+
---
14+
15+
## API Call Types
16+
17+
We provide two types of API calls, `Subscription` and `Send Once` ( Fire and Forget )
18+
19+
### Subscription
20+
21+
Some API calls provide `subscription` ability, which means when you subscribe to them every time that particular event happens for example [Tick History](https://api.deriv.com/api-explorer#ticks_history), these API calls have an `optional` `subscription` field, which if you pass `1` to them, the subscription starts. most of the time data provided by this type of calls will be considered as data source for other calls or features.
22+
23+
### Send Once
24+
25+
`Send Once` API calls are just for getting some data as the name says for once. so if you want to get the updated data you have to send the API call again and get the updated data, most of the time API calls of this type will be used based on other other calls responses or even UI event events such as `Click`, `Scroll`, etc.
26+
27+
In order to make it easier for you to handle the `request` and `response` flow of your websocket connection, every deriv websocket API calls has a general structure. you can use them for caching, validation, request and response synchronization, etc.
28+
29+
## Request Data Anatomy
30+
31+
Request data anatomy means the `data` you send to the `websocket_connection.send` function, it doesn't matter if it's a `subscription` or a just `send once` request.
32+
33+
### Identifier Field
34+
35+
Every `request` has an `Identifier` field which is `unique` which gets usually a `number` or `1` as value.
36+
37+
:::caution
38+
Identifier field is always required.
39+
:::
40+
41+
### Required Fields
42+
43+
Every request data has several required fields which you must provide them and they may contain optional fields as well, let's explore this with an example on `Residence List`:
44+
45+
`Residence List` Call returns a list of countries and 2-letter country codes, suitable for populating the account opening form.
46+
47+
Request data for this call is like so:
48+
49+
```ts
50+
interface IAssetIndex {
51+
residence_list: 1; // it must always be `1`
52+
passthrough?: object;
53+
req_id?: number;
54+
}
55+
```
56+
57+
The `residence_list` field is the `Identifier Field` for the call and is required here, there may other required fields which are related to type of the request you wanna send. if you want to know more about `Residence List` check [it](https://api.deriv.com/api-explorer#residence_list) out here.
58+
59+
### Optional Fields
60+
61+
Every Call has several `Optional` fields as well, `passthrough` and `req_id` are always part of the request data but you can choose to opt-out and not use them.
62+
63+
#### Passthrough Field
64+
65+
Whatever you pass to this field will be returned back to you on `response` object, this can be helpful when you wanna simulate some kinda stateful like flow for your `requests` and `responses`.
66+
67+
#### Req Id Field
68+
69+
You may want to `tag` your requests and pass them through our `websocket` calls. you can do it by passing a `number` to this field. it can be helpful when you wanna map `requests` to `responses`.
70+
71+
:::tip
72+
`passthrough` and `req_id` are always optional and present in any API call.
73+
:::
74+
75+
:::caution
76+
There may be other optional fields for a request which are only related to that api call, please check our [API Explorer](https://api.deriv.com/api-explorer) to get familiar with them.
77+
:::
78+
79+
## Response Data Anatomy
80+
81+
When you get the response for the call, there will be a `Field` with the same name as the `Identifier`. and it contains the actual data.
82+
83+
The response for the `Residence List` Call would be something like so:
84+
85+
```js
86+
const response = {
87+
echo_req: {
88+
req_id: 1,
89+
residence_list: 1,
90+
},
91+
msg_type: 'residence_list',
92+
req_id: 1,
93+
residence_list: [...],
94+
};
95+
```
96+
97+
Here the `residence_list` is the `Identifier Field` and it contains the actual data you requested. the array is removed here for brevity sake, you can check the actual real response [here](https://api.deriv.com/api-explorer#residence_list).
98+
99+
### The `echo_req` Field
100+
101+
As you can see this `Field` contains the exact `Request Data` you sent to the server.
102+
103+
### The `msg_type` Field
104+
105+
This `Field` help you understand what kind of message you're getting on `message` event of the websocket connection. for example your `onmessage` event handler can be something like this:
106+
107+
```js
108+
socket.onmessage = (event) => {
109+
const receivedMessage = JSON.parse(event.data);
110+
111+
switch (receivedMessage.msg_type) {
112+
case "residence_list":
113+
console.log("The residence list is : ",receivedMessage.residence_list)
114+
break;
115+
case "some_other_request_identifier"
116+
console.log("the response", receivedMessage.some_other_request_identifier)
117+
default:
118+
console.log("receivedMessage", receivedMessage)
119+
break;
120+
}
121+
}
122+
```
123+
124+
So based on the `msg_type` you get in the response, you can update your logic.
125+
126+
### The `req_id` Field
127+
128+
This is the `Optional` passed to the `Request Data`, you can use it for `validation`, `synchronization`, `caching`, etc.
129+
130+
:::tip
131+
The `msg_type` is always present on the response data.
132+
:::

0 commit comments

Comments
 (0)