# Javascript SDK Reference

The Bloq Javascript SDK enables you to work with Bloq services easily by simplifying authentication.

# Requirements

The Javascript SDK has the following system requirements to install the library:

# Install

To install the Bloq Javascript SDK, use either:

npm install --save @bloq/cloud-sdk

OR

yarn add @bloq/cloud-sdk

# Use

const { auth, connect } = require('@bloq/cloud-sdk')

const clientId = 'CLIENT_ID'
const clientSecret = 'CLIENT_SECRET'

const connectClient = connect.http({
  auth: {
    clientId,
    clientSecret
  },
  coin: 'btc',
  network: 'mainnet'
})

# Auth API

The auth API helps to simplify the authentication process.

# Access Token

accessToken()

This method returns a promise that resolves in a new created access token using the provided client ID and secret.

The Access Token is required to interact with Bloq services. It usually travels in an Authorization header value.

# Code sample

const { auth } = require('@bloq/cloud-sdk')

const clientId = 'CLIENT_ID'
const clientSecret = 'CLIENT_SECRET'

const client = auth({ clientId, clientSecret })

client.accessToken()
  .then(function (accessToken) {
    // Use access token
  })

# Connect API

The Connect API enables you to interact with the Connect REST Interface. The interface is based on Bitcore's Insight APIs. The API has support for Bitcoin (BTC) Mainnet and Bitcoin Cash (BCH) Mainnet.

To create a new client instance, these items are required:

  • clientId: The user client Id, this is used to authenticate the requests. Read more about these keys and how to create them
  • clientSecret: The user client secret, this is used to authenticate the requests. Read more about these keys and how to create them
  • coin: The blockchain coin, current supported values are btc (Bitcoin) and bch (Bitcoin Cash)
  • network: The blockchain network, current supported values are mainnet and testnet

# Use

const { connect } = require('@bloq/cloud-sdk')

const clientId = 'CLIENT_ID'
const clientSecret = 'CLIENT_SECRET'
const coin = 'btc'
const network = 'mainnet'

const client = connect.http({
  auth: {
    clientId,
    clientSecret
  },
  coin,
  network
})

client.block('00000000dfd5d65c9d8561b4b8f60a63018fe3933ecb131fb37f905f87da951a')
  .then(function (block) {
    // Use block
  })

# Address

address({ address, from, to, noTxList })

Get properties of an address by address

This method fetches the properties of an address by address.

# Parameters

Parameter Type Required Description
addr string true The address to be fetched
from number false Number to paginate transactions from addresses
to number false Number to paginate transactions to addresses
noTxList number false Omits the list of transactions when value equals '1'

# Code sample

client.address({
  address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
  from: 1,
  to: 2,
  noTxList: 1
})
  .then(function (address) {
    // Use address
  })

# Address Balance

addressBalance(address)

Get address balance

This method fetches an address balance in satoshis.

# Parameters

Parameter Type Required Description
addr string true The address of the balance to be fetched

# Code sample

client.addressBalance('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
  .then(function (balance) {
    // Use balance in satoshis
  })

# Address Total Received

addressTotalReceived(address)

Get address total received

This method fetches an address total received in satoshis.

# Parameters

Parameter Type Required Description
addr string true The address of the total received balance to be fetched

# Code sample

client.addressTotalReceived('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
  .then(function (totalReceived) {
    // Use total received in satoshis
  })

# Address Total Sent

addressTotalSent(address)

Get address total sent

This method fetches an address total sent in satoshis.

# Parameters

Parameter Type Required Description
addr string true The address of the total sent balance to be fetched

# Code sample

client.addressTotalSent('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
  .then(function (TotalSent) {
    // Use total sent in satoshis
  })

# Address Unconfirmed Balance

addressUnconfirmedBalance(address)

Get address unconfirmed balance

This method fetches an address unconfirmed balance in satoshis.

# Parameters

Parameter Type Required Description
addr string true The address of the unconfirmed balance to be fetched

# Code sample

client.addressUnconfirmedBalance('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
  .then(function (unconfirmedBalance) {
    // Use unconfirmed Balance
  })

# Address Unspent Outputs

addressUnspentOutputs([addresses])

Get addresses unspent outputs

This method fetches unspent outputs from multiple addresses in satoshis.

# Parameters

Parameter Type Required Description
addresses string or array true none

# Code sample

client.addressUnspentOutputs('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
  .then(function (unspentOutputs) {
    // Use unspent outputs
  })

client.addressUnspentOutputs(['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', '12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S'])
  .then(function (unspentOutputs) {
    // Use unspent outputs
  })

# Address Transactions

addressTransactions({ address, pageNum })

Get transactions by address

This method fetches transactions by address.

# Parameters

Parameter Type Required Description
address string true The address associated to the transactions to be fetched
pageNum string false The page number

# Code sample

client.addressTransactions({
  address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
  pageNum: 2
})
  .then(function (transactions) {
    // Use transactions
  })

# Addresses Transactions

addressesTransactions({ addresses, from, to, noAsm, noScriptSig, noSpent })

Get transactions for one or more addresses

This method fetches transactions for one or more addresses.

# Parameters

Parameter Type Required Description
addrs string or array true The addresses associated to the transactions to be fetched
from number false Number to paginate addresses transactions from
to number false Number to paginate addresses transactions to
noAsm boolean false Omits script asm from results
noScriptSig boolean false Omits the scriptSig from all inputs
noSpent boolean false Omits spent information per output

# Code sample

client.addressesTransactions({
  addresses: ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', '12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S'],
  from: 2,
  to: 5,
  noAsm: true,
  noScriptSig: true,
  noSpent: true
})
  .then(function (transactions) {
    // Use transactions
  })

# Block Transactions

blockTransactions({ block, pageNum })

Get transactions by block

This method fetches transactions by block.

# Parameters

Parameter Type Required Description
block string true The block hash associated to the transactions to be fetched
pageNum number false The page number

# Code sample

client.blockTransactions({
  block: '00000000000ef57d9307f89f36e052afdfceaeb71deb8d36cfdd11dcefdc2490',
  pageNum: 2
})
  .then(function (transactions) {
    // Use transactions
  })

# Block

block(hash)

Get block by block hash

This method fetches a block by block hash.

# Parameters

Parameter Type Required Description
hash string true The hash of the block to be fetched

# Code sample

client.block('00000000000ef57d9307f89f36e052afdfceaeb71deb8d36cfdd11dcefdc2490')
  .then(function (block) {
    // Use block
  })

# Block Hash

blockHash(height)

Get block hash by height

This method fetches a block by block height.

# Parameters

Parameter Type Required Description
height number true The height of the block hash to be fetched. A height of 0 is the genesis block.

# Code sample

client.blockHash(100)
  .then(function (block) {
    // Use block hash
  })

# Blocks

blocks({ limit, date })

Get block summary by date

This method fetches block summaries by date.

# Parameters

Parameter Type Required Description
limit integer false The number of blocks to display
date string(date) false The date of the blocks to display

# Code sample

client.blocks({
  limit: 1,
  blockDate: '2016-04-22'
})
  .then(function (blocks) {
    // Use blocks
  })

# Estimate Fee

estimateFee(nbBlocks)

Get fee estimation

This method retrieves fee estimates.

# Parameters

Parameter Type Required Description
nbBlocks number false The number of blocks to confirm a transaction

# Code sample

client.estimateFee(2)
  .then(function (fee) {
    // Use fee
  })

# Raw Block

rawBlock(hash)

Get raw block by hash

This method fetches a block by block height.

# Parameters

Parameter Type Required Description
hash string true The hash of the block to be fetched

# Code sample

client.rawBlock('00000000000ef57d9307f89f36e052afdfceaeb71deb8d36cfdd11dcefdc2490')
  .then(function (rawBlock) {
    // Use raw block
  })

# Send Transaction

sendTransaction(rawtx)

Broadcast transaction

This method broadcasts a signed transaction.

# Parameters

Parameter Type Required Description
rawtx string true Signed transaction as hex string

# Code sample

client.sendTransaction(rawtx)
  .then(function (rawtx) {
    // Use raw transaction
  })

# Status

status(query)

Get the status of the bitcoin network

This method fetches the status of the bitcoin network.

# Parameters

Parameter Type Required Description
query string false A specific attribute from the status code. Possible values are: getInfo, getDifficulty, getBestBlockHash and getLastBlockHash

# Code sample

client.status()
  .then(function (status) {
    // Use status
  })

# Transaction

transaction(hash)

Get transaction by transaction hash

This method fetches transaction by transaction hash.

# Parameters

Parameter Type Required Description
hash string true The hash of the transaction to be fetched

# Code sample

client.transaction('f64a111dba007fae77e0ad0488d9844b9e8b075fda321265a0e88556c98f1c94')
  .then(function (transaction) {
    // Use transaction
  })
Last updated: 4/28/2020, 6:18:11 AM