Perp API Reference
Query and subscribe to perpetual trading data including positions, trade history, market borrowing rates, and referrals.
Table of Contents
Section titled “Table of Contents”Queries
Section titled “Queries”Get a specific trade by ID and trader address.
Signature:
trade(id: Int!, trader: String!): PerpTradeParameters:
id: Trade ID (integer)trader: Trader’s wallet address (required)
Returns: PerpTrade object or null if not found
Example:
query GetTrade { perp { trade(id: 42, trader: "nibi1abc...") { id isOpen isLong leverage collateralAmount openPrice closePrice perpBorrowing { baseToken { symbol } collateralToken { symbol } marketId } openBlock { block block_ts } closeBlock { block block_ts } state { pnlCollateral pnlPct liquidationPrice positionValue } } }}Use Cases:
- Display single trade details
- Track specific position status
- Calculate current PnL
trades
Section titled “trades”List trades with filtering, pagination, and ordering.
Signature:
trades( where: PerpTradesFilter! limit: Int offset: Int order_by: PerpTradesOrder order_desc: Boolean): [PerpTrade!]!Parameters:
where: Filter object (required)trader: Trader address (required)isOpen: Filter by open/closed statusperpMarketId: Filter by market IDperpCollateralId: Filter by collateral token ID
limit: Max results (default: 100)offset: Skip results for paginationorder_by: Sort field (sequenceonly)order_desc: Sort descending (default: false)
Returns: Array of PerpTrade objects
Example - Get Open Positions:
query GetOpenPositions($trader: String!) { perp { trades( where: { trader: $trader isOpen: true } limit: 50 order_desc: true ) { id isLong leverage collateralAmount openPrice perpBorrowing { baseToken { symbol } marketId } state { pnlCollateral pnlPct liquidationPrice } } }}Example - Filter by Market:
query GetBTCTrades($trader: String!) { perp { trades( where: { trader: $trader perpMarketId: 1 # BTC market } limit: 20 ) { id isOpen openPrice closePrice } }}Use Cases:
- List user’s open positions
- Display trading history
- Filter by specific markets
- Build position dashboard
tradeHistory
Section titled “tradeHistory”Get trade events and position changes.
Signature:
tradeHistory( where: PerpTradeHistoryFilter! limit: Int offset: Int order_by: PerpTradeHistoryOrder order_desc: Boolean): [PerpTradeHistoryItem!]!Parameters:
where: Filter object (required)trader: Trader address (required)
limit: Max results (default: 100)offset: Skip resultsorder_by: Sort bysequenceortrade_idorder_desc: Sort descending
Returns: Array of PerpTradeHistoryItem objects
Trade Change Types:
position_opened: New position createdposition_closed_user: User closed positionposition_closed_sl: Stop loss triggeredposition_closed_tp: Take profit triggeredposition_liquidated: Position liquidatedsl_updated: Stop loss updatedtp_updated: Take profit updatedlimit_order_created: Limit order placedstop_order_created: Stop order placedorder_triggered: Order executedorder_closed_user: Order cancelled
Example:
query GetTradeHistory($trader: String!) { perp { tradeHistory( where: { trader: $trader } limit: 50 order_by: sequence order_desc: true ) { id tradeChangeType block { block block_ts } trade { id isLong leverage } realizedPnlCollateral realizedPnlPct } }}Use Cases:
- Build activity feed
- Track position modifications
- Calculate realized PnL history
- Audit trade actions
borrowing
Section titled “borrowing”Get detailed borrowing information for a specific market and collateral.
Signature:
borrowing(collateralId: Int!, marketId: Int!): PerpBorrowingParameters:
collateralId: Collateral token IDmarketId: Market ID
Returns: PerpBorrowing object or null
Example:
query GetMarketInfo { perp { borrowing(collateralId: 1, marketId: 1) { marketId baseToken { symbol name } quoteToken { symbol } collateralToken { symbol } price oiLong oiShort oiMax feesPerHourLong feesPerHourShort openFeePct closeFeePct triggerOrderFeePct maxLeverage minLeverage minPositionSizeUSD visible } }}Key Fields Explained:
oiLong/oiShort: Open interest for longs/shortsoiMax: Maximum allowed open interestfeesPerHourLong/feesPerHourShort: Borrowing rate (per hour)openFeePct/closeFeePct: Opening/closing fee percentagesmaxLeverage/minLeverage: Standard market leverage limitsminPositionSizeUSD: Minimum position size in USD
For scheduled markets, maxLeverage is not the same as the after-hours
effective leverage cap. If your integration needs to detect carried positions
that require after-hours normalization, use the perp contract’s after-hours
queries and get_trade_data.needs_after_hours_trigger.
Use Cases:
- Display market information
- Calculate funding rates
- Show available leverage
- Check market capacity (OI limits)
borrowings
Section titled “borrowings”List all available markets with basic information.
Signature:
borrowings( where: PerpBorrowingsFilter limit: Int offset: Int order_by: PerpBorrowingsOrder order_desc: Boolean): [PerpBorrowingShortInfo!]!Parameters:
where: Filter object (optional)marketId: Filter by market IDcollateralId: Filter by collateral ID
limit: Max resultsoffset: Skip resultsorder_by: Sort bybase_token_nameorder_desc: Sort descending
Returns: Array of PerpBorrowingShortInfo objects
Example:
query ListMarkets { perp { borrowings( order_by: base_token_name limit: 20 ) { marketId baseToken { symbol name logoUrl } quoteToken { symbol } collateralToken { symbol } visible } }}Use Cases:
- List all tradeable markets
- Build market selector
- Filter visible markets
Subscriptions
Section titled “Subscriptions”Subscribe to real-time updates for perpetual trading data.
perpTrades
Section titled “perpTrades”Subscribe to trade updates for a specific trader.
Signature:
subscription { perpTrades(where: SubPerpTradesFilter!): [PerpTrade!]!}Example:
subscription WatchTrades($trader: String!) { perpTrades(where: { trader: $trader }) { id isOpen leverage state { pnlCollateral pnlPct } }}perpTradeHistory
Section titled “perpTradeHistory”Subscribe to trade events for a specific trader.
Signature:
subscription { perpTradeHistory(where: SubPerpTradeHistoryFilter!): [PerpTradeHistoryItem!]!}Example:
subscription WatchTradeEvents($trader: String!) { perpTradeHistory(where: { trader: $trader }) { id tradeChangeType realizedPnlCollateral block { block_ts } }}perpBorrowing
Section titled “perpBorrowing”Subscribe to borrowing rate updates for a specific market.
Signature:
subscription { perpBorrowing(collateralId: Int!, marketId: Int!): PerpBorrowing}Example:
subscription WatchMarket($collateralId: Int!, $marketId: Int!) { perpBorrowing(collateralId: $collateralId, marketId: $marketId) { price oiLong oiShort feesPerHourLong feesPerHourShort }}perpBorrowings
Section titled “perpBorrowings”Subscribe to updates for all markets.
Signature:
subscription { perpBorrowings: [PerpBorrowingShortInfo!]!}Example:
subscription WatchAllMarkets { perpBorrowings { marketId baseToken { symbol } visible }}Types Reference
Section titled “Types Reference”PerpTrade
Section titled “PerpTrade”type PerpTrade { id: Int! trader: String! isOpen: Boolean! isLong: Boolean! tradeType: PerpTradeType! leverage: Float! collateralAmount: Int! openCollateralAmount: Int! openPrice: Float! closePrice: Float sl: Float # Stop loss price tp: Float # Take profit price perpBorrowing: PerpBorrowingShortInfo! openBlock: Block closeBlock: Block state: PerpTradeState}PerpTradeState
Section titled “PerpTradeState”Real-time position state with PnL and fees.
type PerpTradeState { pnlCollateral: Float! pnlPct: Float! pnlCollateralAfterFees: Float! positionValue: Float! liquidationPrice: Float! borrowingFeeCollateral: Float! borrowingFeePct: Float! closingFeeCollateral: Float! closingFeePct: Float! remainingCollateralAfterFees: Float!}PerpBorrowing
Section titled “PerpBorrowing”Complete market information.
type PerpBorrowing { marketId: Int! baseToken: Token! quoteToken: Token! collateralToken: Token! price: Float! oiLong: Int! oiShort: Int! oiMax: Int! feesPerHourLong: Float! feesPerHourShort: Float! openFeePct: Float! closeFeePct: Float! triggerOrderFeePct: Float! maxLeverage: Float! minLeverage: Float! minPositionSizeUSD: Int! visible: Boolean!}PerpTradeType:
trade: Market tradelimit: Limit orderstop: Stop order
PerpTradeChangeType:
position_openedposition_closed_userposition_closed_slposition_closed_tpposition_liquidatedsl_updatedtp_updatedlimit_order_createdstop_order_createdorder_triggeredorder_closed_user