Authentication
As mentioned in the "General Information" section, Websocket is divided into Public and Private data. No authentication is required to receive Public data. To receive Private data, authentication must be successful when establishing a Websocket connection.
- Authentication is carried out through the
Authorization
header, just like in the REST API. - Please refer to "Generate an Authorization Token (JWT)" in Creating an Authorized Request.
(Example) Including Authentication token in the Header for Websocket Connection.
const jwt = require("jsonwebtoken");
const {v4: uuidv4} = require('uuid');
const WebSocket = require("ws");
const payload = {
access_key: "Your Access key",
nonce: uuidv4(),
};
const jwtToken = jwt.sign(payload, "Your Secret key");
const ws = new WebSocket("wss://api.upbit.com/websocket/v1", {
headers: {
authorization: `Bearer ${jwtToken}`
}
});
ws.on("open", () => {
console.log("connected!");
// Request after connection
ws.send('[{"ticket":"test example"},{"type":"myTrade"}]');
});
ws.on("error", console.error);
ws.on("message", (data) => console.log(data.toString()));
ws.on("close", () => console.log("closed!"));
import jwt # PyJWT
import uuid
import websocket # websocket-client
def on_message(ws, message):
# do something
data = message.decode('utf-8')
print(data)
def on_connect(ws):
print("connected!")
# Request after connection
ws.send('[{"ticket":"test example"},{"type":"myTrade"}]')
def on_error(ws, err):
print(err)
def on_close(ws, status_code, msg):
print("closed!")
payload = {
'access_key': "발급받은 Access key",
'nonce': str(uuid.uuid4()),
}
jwt_token = jwt.encode(payload, "발급받은 Secret key");
authorization_token = 'Bearer {}'.format(jwt_token)
headers = {"Authorization": authorization_token}
ws_app = websocket.WebSocketApp("wss://api.upbit.com/websocket/v1",
header=headers,
on_message=on_message,
on_open=on_connect,
on_error=on_error,
on_close=on_close)
ws_app.run_forever()
Request Format
Once the connection is successfully established, various requests can be made to the Websocket server.
Requests are made using JSON Objects, and responses are also JSON Objects.
Requests are divided into ticket field, type field, and format field, each consisting of the following components.
Request format
[{Ticket Field},{Type Field},....,{Type Field},{Format Field}]
Ticket Field
To identify the data that you want to get, a value of 'ticket field' is required.
This value identifies the recipient of the market data and it is recommended to use a unique value (such as UUID).
Field Name | Type | Description | Required | Default |
---|---|---|---|---|
ticket | String | A value that can identify you | O |
Type Field
In this field, you have to list the market data you want to receive.
"isOnlySnapshot" and "isOnlyRealtime" fields are optional and both snapshot and real-time data will be received if they are omitted.
Multiple {type field} can be specified for a single request.
Field Name | Type | Description | Required | Default |
---|---|---|---|---|
type | String | data type - ticker : Current market price- trade : Transaction- orderbook : Orders | O | |
codes | List | Market code list *Must be requested in capital letters. | O | |
isOnlySnapshot | Boolean | Only provides snapshot data | X | false |
isOnlyRealtime | Boolean | Only provides real-time data | X | false |
'codes' in private data
For private data, if the
codes
field is omitted or requested as an empty array, you can receive information for all markets.
Format Field
If specified as "Simple", the response field names will be simplified. This is for reducing the traffic burden.
Field Name | Type | Description | Required | Default |
---|---|---|---|---|
format | String | Format to receive - DEFAULT : default format- SIMPLE : simplified format | X | DEFAULT |