Skip to main content
An authenticated session is a two-way authenticated communication channel with the Perps system that allows your app to place orders, read private Perps account data, and receive private real-time updates.

Set Up Perps Access

1

Create a Secure Client

Create a SecureClient for the Polymarket wallet that owns the Perps account, using the signer that controls it.
import { createSecureClient } from "@polymarket/client";
import { privateKey } from "@polymarket/client/viem";

const client = await createSecureClient({
  wallet: process.env.POLYMARKET_WALLET_ADDRESS!,
  signer: privateKey(process.env.PRIVATE_KEY!),
});
This example uses Viem for wallet signing. See the TypeScript tooling guide for other wallet library integrations.
2

Open a Perps Session

Open a Perps session. By default, delegated Perps credentials expire after one week.
const session = await client.openPerpsSession();
You can also set the session lifetime and label explicitly. expiresIn is measured in milliseconds.
const session = await client.openPerpsSession({
  expiresIn: 7 * 24 * 60 * 60 * 1000,
  label: "trading-app",
});

Session Lifecycle

Open an authenticated session to start trading, read private Perps account data, and receive private real-time updates.
1

Listen for Session Events

After opening a Perps session, iterate over it to receive private real-time updates.
const session = await client.openPerpsSession();

for await (const event of session) {
  switch (event.type) {
    case "order":
      // Update local order state.
      break;

    case "fill":
      // Update position, PnL, or execution history.
      break;

    case "portfolio":
      // Refresh margin, equity, and position views.
      break;
  }
}
This example handles a few common session events. order and fill are the core trading updates, while portfolio provides periodic account-level snapshots for margin, equity, positions, and withdrawable balance.See Reconcile Trade State for how to use these events to keep local trading state in sync.
2

Close the Session

You can close the session at any time by calling session.close(). Closing a session releases local resources; stored credentials can still be resumed until they expire.
for await (const event of session) {
  if (shouldCloseSession) {
    await session.close();
    break;
  }

  // …
}

Resume a Session

Resume a session when stored credentials are still valid and a Perps workflow needs to continue in a new runtime context.
Read session.credentials after opening a session and store the object in secure credential storage.
const credentials = session.credentials;
// credentials: PerpsCredentials
where PerpsCredentials is:
type PerpsCredentials = {
  proxy: EvmAddress;
  privateKey: PrivateKey;
  secret: string;
  expiresAt: number;
};
Pass stored credentials back to openPerpsSession() while they are still valid. The SDK validates them and resumes the session.
const session = await client.openPerpsSession({
  credentials,
});
// session: PerpsSession