NPM package module for Mempool APIs.
Documentation: https://mempool.space/api
Install the npm module.
# npm
$ npm install @mempool/mempool.js --save
# yarn
$ yarn add @mempool/mempool.jsOr if you're not into package management, just download a ZIP file.
Import the module.
import mempoolJS from '@mempool/mempool.js';
// default mempool.space endpoints
const { bitcoin, liquid } = mempoolJS();
// (optional) your custom endpoints
const { bitcoin } = mempoolJS({
protocol: 'https', // optional, defaults to http for localhost, otherwise https
hostname: 'mempool.space',
network: 'testnet' // 'signet' | 'testnet' | 'mainnet',
config: { // optional axios request config to add to requests
headers: {
authorization: 'Basic auth'
}
}
});
// Liquid API
const { liquid } = mempoolJS({
protocol: 'https', // optional, defaults to http for localhost, otherwise https
hostname: 'liquid.network',
network: 'liquid' // 'liquid' | 'liquidtestnet'
});Caching is disabled by default. When enabled, the client caches only GET requests, deduplicates concurrent cache misses for the same normalized request key, respects TTL expiry, and exposes cache.clear() for manual invalidation.
const client = mempoolJS({
hostname: 'mempool.space',
cache: {
enabled: true,
ttlMs: 30000, // 30 seconds
maxEntries: 100, // default bounded in-memory store size
},
});
const tx = await client.bitcoin.transactions.getTx({ txid: '...' });
await client.cache.clear();To use a custom or isolated store, pass your own store implementation or reuse the built-in bounded memory store factory:
const privateStore = mempoolJS.createMemoryCacheStore({ maxEntries: 50 });
const client = mempoolJS({
hostname: 'mempool.space',
config: {
headers: {
authorization: '******',
},
},
cache: {
enabled: true,
ttlMs: (request) => (request.url?.includes('/tx/') ? 60000 : 5000),
store: privateStore,
allowAuthorizedRequests: true,
shouldCache: (request) => request.url !== '/fees/recommended',
},
});Recommended starting TTLs:
- Confirmed transaction or block lookups:
30sto5m - Address, stats, and Lightning summaries:
5sto30s - Mempool state, fees, and tips:
1sto5s, or disable caching if you need the freshest view
Security and behavior notes:
- Requests are not cached unless
cache.enabledistrue. POSTand other non-GETrequests are never cached.- Errors are never cached.
- Requests carrying
authorization,cookie,proxy-authorization,set-cookie, orx-api-keyheaders are skipped unless you explicitly provide a custom store and setallowAuthorizedRequests: true. - The default store is per-client-instance, in-memory, and bounded; use a custom store if you need cross-instance persistence.
- Cached responses are reused client-side only. This package does not add server-side Redis, CDN, or HTTP cache headers.
Include the line below in the head tag of your html file.
<script type="text/javascript" src="https://mempool.space/mempool.js"></script>Call mempoolJS() function to access the API methods.
// default mempool.space endpoints
const { bitcoin } = mempoolJS();
// (optional) your custom endpoints
const { bitcoin } = mempoolJS({
protocol: 'https', // optional, defaults to http for localhost, otherwise https
hostname: 'mempool.space',
network: 'testnet', // 'signet' | 'testnet' | 'mainnet'
});
// Liquid API
const { liquid } = mempoolJS({
protocol: 'https', // optional, defaults to http for localhost, otherwise https
hostname: 'liquid.network',
network: 'liquid', // 'liquid' | 'liquidtestnet'
});Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.