The JS SDK lets you connect to a contract blockchain, submit transactions, and query on-chain data from a Node.js or browser environment—without writing raw gRPC or HTTPS calls yourself.
This guide walks you through installing the SDK, preparing the required SSL certificates and account credentials, initializing a connection, and verifying it with a live block query.
Prerequisites
Before you begin, ensure that you have:
Node.js above v10 and below v16 — download from nodejs.org
A contract blockchain instance in the BaaS platform
The JS SDK package (
alipay-mychain-0.2.27.tgz) — see JS SDK introduction for download instructions
Step 1: Install the SDK
In your project directory, run:
npm i alipay-mychain-0.2.27.tgz --saveStep 2: Prepare SSL certificates
Three certificate files are required to establish a TLS connection to the blockchain node.
| File | Purpose | How to get it |
|---|---|---|
ca.crt | Root certificate for server authentication | Download from the BaaS platform |
client.key | RSA private key for the client | Generate using the BaaS key generation tool |
client.crt | RSA client certificate, paired with client.key | Generate client.csr using the key generation tool, submit it to the BaaS platform, then download the approved certificate |
If you haven't applied for certificates yet, follow the Access the Blockchain section in Quick Experience for Free to generate all three files.
Step 3: Prepare a blockchain account
Each transaction on the contract blockchain must be signed by an existing account. The SDK needs the account's account name and its private key file.
Create an account in the BaaS platform. During account creation, specify the account name, public key, and recovery public key. The user.key file corresponds to the account's public key and serves as the private key file.
Convert user.key to PEM format so the SDK can read it:
openssl ec -in user.key -passin pass:${key_password} -passout pass:${key_password} -aes256 -out user.pemReplace ${key_password} with your private key password.
Keep user.pem and client.key secure. Never commit private key files to version control or share them in logs.
Step 4: Connect and verify
The following example initializes a connection to the blockchain and calls QueryLastBlock to confirm the connection is working.
const Chain = require("@alipay/mychain/index.node") // Node.js environment
const fs = require("fs")
// Load the account private key
const accountKey = fs.readFileSync("./certs/user.pem", { encoding: "utf8" })
const accountPassword = "<your-pem-password>" // Replace with your user.pem password
const keyInfo = Chain.utils.getKeyInfo(accountKey, accountPassword)
// keyInfo.privateKey and keyInfo.publicKey are hexadecimal strings
// Load SSL certificates and configure the connection
const passphrase = "<your-client-key-password>" // Replace with your client.key password
const opt = {
host: "<node-ip>", // IP address of the target blockchain node
port: 18130, // Port number of the blockchain node
timeout: 30000, // Connection timeout in milliseconds
cert: fs.readFileSync("./certs/client.crt", { encoding: "utf8" }),
ca: fs.readFileSync("./certs/ca.crt", { encoding: "utf8" }),
key: fs.readFileSync("./certs/client.key", { encoding: "utf8" }),
userPublicKey: keyInfo.publicKey,
userPrivateKey: keyInfo.privateKey,
userRecoverPublicKey: keyInfo.publicKey,
userRecoverPrivateKey: keyInfo.privateKey,
passphrase: passphrase
}
// Initialize the connection
const chain = Chain(opt)
// Verify the connection by querying the latest block
chain.ctr.QueryLastBlock({}, (err, data) => {
console.log('raw data:', data)
console.log('block hash:', data.block.block_header.hash)
console.log('block number:', data.block.block_header.block_number)
})Replace the following placeholders before running:
| Placeholder | Description | Example |
|---|---|---|
<your-pem-password> | Password set when converting user.key | 123abc |
<your-client-key-password> | Password for client.key | 123abc |
<node-ip> | IP address of the blockchain node | 127.0.0.1 |
A successful response looks like:
raw data: { msg_type: 58,
sequence: 1,
return_code: 0,
group_id: '0x0000000000000000000000000000000000000000',
block:
{ block_header:
{ hash:
'0xe99d8958a45e8c87a7b10efc259828f06fe083995be52997f5d310f2b6d073a6',
version: 2,
block_number: 84265,
parent_hash:
'0x918b263a8e6c6fff594b89570970ef4bef24cf93aeed5347f7b250b070857c4b',
transaction_root:
'0x0000000000000000000000000000000000000000000000000000000000000000',
receipt_root:
'0x0000000000000000000000000000000000000000000000000000000000000000',
state_root:
'0x9f71cb9ce960e5637bad6da5be8daa2d7e557942208f241a60589b2de98e6c71',
gas_used: 0,
timestamp: 1547382477852,
log_bloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
block_body:
{ transaction_list: [],
receipt_list: [],
consensus_proof:
'0xf8f2f8c9b8419746989382c1613c6c3ce98bf79ac92a8d69952c22f4064194869c53b4075d517cfc98eda861212687e49b186c08d196770bd356762fa2a88d0288c0556f271a01b84138a97446a75c76a31d24880c343407bd7bc24608685c494240ac603cad62201a01a423718661b24e517ee6f6b2fee6d356b57833305860700cca81b0238f870400b841eaf508392d9098a3e7fb46f6f7aa4b53311e5a0d13e300d02025af7453ea130a6c27407c1da254578cae80ed498d4807587883f837c1716b0be8ae02cf955d6000e61e83014929a0e200d8bee723d820022d5c5a1f8fe6b521c0a4fff0b5ec03a7c6061276d68b58' } },
api: 'QueryLastBlock' }
block hash: 0xe99d8958a45e8c87a7b10efc259828f06fe083995be52997f5d310f2b6d073a6
block number: 84265The response includes areturn_codefield. A value of0means the call succeeded; any other value is an error code. Theresultfield in the transaction receipt works in the same way. See Error codes for details.
SDK reference
Import by environment
// Node.js environment (TLS protocol)
const Chain = require("@alipay/mychain/index.node")
// Browser environment
const Chain = require("@alipay/mychain")Configuration options
All configuration values are strings unless noted otherwise.
| Option | Required | Description | Example |
|---|---|---|---|
host | Yes | IP address (TLS) or server name (HTTPS) of the blockchain node | 127.0.0.1, https://www.alipay.com or https://127.0.0.1 |
port | Yes | Port number of the blockchain node (numbers only) | 18130 |
clients | No | Array of backup nodes as {host, port} objects. If set, host and port are not required. The SDK switches to a backup node when the primary node is unavailable. | [{host:'127.0.0.1',port:18130},{host:'127.0.0.2',port:18130}] |
timeout | Yes | Connection timeout in milliseconds | 30000 |
ca | Yes | Root certificate of the contract blockchain | Obtain from the BaaS platform |
cert | Yes | Client certificate file | Obtain from the BaaS platform |
key | Yes | Client private key file used to generate a certificate request file for certificate application | Generate using the key generation tool |
userPublicKey | Yes | Account public key (hexadecimal string) | 0x971c77d3... |
userPrivateKey | Yes | Account private key (hexadecimal string) | 0x4015e396... |
userRecoverPublicKey | Yes | Account recovery public key (hexadecimal string) | 0xb961f6a1... |
userRecoverPrivateKey | Yes | Account recovery private key (hexadecimal string) | 0x44a973e5... |
passphrase | Yes | Password for client.key, used for TLS or HTTPS connections | 123abc |
checkServerIdentity | No | Boolean. When true, verifies that the server's CN field matches its hostname (TLS only). Default: false. | false |
tx_querycount | No | Number of retries when polling QueryTransactionReceipt after submitting a transaction. Default: 3. | 5 |
tx_querytime | No | Polling interval in milliseconds for QueryTransactionReceipt. Default: 3000. | 200 |
What's next
Submit your first transaction — see the JS SDK API reference for available operations
Handle errors — check Error codes for the full list of
return_codevalues and their meaningsExplore the full SDK — see JS SDK introduction for advanced usage and all supported operations