I am using the code syntax provided by mempool.space for a POST transaction request from their website here:


const mempoolJS = require("@mempool/mempool.js")

const init = async () => {
  const { bitcoin: { transactions } } = mempoolJS({
    hostname: 'mempool.space'

  const txHex = '0200000001fd5b5fcd1cb066c27cfc9fda5428b9be850b81ac440ea51f1ddba2f987189ac1010000008a4730440220686a40e9d2dbffeab4ca1ff66341d06a17806767f12a1fc4f55740a7af24c6b5022049dd3c9a85ac6c51fecd5f4baff7782a518781bbdd94453c8383755e24ba755c01410436d554adf4a3eb03a317c77aa4020a7bba62999df633bba0ea8f83f48b9e01b0861d3b3c796840f982ee6b14c3c4b7ad04fcfcc3774f81bff9aaf52a15751fedfdffffff02416c00000000000017a914bc791b2afdfe1e1b5650864a9297b20d74c61f4787d71d0000000000001976a9140a59837ccd4df25adc31cdad39be6a8d97557ed688ac00000000';

  const txid = await transactions.postTx({ txHex });

This code snippet isn't working for me. I keep getting a UnhandledPromiseRejectionWarning: Error: Request failed with status code 400. I am unsure if it's because I am using the require instead of import? Or I am not correctly sending a POST request? Or not handling the Promise syntax correctly?

Would appreciate suggestions.

  • Try this: pastebin.com/raw/3DL7ULsW
    – user103136
    Sep 4, 2021 at 10:34
  • Would I be entering all this into the same .js file?
    – jiamijiang
    Sep 4, 2021 at 12:46
  • I will share the steps for this workaround in answer below.
    – user103136
    Sep 4, 2021 at 13:03

2 Answers 2


I tested your code and apparently the code is right but your Hex string is wrong. The API response is as follows:

data: 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}'

Make sure that you have right Hex to post your tx. :)

  1. Create a transaction and get hex:
  1. Use Postman to send a POST request with:

    URL: https://mempool.space/testnet/api/tx

    Body: Transaction hex

  2. It will return transaction id:



Doing same thing using JavaScript:

  1. Click on </>


  1. Select one of the options for JavaScript. I used JavaScript-Fetch:
var myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");

var raw = "020000000001015af44aa9c7e14d679cea821d3fc5981f071910f1509f6a6e843ae154cec4b0230100000000fdffffff02a0860100000000001600141c97aee9cf15d8a9016c3a98c5a503ed49d012caec2102000000000016001463924d833ecc676de6dec0cf6c72c6c94bc1c5350247304402206fbb992ce8f8e35b5c229dae731709c7d82de404d882e62271ce6e7551e103ff02203e94d589766db4e583d862b6ad3951fe62c498b11fdeae85bfc37ef61e2af13a0121039072d28be7d83a8f4b1983da7a74d2bd28e80ee79d4ee2bf603e101865099378dce91f00";

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'

fetch("https://mempool.space/testnet/api/tx", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

  1. Run this on https://jsfiddle.net/ and you will get the transaction id in response:



  • Thank you. This was helpful. Except I had to tweak the top line to this: ``` var myHeaders = new fetch.Headers() ``` Also, I got an error back from their server saying: ``` sendrawtransaction RPC error: {"code":-25,"message":"bad-txns-inputs-missingorspent"} ``` But I am sure there is an input already specified in my raw tx hex. I'll share the hex below.
    – jiamijiang
    Sep 5, 2021 at 2:05
  • 0200000001661f29d4f3a3ccedf3865b51ade356715f8b201c6c1299c0ab64ba502d4ae274000000006a4730440220516e15b6354f17e3f02711107624005e6ccd26cf61ef21b35e91315dfbd33e9f022049dadb73ca3e814de9da0b35ff20a8ca82147dda138ede948ccac5621a5bc1bb01210384485d43e938f8c68c4ac903d4d5a97fe842f66c12b860d8d2e1242d2b01fc1fffffffff0160ea00000000000016001490638c5df76b1a37dc852e21ed5be5b66dbe199100000000
    – jiamijiang
    Sep 5, 2021 at 2:06
  • There is something wrong with the transaction. Maybe some inputs already spent.
    – user103136
    Sep 5, 2021 at 9:30
  • actually the reason was because i was signing it incorrectly. But its all fixed now. Thanks again.
    – jiamijiang
    Sep 13, 2021 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.