Usage

Learn how to initialize and use the Nimiq Hub API in your applications.

Initialization

Install and initialize the Hub API for your chosen network:

npm install @nimiq/hub-api
import { HubApi } from '@nimiq/hub-api';
 
// Connect to testnet
const hubApi = new HubApi('https://hub.nimiq-testnet.com');
 
// Or connect to mainnet
const hubApi = new HubApi('https://hub.nimiq.com');

Calling Methods

API methods must be called in response to user actions to handle popups correctly. Here are examples in different frameworks:

'use client';
import { HubApi } from '@nimiq/hub-api';
 
export default function CheckoutButton() {
  const hubApi = new HubApi('https://hub.nimiq-testnet.com');
 
  const handleClick = async () => {
    try {
      await hubApi.checkout({
        appName: 'My App',
        recipient: 'NQ...',
        value: 1000000,
      });
    } catch (error) {
      console.error('Checkout failed:', error);
    }
  };
 
  return <button onClick={handleClick}>Checkout</button>;
}

Important Notes

  • The Hub API opens a popup window on desktop and a new tab on mobile
  • API calls must be made directly in response to user actions
  • Avoid async operations before API calls to prevent popup blocking
  • Always handle both success and error cases

Network Selection

  • Use https://hub.nimiq-testnet.com for development and testing
  • Use https://hub.nimiq.com for production/mainnet
  • Network selection affects all transactions and account operations

Next Steps

On this page