Using Redirects

Learn how to configure and handle redirects instead of popups in the Nimiq Hub API.

Configuring Top-Level Redirects

You can use redirects instead of popups by providing a RedirectRequestBehavior instance:

HTTPS Required

Your app must run under HTTPS to use redirects!

API Changes

The redirect configuration API may change in future versions.

RedirectRequestBehavior Configuration

The RedirectRequestBehavior constructor accepts two optional parameters:

// Custom return URL
 
The `RedirectRequestBehavior` constructor accepts two optional parameters:
 
```typescript
// Custom return URL
const redirectBehavior = new RedirectRequestBehavior('https://your-app.com/return');
 
// With stored data
const storedData = { foo: 'I am the state' };
const redirectBehavior = new RedirectRequestBehavior(null, storedData);
 
// Both options
const redirectWithData = new RedirectRequestBehavior(
  'https://your-app.com/return',
  { foo: 'state data' }
);

If no return URL is specified, the current URL without parameters will be used.

Handling Redirect Responses

When using redirects, follow these steps to handle responses from the Hub:

import { HubApi } from '@nimiq/hub-api';
 
// 1. Initialize Hub API
const hubApi = new HubApi('https://hub.nimiq-testnet.com');
 
// 2. Define handlers
function onSuccess(result: any, storedData: any) {
  console.log('Success:', result);
  console.log('Stored data:', storedData);
}
 
function onError(error: Error, storedData: any) {
  console.error('Error:', error);
  console.log('Stored data:', storedData);
}
 
// 3. Listen for responses
hubApi.on(HubApi.RequestType.CHECKOUT, onSuccess, onError);
hubApi.on(HubApi.RequestType.SIGN_TRANSACTION, onSuccess, onError);
hubApi.on(HubApi.RequestType.CHOOSE_ADDRESS, onSuccess, onError);
hubApi.on(HubApi.RequestType.SIGN_MESSAGE, onSuccess, onError);
 
// 4. Check for redirect response
hubApi.checkRedirectResponse();

Available Request Types

The Hub API provides these request types for redirect handling:

enum HubApi.RequestType {
    CHECKOUT = 'checkout',
    CHOOSE_ADDRESS = 'choose-address',
    SIGN_TRANSACTION = 'sign-transaction',
    SIGN_MESSAGE = 'sign-message',
}

Each request type corresponds to its respective API method and should be used when setting up redirect response listeners.

On this page