2021-04-08 10:15:30 -03:00
# Mempool JS API
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[](https://www.npmjs.org/package/mempool-js)
[](https://david-dm.org/mempool/mempool-js#info =dependencies)
[](https://snyk.io/test/github/mempool/mempool-js)
2021-02-10 11:20:01 -03:00
[](https://opensource.org/licenses/MIT)
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
NPM package module for Mempool JS API.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Documentation: [https://mempool.space/api ](https://mempool.space/api )
2021-02-10 11:20:01 -03:00
---
2021-02-08 16:54:37 -03:00
## Features
2021-04-08 10:15:30 -03:00
- [Instalation ](#installation )
- [CommonJS ](#commonjs )
- [NodeJS ](#es-modules )
- [Usage ](#usage )
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
- Addresses
- [Get Address ](#get-address )
- [Get Address Txs ](#get-address-txs )
- [Get Address Txs Chain ](#get-address-txs-chain )
- [Get Address Txs Mempool ](#get-address-txs-mempool )
- [Get Address Txs Utxo ](#get-address-txs-utxo )
2021-02-08 16:54:37 -03:00
- Blocks
2021-02-08 16:59:48 -03:00
- [Get Block ](#get-block )
- [Get Block Status ](#get-block-status )
- [Get Block Txs ](#get-block-txs )
- [Get Block Txids ](#get-block-txids )
- [Get Block Txid ](#get-block-txid )
- [Get Block Raw ](#get-block-raw )
- [Get Blocks Height ](#get-blocks-height )
- [Get Blocks ](#get-blocks )
- [Get Blocks Tip Height ](#get-blocks-tip-height )
- [Get Blocks Tip Hash ](#get-blocks-tip-hash )
2021-04-08 10:15:30 -03:00
- Fees
- [Get Fees Recommended ](#get-fees-recommended )
- [Get Fees Mempool Blocks ](#get-fees-mempool-blocks )
- Mempool
- [Get Mempool ](#get-mempool )
- [Get Mempool Recent ](#get-mempool-recent )
- [Get Mempool Txids ](#get-mempool-txids )
2021-02-08 16:54:37 -03:00
- Transactions
2021-02-08 16:59:48 -03:00
- [Get Tx ](#get-tx )
- [Get Tx Status ](#get-tx-status )
- [Get Tx Hex ](#get-tx-hex )
- [Get Tx Raw ](#get-tx-raw )
- [Get Tx Merkle Block Proof ](#get-tx-merkle-block-proof )
- [Get Tx Merkle Proof ](#get-tx-merkle-proof )
- [Get Tx Outspend ](#get-tx-outspend )
- [Get Tx Outspends ](#get-tx-outspends )
- [Post Tx Outspends ]($post-tx-outspends )
2021-02-09 16:22:48 -03:00
- [Websocket ](#websocket )
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
- [Contribute ](#contribute )
- [License ](#license )
2021-02-08 16:54:37 -03:00
---
2021-04-08 10:15:30 -03:00
## **Installation**
### **ES Modules**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
First, install the npm module.
2021-02-08 16:54:37 -03:00
```bash
2021-04-08 10:15:30 -03:00
# npm
$ npm install @mempool/mempool -js --save
# yarn
$ yarn add @mempool/mempool -js
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
Or if you're not into package management, just [download a ZIP ](https://github.com/mempool/mempool-js/archive/refs/heads/main.zip ) file.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Then import the module.
```js
import mempoolJS from '@mempool/mempool -js';
const {
addresses,
blocks,
fees,
mempool,
transactions,
websocket,
} = mempoolJS();
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
**Custom Endpoints (Optional)**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
You can set your custom **API** and **WS** endpoints.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
```js
import mempoolJS from '@mempool/mempool -js';
const { address } = mempoolJS({
apiEndpoint: 'https://mempool.space/api/',
websocketEndpoint: 'wss://mempool.space/api/v1/ws',
});
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **CommonJS**
First, include the script located on the `dist` folder.
```html
< script type = "text/javascript" src = "./dist/mempool.min.js" > < / script >
```
Now, you have an access to a variable function to access the API methods.
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const {
addresses,
blocks,
fees,
mempool,
transactions,
websocket,
} = mempoolJS();
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
## **Usage**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Address**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns details about an address. Available fields: `address` , `chain_stats` , and `mempool_stats` . `{chain,mempool}\_stats` each contain an object with `tx_count` , `funded_txo_count` , `funded_txo_sum` , `spent_txo_count` , and `spent_txo_sum` .
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Parameters:
- {string} address - Address id.
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { addresses } = mempoolJS();
const address = await addresses.getAddress('15e10745f15593a...');
console.log(address);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Address Txs**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Get transaction history for the specified address/scripthash, sorted with newest first. Returns up to 50 mempool transactions plus the first 25 confirmed transactions. You can request more confirmed transactions using `:last_seen_txid` (see below).
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Parameters:
- {string} address - Address id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { addresses } = mempoolJS();
const addressTxs = await addresses.getAddressTxs('15e10745f15593a...');
console.log(addressTxs);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Address Txs Chain**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Get confirmed transaction history for the specified address/scripthash, sorted with newest first. Returns 25 transactions per page. More can be requested by specifying the last txid seen by the previous query.
Parameters:
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
- {string} address - Address id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { addresses } = mempoolJS();
const addressTxsChain = await addresses.getAddressTxsChain(
'15e10745f15593a...'
);
console.log(addressTxsChain);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Address Txs Mempool**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Get unconfirmed transaction history for the specified address/scripthash. Returns up to 50 transactions (no paging).
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Parameters:
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
- {string} address - Address id.
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { addresses } = mempoolJS();
const addressTxsMempool = await addresses.getAddressTxsMempool(
'15e10745f15593a...'
);
console.log(addressTxsMempool);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Address Txs Utxo**
Get unconfirmed transaction history for the specified address/scripthash. Returns up to 50 transactions (no paging).
Parameters:
- {string} address - Address id.
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
```js
const { addresses } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const addressTxsUtxo = await addresses.getAddressTxsUtxo('15e10745f15593a...');
console.log(addressTxsUtxo);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Block**
Returns details about a block. Available fields: `id` , `height` , `version` , `timestamp` , `bits` , `nonce` , `merkle_root` , `tx_count` , `size` , `weight` , and `previousblockhash` .
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} hash - Hash from a block
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
const block = await blocks.getBlock('000000000000000015dc...');
2021-02-08 16:54:37 -03:00
console.log(block);
```
2021-04-08 10:15:30 -03:00
### **Get Block Status**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the confirmation status of a block. Available fields: `in_best_chain` (boolean, false for orphaned blocks), `next_best` (the hash of the next block, only available for blocks in the best chain).
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} hash - Hash from a block
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
const blockStatus = await blocks.getBlockStatus('000000000000000015dc...');
2021-02-08 16:54:37 -03:00
console.log(blockStatus);
```
2021-04-08 10:15:30 -03:00
### **Get Block Txs**
2021-02-08 16:54:37 -03:00
Returns a list of transactions in the block (up to 25 transactions beginning at start_index). Transactions returned here do not have the status field, since all the transactions share the same block and confirmation status.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
- {number} params.start_index - Default: 25
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
2021-02-08 16:54:37 -03:00
const blockTxs = await blocks.getBlockTxs({
2021-04-08 10:15:30 -03:00
hash: '000000000000000015dc...',
2021-02-08 16:54:37 -03:00
});
console.log(blockTxs);
```
2021-04-08 10:15:30 -03:00
### **Get Block Txids**
2021-02-08 16:54:37 -03:00
Returns a list of all txids in the block.
Parameters:
2021-04-08 10:15:30 -03:00
- {string} hash - Hash from a block
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
const blockTxids = await blocks.getBlockTxids('000000000000000015dc...');
2021-02-08 16:54:37 -03:00
console.log(blockTxids);
```
2021-04-08 10:15:30 -03:00
### **Get Block Txid**
2021-02-08 16:54:37 -03:00
Returns the transaction at index :index within the specified block.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
- {number} params.index - Index
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
2021-02-08 16:54:37 -03:00
const blockTxid = await blocks.getBlockTxid({
hash: '000000000000000015dc...',
2021-04-08 10:15:30 -03:00
index: 218,
2021-02-08 16:54:37 -03:00
});
console.log(blockTxids);
```
2021-04-08 10:15:30 -03:00
### **Get Block Raw**
2021-02-08 16:54:37 -03:00
Returns the raw block representation in binary.
Parameters:
2021-04-08 10:15:30 -03:00
- {string} hash - Hash from a block
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
const blockRaw = await blocks.getBlockRaw('000000000000000015dc...');
2021-02-08 16:54:37 -03:00
console.log(blockRaw);
```
2021-04-08 10:15:30 -03:00
### **Get Blocks Height**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the hash of the block currently at `:height` .
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {number} height - Height number from a block
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
const blockHeight = await blocks.getBlockHeight(42);
2021-02-08 16:54:37 -03:00
console.log(blockHeight);
```
2021-04-08 10:15:30 -03:00
### **Get Blocks**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the 10 newest blocks starting at the tip or at `:start_height` if specified.
2021-02-08 16:54:37 -03:00
Parameters:
- {Object} params - Params object.
- {number} params.start_height - Height from a block
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
2021-02-08 16:54:37 -03:00
const getBlocks = await blocks.getBlocks({
2021-04-08 10:15:30 -03:00
start_height: 66666,
2021-02-08 16:54:37 -03:00
});
console.log(getBlocks);
```
2021-04-08 10:15:30 -03:00
### **Get Blocks Tip Height**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the 10 newest blocks starting at the tip or at `:start_height` if specified.
2021-02-08 16:54:37 -03:00
Parameters:
- {Object} params - Params object.
- {number} params.start_height - Height from a block
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
2021-02-08 16:54:37 -03:00
const blocksTipHeight = await blocks.getBlocksTipHeight();
console.log(blocksTipHeight);
```
2021-04-08 10:15:30 -03:00
### **Get Blocks Tip Hash**
2021-02-08 16:54:37 -03:00
Returns the hash of the last block.
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/blocks.ts ) ] [ [HTML Example ](examples/html/blocks.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { blocks } = mempoolJS();
2021-02-08 16:54:37 -03:00
const blocksTipHash = await blocks.getBlocksTipHash();
console.log(blocksTipHash);
```
2021-04-08 10:15:30 -03:00
### **Get Fees Recommended**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns our currently suggested fees for new transactions.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/fees.ts ) ] [ [HTML Example ](examples/html/fees.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { fees } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const feesRecommended = await fees.getFeesRecommended();
console.log(feesRecommended);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Fees Mempool Blocks**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns current mempool as projected blocks.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/fees.ts ) ] [ [HTML Example ](examples/html/fees.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { fees } = mempoolJS();
const feesMempoolBlocks = await fees.getFeesMempoolBlocks();
console.log(feesMempoolBlocks);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Mempool**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns current mempool backlog statistics.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/mempool.ts ) ] [ [HTML Example ](examples/html/mempool.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
```js
const { mempool } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const getMempool = await mempool.getMempool();
console.log(getMempool);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Mempool Txids**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Get the full list of txids in the mempool as an array. The order of the `txids` is arbitrary and does not match bitcoind.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/mempool.ts ) ] [ [HTML Example ](examples/html/mempool.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
```js
const { mempool } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const getMempoolTxids = await mempool.getMempoolTxids();
console.log(getMempoolTxids);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Mempool Recent**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Get a list of the last 10 transactions to enter the mempool. Each transaction object contains simplified overview data, with the following fields: `txid` , `fee` , `vsize` , and `value` .
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/mempool.ts ) ] [ [HTML Example ](examples/html/mempool.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { mempool } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const getMempoolRecent = await mempool.getMempoolRecent();
console.log(getMempoolRecent);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns details about a transaction. Available fields: `txid` , `version` , `locktime` , `size` , `weight` , `fee` , `vin` , `vout` , and `status` .
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const tx = await transactions.getTx('15e10745f15593...');
console.log(tx);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Status**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the confirmation status of a transaction. Available fields: `confirmed` (boolean), `block_height` (optional), and `block_hash` (optional).
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txStatus = await transactions.getTxStatus('15e10745f15593...');
console.log(txStatus);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Hex**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns a transaction serialized as hex.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txHex = await transactions.getTxHex('15e10745f15593...');
console.log(txHex);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Raw**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns a transaction as binary data.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txRaw = await transactions.getTxRaw('15e10745f15593...');
console.log(txRaw);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Merkle Block Proof**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns a merkle inclusion proof for the transaction using bitcoind's merkleblock format.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof(
'15e10745f15593...'
);
console.log(txMerkleBlockProof);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Merkle Proof**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns a merkle inclusion proof for the transaction using Electrum's blockchain.transaction.get_merkle format.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txMerkleProof = await transactions.getTxMerkleProof('15e10745f15593...');
console.log(txMerkleProof);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Get Tx Outspend**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the spending status of a transaction output. Available fields: `spent` (boolean), `txid` (optional), `vin` (optional), and `status` (optional, the status of the spending tx).
2021-02-08 16:54:37 -03:00
Parameters:
- {Object} params - Params object.
2021-04-08 10:15:30 -03:00
- {string} params.txid - Transactions id.
- {number} params.vout - Vout number.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
const txOutspend = await transactions.getTxOutspend({
txid: '15e10745f15593...',
vout: 3,
2021-02-08 16:54:37 -03:00
});
2021-04-08 10:15:30 -03:00
console.log(txOutspend);
2021-02-08 16:54:37 -03:00
```
2021-04-08 10:15:30 -03:00
### **Get Tx Outspends**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Returns the spending status of all transaction outputs.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const txOutspends = await transactions.getTxOutspends('15e10745f15593...');
console.log(txOutspends);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Post Tx Outspends**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Broadcast a raw transaction to the network. The transaction should be provided as hex in the request body. The `txid` will be returned on success.
2021-02-08 16:54:37 -03:00
Parameters:
2021-04-08 10:15:30 -03:00
- {string} txid - Transactions id.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/transactions.ts ) ] [ [HTML Example ](examples/html/transactions.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { transactions } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const postTx = await transactions.postTx('15e10745f15593...');
console.log(postTx);
```
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
### **Websocket**
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Default push: `{ action: 'want', data: ['blocks', ...] }` to express what you want pushed. Available: blocks, mempool-block, live-2h-chart, and stats.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
Push transactions related to address: `{ 'track-address': '3PbJ...bF9B' }` to receive all new transactions containing that address as input or output. Returns an array of transactions. address-transactions for new mempool transactions, and block-transactions for new block confirmed transactions.
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
[ [NodeJS Example ](examples/nodejs/addresses.ts ) ] [ [HTML Example ](examples/html/addresses.html ) ] [ [Top ](#features ) ]
2021-02-08 16:54:37 -03:00
```js
2021-04-08 10:15:30 -03:00
const { websocket } = mempoolJS();
2021-02-08 16:54:37 -03:00
2021-04-08 10:15:30 -03:00
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
2021-02-09 16:22:48 -03:00
2021-04-08 10:15:30 -03:00
ws.on('message', function incoming(data) {
const res = JSON.parse(data.toString());
2021-02-09 16:22:48 -03:00
if (res.blocks) {
2021-04-08 10:15:30 -03:00
res.blocks.forEach((block: { height }) => {
2021-02-09 16:22:48 -03:00
console.log(block.height);
});
}
if (res.mempoolInfo) {
console.log(res.mempoolInfo);
}
if (res.transactions) {
console.log(res.transactions);
}
if (res.mempoolBlocks) {
console.log(res.mempoolBlocks);
}
});
```
2021-02-08 16:54:37 -03:00
---
2021-04-08 10:15:30 -03:00
## **Contributing**
2021-02-08 16:54:37 -03:00
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
---
2021-04-08 10:15:30 -03:00
## **License** [MIT](https://choosealicense.com/licenses/mit/)