Skip to navigation

SDK Examples

Open and use sessions with the TypeScript and Python SDKs

Both SDKs build and validate session grants before anything is signed, and manage one JWT and one WebSocket per account the session covers.

Session support ships in @paradex/sdk 0.10.0 and paradex-py 0.10.0, and is experimental. Earlier versions sign a passkey SessionMint the server no longer accepts. Method names and options may change before they are marked stable.

Read the Paradex chain id from the starknet_chain_id field of GET /v1/system/config for the environment you use.

Open a session and trade

paradex.js runs in the browser. SessionConnector generates the session key in the page, keeps it encrypted at rest under a WebCrypto key that cannot be exported, asks the wallet for one signature and registers the grant with POST /v2/sessions. SessionAccounts then signs in for each account and routes requests by account.

import * as Paradex from '@paradex/sdk';
const connector = new Paradex.SessionConnector({
environment: 'testnet',
paradexChain: starknetChainId, // starknet_chain_id from GET /v1/system/config
// The injected wallet signs the grant with eth_signTypedData_v4.
// For MPC or other external signers use Paradex.Session.ownerSignerFromExternal.
owner: Paradex.Session.ownerSignerFromEip1193({ provider: window.ethereum }),
storage: Paradex.Session.indexedDbStorage(),
});
// Reuse an unexpired session from an earlier visit, or open a new one.
const [existing] = await connector.restore();
const session =
existing ??
(await connector.open({
account: '0x<main account>',
scopes: ['trade'],
}));
const accounts = new Paradex.SessionAccounts({
session,
environment: 'testnet',
paradexChain: starknetChainId,
onDropped: (reason) => {
console.warn('Session ended:', reason.code);
void connector.forget(session); // or restore() keeps returning the ended session
},
});
await accounts.start();
await accounts.placeOrder('0x<main account>', {
market: 'BTC-USD-PERP',
side: 'BUY',
type: 'LIMIT',
size: '0.01',
price: '60000',
});
await accounts.subscribe('0x<main account>', 'orders.ALL', (data) => console.log(data));

Open a new account with the first grant

A wallet with no Paradex account can open one with its first grant, as described in Create a Session. Name the address the server derives for the wallet, list no subaccounts, and pass any referral or marketing codes. They are sent with the grant but not signed.

const { address } = await Paradex.Onboarding.fetch({
apiUrl: 'https://api.testnet.paradex.trade/v1',
ethAddress: '0x<owner wallet>',
});
const session = await connector.open({
account: address,
scopes: ['trade'],
onboarding: { referralCode: 'cryptofox8' },
});

Refusals, including the onboarding ones, come back as a SessionApiError with a typed code in TypeScript, and as an exception naming the code in Python.

Handle the end of a session

Both SDKs follow the rule described in Check a session’s status: when a sign in or a request is refused for authentication, they call GET /v2/sessions/{grant_id}/status once. If the session is revoked, expired or unknown, every account is dropped together, JWTs are cleared and sockets are closed. Any other answer is treated as transient. A 403 from a missing scope never drops the session.

const accounts = new Paradex.SessionAccounts({
session,
environment: 'testnet',
paradexChain: starknetChainId,
onDropped: (reason) => {
// reason.code says why, such as GRANT_REVOKED or GRANT_EXPIRED.
showSignInAgain();
},
});
// The session object from GET /v2/sessions/{grant_id}
const { state } = await accounts.status();

Revoke the session

Revoking from the SDK calls DELETE /v2/sessions/{grant_id} with the session’s own JWT, then drops every account.

Both SDKs send cancel_orders=true by default, which also cancels the session’s resting orders, TWAPs, untriggered stops and the open RFQs it created, on every account. Once revoked, the session key can no longer cancel them itself. To sign out and leave the orders working, as the Paradex app does, pass { cancelOrders: false } in TypeScript or cancel_orders=False in Python. A session whose grant approves a builder still cancels its TWAPs and untriggered stops.

The session is dropped only once the server confirms the revoke, or answers 404 because it no longer has the grant. If the session key itself is refused, the status check above decides: a revoked, expired or unknown grant drops the session, and the call raises the drop error (SessionGrantError in TypeScript, SessionDroppedError in Python). Any other error is raised and the session stays live. After a 5xx the grant may already be revoked with its orders still working: a retry then drops the session, and only the owner’s DELETE /v2/sessions/{grant_id}?cancel_orders=true cancels them.

await accounts.revoke(); // or accounts.revoke({ cancelOrders: false })
accounts.close();

To revoke every session on the account at once, call DELETE /v2/sessions with a JWT from the account’s own key.

Typed data only

Both SDKs also expose the typed data on its own, for apps that sign with their own tooling or open sessions with a passkey.

const grant = Paradex.SessionTypedData.createGrant({
paradexChain: starknetChainId,
environment: 'mainnet',
account: '0x<main account>',
sessionKey: '0x<session public key>',
scopes: ['trade'],
});
// EVM owner: EIP-712 on the wallet's current chain
const chainId = await window.ethereum.request({ method: 'eth_chainId' });
const eip712 = Paradex.SessionTypedData.buildGrantEip712(grant, chainId);
// Starknet owner: SNIP-12 revision 1
const snip12 = Paradex.SessionTypedData.buildGrantSnip12(grant);
// Show the owner what they approve before they sign
console.log(Paradex.SessionTypedData.summarizeGrant(grant));
// Passkey: the WebAuthn challenge for a SessionMint you built (see the Passkeys page).
// Paradex.CEILING_LIMITS takes the passkey's ceiling; expiresAt: Paradex.LONGEST_SESSION its longest session.
const challenge = Paradex.SessionTypedData.mintChallenge(mint);

The default limits in both SDKs are the only form the server accepts today: every amount "none" and markets ["*"].