For the complete documentation index, see llms.txt. This page is also available as Markdown.

examples

Complete examples for common API operations.

Setup

// Load from environment (or use defaults for mainnet)
const API_URL = process.env.PERPL_API_URL || 'https://app.perpl.xyz/api';
const WS_URL = process.env.PERPL_WS_URL || 'wss://app.perpl.xyz';  // Note: WebSocket doesn't use /api prefix
const CHAIN_ID = Number(process.env.PERPL_CHAIN_ID) || 143;

// Market IDs for mainnet
// Note: Testnet uses different IDs (BTC=16, ETH=32, SOL=48, MON=64, ZEC=256)
const MARKETS = {
  BTC: 1,
  MON: 10,
  ETH: 20,
  SOL: 31,
  HYPE: 40,
  ZEC: 50,
} as const;

Authentication

Perpl authenticates programmatic clients with API keys (an Ed25519 key pair). Every request is signed with the key's private key — there is no bearer token or session cookie.

Getting a key

Create a key with the web UI — connect your wallet at:

  • Mainnet: https://app.perpl.xyz/apikeys

  • Testnet: https://testnet.perpl.xyz/apikeys

The UI walks you through the wallet-signed enrollment and hands you the private key (privateKey, Ed25519) plus the API_KEY token (X-API-Key). Third-party integrations can also enroll keys programmatically — see Integrations; a runnable JS enrollment example lives at examples/js/enroll_api_key.js. That example enrolls a builder-bound key when PERPL_BUILDER_ID (and optionally PERPL_MAX_BUILDER_FEE_PER_100K) is set — see Builder codes for what those mean and how to charge the fee with bf on an order.

The runnable example programs read an already-enrolled key from the environment:

Signed REST requests

Every REST call is signed with a signedFetch helper. It builds the canonical string, signs it with the key, and sends the four X-API-* headers (see authentication.md).


Fetching Market Data

Get Context

Get Candles

Get Funding History

Get Funding History of All Markets

Get the Order Book

Subscribe to the order-book@<market_id> stream instead if you are tracking the book continuously — see Order Book Subscription. This endpoint only ever answers a snapshot, so there is no o: 0 level-removal convention to handle.

Get Tickers

A market whose state has not been received yet is absent from d rather than zeroed — do not assume a key exists for every market in /v1/pub/context. If no market state is available at all the request is refused with 503 instead of returning an empty map.


Market Data WebSocket

Order Book Subscription

Trade Stream


Trading WebSocket

Trading Client


Trading over REST

The open orders, open positions and wallet are also served over HTTP, in the same shapes the WebSocket snapshots use — and orders can be placed, changed and cancelled without holding a connection open. A client that polls rather than streams needs no socket at all.

Runnable: examples/js/submit_orders.js, examples/typescript/submit_orders.ts, examples/python/submit_orders.py, examples/rust/src/bin/submit_orders.rs.

Get Open State

A wallet that holds no exchange account is answered 404, not an empty list — that is the "you have not created an account yet" signal, distinct from "you have an account with nothing open", which is a 200 with an empty d.

Submit a Batch of Orders

Four things to get right, none of which the HTTP status tells you:

1

A zero code acknowledges forwarding, not execution

Whether the order posted, filled or was rejected is settled a block later — read it from the WebSocket order updates (mt: 24) or by polling GET /v1/trading/orders.

2

A batch is not a transaction

Orders that were accepted stay accepted when a later one is refused.

3

Orders are identified by position

statuses[i] answers d[i]; there is no echo of the order in the status.

4

Every order costs a unit of the request-rate allowance

A batch that outruns it is served up to that point, and the rest are answered 429 in their own positions and may be retried.

Confirming an Order Landed

Request IDs (rq) are tracked per account. A new order's rq must be strictly greater than lfr on the account in the wallet snapshot — the last request ID the exchange forwarded — so take rq = max(localCounter, lfr) + 1; rq <= lfr is rejected as OrderDescIdTooLow, and an id below the last accepted is refused up front with 400. Re-sending the same rq is the retry mechanism and stays accepted — see Placing Orders for the full at-most-once rules, which are identical on both transports.


Fetching History

Get All Fills

Get Position PnL History


Utility Functions

Price Conversion

Size Conversion

Leverage Conversion


Error Handling