Skip to main content

Overview

This guide introduces how to get started with using the Cobo WaaS 2.0 JavaScript SDK, which allows you to integrate the WaaS service into your existing application using the JavaScript programming language. To learn more about the initial setup steps necessary for utilizing the WaaS API, see Send your first request. You can go to GitHub to access the source code of the SDK.

Prerequisites

Create a project and install the SDK

  1. Create a project directory and initialize NPM:
mkdir waas2-js && cd waas2-js
npm init -y
  1. Install the SDK:
npm install @cobo/cobo-waas2 --save
The directory structure after installation is as follows:
waas2-js/
├─ node_modules/
├─ package.json
├─ package-lock.json
└─ (to be created later) index.js
If your project already has package.json and package-lock.json, you can directly copy them to the root directory and run npm install to pull dependencies.
  1. Create a file index.js in the root directory and paste the following code into it.
The directory structure after installation is as follows:
waas2-js/
├─ node_modules/
├─ package.json
├─ package-lock.json
└─ (to be created later) index.js
If your project already has package.json and package-lock.json, you can directly copy them to the root directory and run npm install to pull dependencies.
  1. Create a file index.js in the root directory and paste the following code into it.

Configure API key and HTTP host

  1. Set the API secret.
// Initialize the default API client
const apiClient = CoboWaas2.ApiClient.instance
// Set the API secret
apiClient.setPrivateKey("<YOUR_API_SECRET_IN_HEX>");
  1. Select which environment you want to use.
// Select the development environment
apiClient.setEnv(CoboWaas2.Env.DEV);

// Select the production environment
apiClient.setEnv(CoboWaas2.Env.PROD);

Code sample

For operation-specific documentation and sample code, see the docs folder in the WaaS SDK GitHub repository.

List supported chains

const CoboWaas2 = require("@cobo/cobo-waas2");
// Initialize the API client
const apiClient = CoboWaas2.ApiClient.instance;
// Set the environment (development: Env.DEV, production: Env.PROD)
apiClient.setEnv(CoboWaas2.Env.DEV);
// Configure API Secret – replace <YOUR_PRIVATE_KEY> with your API Secret
apiClient.setPrivateKey("<YOUR_PRIVATE_KEY>");
// Create a WalletsApi instance
const apiInstance = new CoboWaas2.WalletsApi();
// Define query parameters
const opts = {
  wallet_type: "Custodial",      // Wallet type: Custodial wallet
  wallet_subtype: "Asset",       // Wallet subtype: Asset wallet
  chain_ids: "BTC,ETH",          // Chain IDs to query: BTC, ETH
  limit: 10,                     // Limit the number of results to 10 entries
  before: "RqeEoTkgKG5rpzqYzg2Hd3szmPoj2cE7w5jWwShz3C1vyGmk1", // For pagination, optional
  after: "RqeEoTkgKG5rpzqYzg2Hd3szmPoj2cE7w5jWwShz3C1vyGSAk",  // For pagination, optional
};
// Call the API to list supported chains
apiInstance.listSupportedChains(opts).then(
  (data) => {
    console.log("API called successfully. Returned data: " + data);
  },
  (error) => {
    console.error(error);
  },
);
Example response is as follows:
API called successfully. Returned data: {
 "data": [
   {
     "chain_id": "BTC",
     "symbol": "Bitcoin",
     "icon_url": "https://d.cobo.com/public/logos/btc%403x.png",
     "explorer_tx_url": "https://mempool.space/tx/{txn_id}",
     "explorer_address_url": "https://mempool.space/address/{address}",
     "require_memo": false,
     "confirming_threshold": 4
   },
   {
     "chain_id": "ETH",
     "symbol": "Ethereum",
     "icon_url": "https://d.cobo.com/public/logos/eth%403x.png",
     "explorer_tx_url": "https://etherscan.io/tx/{txn_id}",
     "explorer_address_url": "https://etherscan.io/address/{address}",
     "require_memo": false,
     "confirming_threshold": 64
   }
 ],
 "pagination": {
   "before": "",
   "after": "",
   "total_count": 2
 }
}

Create a wallet

const CoboWaas2 = require("@cobo/cobo-waas2");
// Initialize the API client
const apiClient = CoboWaas2.ApiClient.instance;
// Set the environment (development: Env.DEV, production: Env.PROD)
apiClient.setEnv(CoboWaas2.Env.DEV);
// Configure API Secret – replace <YOUR_PRIVATE_KEY> with your API Secret
apiClient.setPrivateKey("<YOUR_PRIVATE_KEY>");
// Create a WalletsApi instance
const apiInstance = new CoboWaas2.WalletsApi();
// Define parameters for wallet creation
const opts = {
  CreateWalletParams: CoboWaas2.CreateWalletParams.constructFromObject({
    name: "My WaaS 2.0 Wallet", // Wallet name
    wallet_type: "Custodial",   // Wallet type: Custodial Wallets
    wallet_subtype: "Asset",    // Wallet subtype: Asset Wallets
  }),
};
// Call the API to create a wallet
apiInstance.createWallet(opts).then(
  (data) => {
    console.log("API called successfully. Returned data: " + data);
  },
  (error) => {
    console.error(error);
  },
);
Feel free to share your feedback to improve our documentation!