Box API's

For maximum integration flexibility.

Box API Documentation

This page provides instructions for accessing the Box API directly without using React hooks or components. You will learn how to make API requests, handle responses, and get started with the Box API.

API Request Type

The API request type is defined as follows:

interface BoxActionRequest {
  sender: Address;
  srcChainId: ChainId;
  srcToken?: Address;
  dstChainId: ChainId;
  dstToken?: Address;
  slippage: number;
  actionType: ActionType;
  actionConfig: ActionConfig;
}

API Response Type

The API response type is defined as follows:

type BoxActionResponse = {
  tx: Transaction;
  tokenPayment?: Payment;
  applicationFee?: Payment;
  bridgeFee?: Payment;
  bridgeId?: BridgeId;
  relayInfo?: RelayInfo;
};

Serialization and Deserialization

You will need to serialize and deserialize the actionRequest object. Use the following serialization and deserialization methods from @decent.xyz/box-hooks:

import { bigintSerializer, bigintDeserializer } from "@decent.xyz/box-common";

Alternatively, you can manually define these methods within your own codebase:

export const bigintSerializer = (key: string, value: unknown): unknown => {
  if (typeof value === "bigint") {
    return value.toString() + "n";
  }

  return value;
};

export const bigintDeserializer = (key: string, value: unknown): unknown => {
  if (typeof value === "string" && /^-?\d+n$/.test(value)) {
    return BigInt(value.slice(0, -1));
  }

  return value;
};

API Key

Before making API requests, ensure you have an API key that allows access to the Box API.

const apiKey = "YOUR_API_KEY_HERE";

API Endpoint

The API endpoint for Box API is:

const baseUrl = "https://box-v1.api.decent.xyz/api/getBoxAction";

Making an API Request

To make an API request, build your actionRequest object and encode it as arguments in the URL and make the request with your API key:

const actionRequest = {
  // Build your action request
  // ...
};

const url = `${baseUrl}?arguments=${JSON.stringify(
  actionRequest,
  bigintSerializer
)}`;

const response = await fetch(url, {
  headers: {
    "x-api-key": apiKey,
  },
});

Handling the Response

Get the plain text response so you can serialize to a BoxActionResponse:

const data = await response.text();

const actionResponse: BoxActionResponse = JSON.parse(data, bigintDeserializer);

From here, you can handle the response in the same way as with a hooks implementation. For further documentation and examples, refer to the following links:

Box Hooks Example Demo
Box Hooks Example Code