Saltar al contenido principal

Get a list of countries

To get a list of countries, update the open event listener using the following approach:

index.js
const ping_interval = 12000; // it's in milliseconds, which equals to 120 seconds
let interval;
// subscribe to `open` event
websocket.addEventListener('open', (event) => {
console.log('websocket connection established: ', event);
const payload = JSON.stringify({
residence_list: 1,
});
websocket.send(payload);

// to Keep the connection alive
interval = setInterval(() => {
const sendMessage = JSON.stringify({ ping: 1 });
websocket.send(sendMessage);
}, ping_interval);
});

Now, update the message event listener to render the data:

index.js
// subscribe to `message` event
websocket.addEventListener('message', (event) => {
const receivedMessage = JSON.parse(event.data);
switch (receivedMessage.msg_type) {
case 'residence_list':
console.log('list of countries', receivedMessage.residence_list);
break;
case 'ping':
console.log('ping/pong response: ', receivedMessage.ping);
break;
default:
console.log('received message: ', receivedMessage);
break;
}
});

The response should be an object:

{
"echo_req": {
"req_id": 1,
"residence_list": 1
},
"msg_type": "residence_list",
"req_id": 1,
"residence_list": [
{
"identity": {
"services": {
"idv": {
"documents_supported": {},
"has_visual_sample": 0,
"is_country_supported": 0
},
"onfido": {
"documents_supported": {},
"is_country_supported": 0
}
}
},
"phone_idd": "35818",
"text": "Aland Islands",
"value": "ax"
},
{
"identity": {
"services": {
"idv": {
"documents_supported": {},
"has_visual_sample": 0,
"is_country_supported": 0
},
"onfido": {
"documents_supported": {
"driving_licence": {
"display_name": "Driving Licence"
},
"national_identity_card": {
"display_name": "National Identity Card"
},
"passport": {
"display_name": "Passport"
}
},
"is_country_supported": 1
}
}
},
"phone_idd": "355",
"text": "Albania",
"tin_format": ["^[A-Ta-t0-9]\\d{8}[A-Wa-w]$"],
"value": "al"
}
]
}

With this call, you will get useful information about supported countries, such as:

  • A 2-letter code for each country
  • Identity service providers for each country
  • Country Tax Identifier Format (tin_format)
  • etc.

This can be useful for account creation forms, in which you need to ask users to provide validated information about their identity base, depending on their country of residence.

caution

For address and tax ID validations, please use the provided 'tin_format' for the country.

User's country is important for your next steps. It determines which assets and features they can use.

tip

It's better to get the list of countries before populating your form.

danger

You will need detailed content about IDV and ONFIDO identity services, their differences and possibilities.

Your final code will be:

index.js
const app_id = 1089; // Replace with your app_id or leave as 1089 for testing.
const websocket = new WebSocket(`wss://ws.binaryws.com/websockets/v3?app_id=${app_id}`);
const ping_interval = 12000; // it's in milliseconds, which equals to 120 seconds
let interval;

// subscribe to `open` event
websocket.addEventListener('open', (event) => {
console.log('websocket connection established: ', event);
const payload = JSON.stringify({
residence_list: 1,
});
websocket.send(payload);

// to Keep the connection alive
interval = setInterval(() => {
const sendMessage = JSON.stringify({ ping: 1 });
websocket.send(sendMessage);
}, ping_interval);
});

// subscribe to `message` event
websocket.addEventListener('message', (event) => {
const receivedMessage = JSON.parse(event.data);
switch (receivedMessage.msg_type) {
case 'residence_list':
console.log('list of countries', receivedMessage.residence_list);
break;
case 'ping':
console.log('ping/pong response: ', receivedMessage.ping);
break;
default:
console.log('received message: ', receivedMessage);
break;
}
});

// subscribe to `close` event
websocket.addEventListener('close', (event) => {
console.log('websocket connectioned closed: ', event);
clearInterval(interval);
});

// subscribe to `error` event
websocket.addEventListener('error', (event) => {
console.log('an error happend in our websocket connection', event);
});