# getBlockTime

The `getBlockTime` RPC method returns the **estimated Unix timestamp** for when a given block (identified by its **slot number**) was produced. The timestamp is expressed as seconds since the Unix epoch. This method is essential for aligning on-chain events with real-world time.

***

#### ✅ Common Use Cases

* **Timestamping On-Chain Events**\
  Determine the approximate real-world time a block was created to timestamp program executions, transactions, or events.
* **Analyze Block Intervals**\
  Calculate time differences between slots to examine validator behavior, slot scheduling, or block spacing.
* **Correlate Off-Chain Events**\
  Match external data (e.g. trades, system logs) with blockchain activity using slot-to-time mapping.

***

#### 🛠 Request Parameters

* **`slot`** (*u64, required*):\
  The slot number for which to retrieve the estimated production time.

***

#### 📦 Response Structure

The `result` field will return:

* **`timestamp`** (*i64*):\
  The block’s estimated Unix timestamp (in seconds).
* **`null`**:\
  Returned if the timestamp is unavailable—typically because:
  * The block is too old and the data has been pruned.
  * The slot was skipped and has no associated block or timestamp.

#### 💡 Example

**Get Estimated Production Time for a Specific Slot**

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getBlockTime",
  "params": [355104000]
}

```

**Code Examples**

{% tabs %}
{% tab title="Nodejs" %}

```javascript
const fetch = require('node-fetch');

async function getBlockTime(rpcUrl) {
  try {
    const response = await fetch(rpcUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 1,
        method: 'getBlockTime',
        "params": [
            355104000 
          ]
      }),
    });

    const data = await response.json();
    
    // Print the exact full response
    console.log('Full RPC Response:');
    console.log(JSON.stringify(data, null, 2));
    
    return data;
  } catch (error) {
    console.error('Error getting health:', error.message);
    return null;
  }
}

// Example usage
const RPC_URL = 'https://rpc.coinvera.io/?x-api-key=your-coinvera-x-api-key';

getBlockTime(RPC_URL);
```

{% endtab %}

{% tab title="Python" %}

```python
import json
import asyncio
import aiohttp

async def get_block_time(rpc_url):
    try:
        payload = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "getBlockTime",
            "params": [
                355104000
            ]
        }
        
        async with aiohttp.ClientSession() as session:
            async with session.post(
                rpc_url,
                headers={'Content-Type': 'application/json'},
                json=payload
            ) as response:
                data = await response.json()
                
                # Print the exact full response
                print('Full RPC Response:')
                print(json.dumps(data, indent=2))
                
                return data
                
    except Exception as error:
        print(f'Error getting health: {error}')
        return None

# Example usage
async def main():
    RPC_URL = 'https://rpc.coinvera.io/?x-api-key=your-coinvera-x-api-key'
    await get_block_time(RPC_URL)

# Run the async function
if __name__ == "__main__":
    asyncio.run(main())
```

{% endtab %}
{% endtabs %}

**Example Response**

```json
{
  "jsonrpc": "2.0",
  "result": 1753231651,
  "id": 1
}
```

📌 Replace `355104000` with a **recent, confirmed slot number** from your target cluster (e.g., Mainnet or Devnet).

#### 🧠 Developer Tips

* **Timestamp Availability May Vary**\
  Older or skipped slots may return `null`. This is due to pruning or the absence of block production.
* **Estimated, Not Exact**\
  The returned timestamp is **stake-weighted**, based on vote timestamps. It’s generally accurate but not cryptographically guaranteed.
* **Node Variability**\
  Timestamps may differ slightly between RPC providers depending on validator reports and ledger history. For recent blocks, confirmation delays may affect availability.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coinvera.io/integration/solana/rpc/getblocktime.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
