> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Use getBlocksWithLimit

> Learn getBlocksWithLimit use cases, code examples, request parameters, response structure, and tips.

The [`getBlocksWithLimit`](https://www.helius.dev/docs/api-reference/rpc/http/getblockswithlimit) RPC method allows you to retrieve a list of confirmed block slot numbers, starting from a specified slot and returning up to a given limit. This is useful when you need a specific number of subsequent confirmed [blocks](https://www.helius.dev/blog/solana-slots-blocks-and-epochs) from a certain point in the ledger.

<Warning>
  **Avoid Batching for Better Performance**

  Batching archival methods significantly increases latency. Batches over 10 requests are not allowed.
</Warning>

## Common Use Cases

* **Fetching a Fixed Number of Subsequent Blocks:** Get a defined number of block slots that were confirmed after a particular start slot.
* **Paginated Block Exploration:** Retrieve blocks in chunks when analyzing a segment of the blockchain.
* **Recent Block Monitoring:** Get the last N blocks from a known recent slot.

## Request Parameters

The `getBlocksWithLimit` method takes the following parameters:

1. **`start_slot`** (u64, required): The first slot to consider (inclusive).
2. **`limit`** (u64, required): The maximum number of block slots to return. The total number of slots queried (from `start_slot` up to `limit` blocks after it) **must not exceed 500,000 slots**.
3. **`commitment`** (string, optional): Specifies the commitment level for the query. If omitted, the default commitment of the node is used. This is passed as the sole field in a configuration object as the last parameter.

## Response Structure

The `result` field of the JSON-RPC response will be an array of u64 integers. Each integer in the array represents a confirmed block slot number, starting from `start_slot` up to the specified `limit`.

* Example: If `start_slot` is 5 and `limit` is 3, a possible result is `[5, 6, 7]`.

## Examples

### 1. Get a Limited Number of Blocks from a Start Slot

This example fetches a list of up to 5 confirmed block slots starting from slot `280000000`.

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocksWithLimit",
      "params": [
        280000000, 
        5 
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksWithLimitExample(startSlot, limit) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocksWithLimit(startSlot, limit);
      console.log(`Up to ${limit} confirmed blocks starting from slot ${startSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks with limit:', error);
    }
  }

  // Example usage:
  const startSlot = 280000000;
  const limit = 5;
  getBlocksWithLimitExample(startSlot, limit);
  ```
</CodeGroup>

### 2. Get Blocks With Limit and a Specific Commitment Level

This example fetches up to 3 blocks starting from slot `290000000` using the `confirmed` commitment level.

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocksWithLimit",
      "params": [
        290000000,
        3,
        { "commitment": "confirmed" }
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksWithLimitAndCommitment(startSlot, limit) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocksWithLimit(startSlot, limit, { commitment: 'confirmed' });
      console.log(`Up to ${limit} confirmed blocks (with 'confirmed' commitment) starting from slot ${startSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks with limit and commitment:', error);
    }
  }

  // Example usage:
  const commitStartSlot = 290000000;
  const commitLimit = 3;
  getBlocksWithLimitAndCommitment(commitStartSlot, commitLimit);
  ```
</CodeGroup>

## Developer Tips

* **Range Clarification:** The `limit` parameter dictates the maximum number of block *slots* to return. The 500,000 slot constraint applies to the conceptual range scanned (i.e., from `start_slot` to `start_slot + limit - 1`). Ensure this conceptual range doesn't exceed 500,000 slots, even if fewer actual blocks are found and returned within that range.
* **Node Data Availability:** Nodes may not retain information for all historical slots. A very old `start_slot` might return fewer blocks than the specified `limit`, or an empty array, depending on the node's ledger retention.
* **Block Confirmation:** This method returns *confirmed* blocks. The exact set of blocks can vary slightly depending on the chosen `commitment` level and which node you query, especially for very recent slots.
* **Use Case Specificity:** `getBlocksWithLimit` is ideal when you know the starting point and need a fixed number of subsequent blocks. If you need all blocks in a known slot range, `getBlocks` might be more appropriate.

This guide provides a clear overview of how to use the `getBlocksWithLimit` RPC method to list a specific number of confirmed block slots on the Solana network.

## Related Methods

<CardGroup cols={2}>
  <Card title="getBlocks" href="/api-reference/rpc/http/getblocks">
    Get all blocks within a slot range
  </Card>

  <Card title="getBlock" href="/api-reference/rpc/http/getblock">
    Get detailed information for a specific block
  </Card>
</CardGroup>
