getblocktemplate - Bitcoin

This method returns data needed to construct a block to work on, following BIP 22, BIP 23, BIP 9, and BIP 145. It is used by mining software.

Parameters

ParameterTypeRequiredDescription
template_requestobjectNoA JSON object specifying capabilities, rules, and mode

Request

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "getblocktemplate",
    "params": [{"rules": ["segwit"]}],
    "id": "getblock.io"
}'

example.js

javascriptconst axios = require('axios');

const response = await axios.post('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'getblocktemplate',
    params: [{"rules": ["segwit"]}],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

console.log(response.data.result);

example.py

pythonimport requests

response = requests.post(
    'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'getblocktemplate',
        'params': [{"rules": ["segwit"]}],
        'id': 'getblock.io'
    }
)

print(response.json())

example.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();

    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "getblocktemplate",
            "params": [{"rules": ["segwit"]}],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;

    println!("Result: {}", response["result"]);
    Ok(())
}

Response

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": {
        "version": 671088644,
        "previousblockhash": "0000...",
        "height": 830001,
        "curtime": 1706886600,
        "bits": "17034219",
        "target": "0000...0000",
        "mintime": 1706883600,
        "sizelimit": 4000000,
        "weightlimit": 4000000,
        "coinbasevalue": 637500000,
        "transactions": [
            {
                "data": "0200...",
                "txid": "...",
                "fee": 2820,
                "weight": 561
            }
        ],
        "longpollid": "0000...123",
        "mutable": [
            "time",
            "transactions",
            "prevblock"
        ],
        "noncerange": "00000000ffffffff"
    }
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultobjectBlock template object

Result Object

FieldTypeDescription
previousblockhashstringHash of the current tip to build on
heightnumberHeight of the block being built
coinbasevaluenumberMaximum coinbase value in satoshis (subsidy plus fees)
transactionsarrayTransactions to include, with data and fees
targetstringThe proof-of-work target

Use Cases

  • Mining Software: Construct candidate blocks to mine
  • Pool Operations: Distribute work to miners
  • Fee Analysis: Read the fees of template transactions
  • Research: Study block construction

Error Handling

Error CodeMessageDescription
403ForbiddenMissing or invalid ACCESS-TOKEN
-8Invalid parameterA parameter is out of range or malformed
-32601Method not foundThe method is not available on this endpoint
-32602Invalid paramsA parameter is missing or has the wrong type