Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE

How to Navigate API Rate Limits Post Nakamoto

The Stacks blockchain is on the brink of the Nakamoto network upgrade. One of the biggest changes coming with Nakamoto is a drastic reduction in block confirmation times, going from the current 10-20 minutes to mere seconds post-Nakamoto. How will these rapid block speeds affect the current Hiro API rate limits?

Type
Tutorial
Topic(s)
Stacks
Published
October 10, 2024
Author(s)
Developer Advocate
API rate limits
Contents

The Challenge with API Rate Limits

With Stacks blocks soon to be confirmed in seconds, the frequency at which new blocks are added to the Stacks blockchain will significantly increase. If you rely on the block endpoints of Hiro’s Stacks Blockchain API to fetch the latest block information, you will need to make API calls much more frequently to keep your applications in sync with the blockchain.

This increased frequency of API calls can lead to hitting the existing API rate limits more often—even when using an API key. Consequently, this could throttle your applications, leading to delays or failures in fetching necessary data, and ultimately degrade the user experience.

Alternative Solutions for Developers

To mitigate the challenges posed by increased block speeds and API rate limits, consider the following solutions:

  • Utilize the Client SDK for the Blockchain API
  • Implement Chainhooks for Event-Driven Architecture

Both options bypass API rate limiting and push data directly to your application without needing to make API requests. Let's delve into how each solution can help you adapt to the Nakamoto upgrade’s faster blocks.

Utilize the Client SDK for the Blockchain API

The @stacks/blockchain-api-client is a JavaScript client library that allows you to interact with the Stacks Blockchain API efficiently. By leveraging WebSockets or Socket.io, you can receive real-time updates directly, eliminating the need for continuous API requests and thus avoiding rate limits.

Why Use the Client SDK?

  • Real-time streaming: You can subscribe to live updates for blocks, transactions, and addresses.
  • Efficient data handling: Receive only relevant data when events occur.
  • Bypass rate limits: Subscriptions via WebSockets are not subjected to rate limits, unlike the Hiro-hosted Stacks API.

Getting Started With the Client SDK

First, install the client library via npm:


npm install --save @stacks/blockchain-api-client

Subscribe to Block Updates

Here's how you can subscribe to new block events using WebSockets:


import { connectWebSocketClient } from '@stacks/blockchain-api-client';
// For testnet, replace with 'wss://api.testnet.hiro.so/'
const client = await connectWebSocketClient('wss://api.mainnet.hiro.so/');
client.subscribeBlocks(event => {
  console.log('New block received:', event);
});

For more examples on how to utilize the Client SDK and subscribe to particular on-chain events, click here.

Implement Chainhooks for Event-Driven Architecture

Chainhook is a reorg-aware transaction indexing engine that enables you to build reorg-resistant, event-driven applications. Instead of polling for data, you define predicates that trigger actions (such as writing to a database or to a smart contract) when certain blockchain events occur.

Why Use Chainhook?

  • Event-driven model: You can react to on-chain events as they happen without needing to make API requests.
  • Customizable predicates: Define complex criteria for tracking on-chain events, helpful for apps that need to react to specific on-chain events or conditions.
  • Reorg resistance: Chainhook handles blockchain reorganizations gracefully.

Setting Up Chainhook

Install Chainhook:


# macOS installation via Homebrew

brew install chainhook

Creating a Predicate

Define a predicate to trigger actions based on block events. For instance, to perform an action when the block height exceeds a certain value, you would use a predicate like this:


{
"chain": "stacks",
"uuid": "12345",
"name": "New Blocks",
"version": 1,
"networks": {
"mainnet": {
"if_this": {
"scope": "block_height",
"higher_than": 150000  // Replace with the current block height
},
"then_that": {
"http_post": {
"url": "http://yourserver.com/api/block-update",
"authorization_header": "Bearer your_auth_token"
}
}
}
}
}

Alternatively, to monitor blocks within a specific range, you can swap out the <code-rich-text>higher_than<code-rich-text> operator with <code-rich-text>between<code-rich-text>. For example: <code-rich-text>"between": [150000, 160000]<code-rich-text>

Running Chainhook With Your Predicate

Execute Chainhook with your predicate file:


chainhook predicates scan ./predicates/blockHeightPredicate.json --mainnet

For continuous monitoring, you can run Chainhook as a service:


chainhook service start --predicate-path=./predicates/blockHeightPredicate.json --mainnet

For more examples on how to utilize Chainhook as a service, click here.

Which Should You Choose?

The best solution depends on your application's requirements.

Choose the Client SDK if:

  • You need immediate updates on a range of blockchain events.
  • You prefer a ready-to-use library with minimal setup.

Choose Chainhook if:

  • Your application needs to handle specific blockchain events with precision.
  • You want to build reorg-resistant features.

In other words, if your needs are simpler and the info you need is more general, then the Client SDK is for you. If you’re willing to do a bit more upfront work to be able to target more specific data, then try out Chainhook.

Conclusion

The Nakamoto upgrade marks a significant milestone for the Stacks ecosystem, speeding up block confirmation times and enhancing network efficiency. While this brings numerous benefits, it also poses challenges related to API rate limits.

By adopting alternative solutions like the Stacks Blockchain API Client SDK or Chainhook, developers can continue to build responsive and reliable applications without being hindered by rate limits. Embracing these tools will not only help you adapt to the new block speeds but also unlock more efficient ways with how your applications interact with the Stacks blockchain post-Nakamoto.

Product updates & dev resources straight to your inbox
Your Email is in an invalid format
Checkbox is required.
Thanks for
subscribing.
Oops! Something went wrong while submitting the form.
Copy link
Mailbox
Hiro news & product updates straight to your inbox
Only relevant communications. We promise we won’t spam.

Related stories