# getblocktemplate

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

POST https://bitcoin-mainnet.g.alchemy.com/v2/{apiKey}

Returns data needed to construct a block to mine. This method is used by miners to request a block template  for mining new blocks. It can be customized with various parameters to specify the desired mining capabilities and rules.


Reference: https://www.alchemy.com/docs/chains/bitcoin/bitcoin-api-endpoints/getblocktemplate

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| template_request | object | No | A JSON object containing the template request parameters. |

## Result

**result** (object): A block template object containing all necessary data for mining.

## Example

### Request

```json
{
  "jsonrpc": "2.0",
  "method": "getblocktemplate",
  "params": [
    {
      "mode": "template",
      "capabilities": [
        "coinbasetxn",
        "workid",
        "coinbase/append"
      ],
      "rules": [
        "segwit"
      ]
    }
  ],
  "id": 1
}
```

### Response

```json
{
  "jsonrpc": "2.0",
  "result": {
    "version": 536870912,
    "previousblockhash": "00000000000000000000669ed57030eb18020ee7029c064f10505156be203d80",
    "transactions": [],
    "coinbaseaux": {
      "flags": ""
    },
    "coinbasevalue": 625000000,
    "target": "0000000000000000000248b60000000000000000000000000000000000000000",
    "mintime": 1745928076,
    "mutable": [
      "time",
      "transactions",
      "prevblock"
    ],
    "noncerange": "00000000ffffffff",
    "sigoplimit": 80000,
    "sizelimit": 4000000,
    "curtime": 1745931642,
    "bits": "170248b6",
    "height": 894471
  },
  "id": 1
}
```

## Code Examples

### cURL

```bash
curl --request POST \
  --url https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo \
  --header 'Content-Type: application/json' \
  --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getblocktemplate",
  "params": [
    {
      "mode": "template",
      "capabilities": [
        "coinbasetxn",
        "workid",
        "coinbase/append"
      ],
      "rules": [
        "segwit"
      ]
    }
  ]
}'
```

### JavaScript

```javascript
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: 1,
    method: 'getblocktemplate',
    params: [
      {
        mode: 'template',
        capabilities: ['coinbasetxn', 'workid', 'coinbase/append'],
        rules: ['segwit']
      }
    ]
  })
};

fetch('https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo"

payload = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getblocktemplate",
    "params": [
        {
            "mode": "template",
            "capabilities": ["coinbasetxn", "workid", "coinbase/append"],
            "rules": ["segwit"]
        }
    ]
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

### Go

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo"

	payload := strings.NewReader("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getblocktemplate\",\n  \"params\": [\n    {\n      \"mode\": \"template\",\n      \"capabilities\": [\n        \"coinbasetxn\",\n        \"workid\",\n        \"coinbase/append\"\n      ],\n      \"rules\": [\n        \"segwit\"\n      ]\n    }\n  ]\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
```

### Java

```java
HttpResponse<String> response = Unirest.post("https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo")
  .header("Content-Type", "application/json")
  .body("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getblocktemplate\",\n  \"params\": [\n    {\n      \"mode\": \"template\",\n      \"capabilities\": [\n        \"coinbasetxn\",\n        \"workid\",\n        \"coinbase/append\"\n      ],\n      \"rules\": [\n        \"segwit\"\n      ]\n    }\n  ]\n}")
  .asString();
```

### C#

```csharp
using RestSharp;


var options = new RestClientOptions("https://bitcoin-mainnet.g.alchemy.com/v2/docs-demo");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"getblocktemplate\",\n  \"params\": [\n    {\n      \"mode\": \"template\",\n      \"capabilities\": [\n        \"coinbasetxn\",\n        \"workid\",\n        \"coinbase/append\"\n      ],\n      \"rules\": [\n        \"segwit\"\n      ]\n    }\n  ]\n}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);

```


## OpenRPC Method Specification

```yaml
name: getblocktemplate
summary: Request a block template for mining
description: |
  Returns data needed to construct a block to mine. This method is used by miners to request a block template  for mining new blocks. It can be customized with various parameters to specify the desired mining capabilities and rules.
params:
  - name: template_request
    required: false
    description: A JSON object containing the template request parameters.
    schema:
      type: object
      properties:
        mode:
          type: string
          description: |
            Specifies the mode of operation. Can be 'template' for a block template or 'proposal' for submitting a block proposal.
          enum:
            - template
            - proposal
        capabilities:
          type: array
          description: |
            A list of strings specifying the capabilities the miner supports.  This can include 'coinbasetxn', 'workid', 'coinbase/append', etc.
          items:
            type: string
        rules:
          type: array
          description: |
            A list of strings specifying the rules the miner will support.  For example, 'segwit' indicates support for Segregated Witness.
          items:
            type: string
result:
  name: result
  description: A block template object containing all necessary data for mining.
  schema:
    type: object
    properties:
      version:
        type: integer
        description: The block version.
      previousblockhash:
        title: Bitcoin Block Hash
        type: string
        pattern: ^[a-fA-F0-9]{64}$
        description: A 64-character hex string representing the block hash.
      transactions:
        type: array
        description: Contents of non-coinbase transactions that should be included in the next block.
        items:
          type: object
      coinbaseaux:
        type: object
        description: Data that should be included in the coinbase's scriptSig content.
      coinbasevalue:
        type: integer
        description: Maximum allowable input to coinbase transaction, including the mining reward and transaction fees (in satoshis).
      target:
        type: string
        description: The hash target for the block.
      mintime:
        type: integer
        description: The minimum timestamp appropriate for the next block time in seconds since epoch.
      mutable:
        type: array
        description: List of ways the block template may be changed.
        items:
          type: string
      noncerange:
        type: string
        description: Range of valid nonces.
      sigoplimit:
        type: integer
        description: Limit of signature operations in blocks.
      sizelimit:
        type: integer
        description: Limit of block size.
      curtime:
        type: integer
        description: Current timestamp in seconds since epoch.
      bits:
        type: string
        description: Compressed target of the next block.
      height:
        type: integer
        description: The height of the next block.
examples:
  - name: getblocktemplate example
    params:
      - name: template_request
        value:
          mode: template
          capabilities:
            - coinbasetxn
            - workid
            - coinbase/append
          rules:
            - segwit
    result:
      name: result
      value:
        version: 536870912
        previousblockhash: 00000000000000000000669ed57030eb18020ee7029c064f10505156be203d80
        transactions: []
        coinbaseaux:
          flags: ''
        coinbasevalue: 625000000
        target: 0000000000000000000248b60000000000000000000000000000000000000000
        mintime: 1745928076
        mutable:
          - time
          - transactions
          - prevblock
        noncerange: 00000000ffffffff
        sigoplimit: 80000
        sizelimit: 4000000
        curtime: 1745931642
        bits: 170248b6
        height: 894471
```
