Skip to content

About

NPM Package for Mempool.Space API.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

Mempool JS API

npm version NPM Known Vulnerabilities License: MIT

NPM package module for Mempool APIs.

Documentation: https://mempool.space/api


Installation

ES Modules

Install the npm module.

# npm
$ npm install @mempool/mempool.js --save

# yarn
$ yarn add @mempool/mempool.js

Or 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'
});

Opt-in request caching

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: 30s to 5m
  • Address, stats, and Lightning summaries: 5s to 30s
  • Mempool state, fees, and tips: 1s to 5s, or disable caching if you need the freshest view

Security and behavior notes:

  • Requests are not cached unless cache.enabled is true.
  • POST and other non-GET requests are never cached.
  • Errors are never cached.
  • Requests carrying authorization, cookie, proxy-authorization, set-cookie, or x-api-key headers are skipped unless you explicitly provide a custom store and set allowAuthorizedRequests: 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.

CommonJS

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'
});

Features


Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.


License MIT

About

NPM Package for Mempool.Space API.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages