Skip to content
🌐Network: Mainnet

Sessions

Use sessions to delegate trading authority from the owner wallet to a temporary session key. Session keys can place and cancel orders on authorized markets, but they cannot withdraw funds or perform privileged owner operations.

Use the SDK when possible. The SDK creates the session key, signs the owner authorization, stores the active session, and uses it for trading calls.

For endpoint-only details, see Session Endpoints.

Create Session

Create a session after account setup and before placing orders.

typescript
// Delegates trading authority for the selected markets.
// Expiry is in days.
await client.createSession(wallet, ["fFUEL/fUSDC"], 30);

console.log("Session created:", client.session!.sessionAddress);
python
# Delegates trading authority for the selected markets.
# Expiry is in days.
session = await client.create_session(
    owner=wallet,
    markets=["fFUEL/fUSDC"],
    expiry_days=30,
)

print(f"Session created, expires: {session.session_expiry}")
rust
use std::time::Duration;

// Delegates trading authority for the selected markets.
let mut session = client
    .create_session(
        &wallet,
        &["fFUEL/fUSDC"],
        Duration::from_secs(30 * 24 * 3600),
    )
    .await?;

println!("Session created, expiry: {}", session.expiry);

Use Session For Trading

After a session is active, use the normal order helpers. The SDK signs trading actions with the session key and submits them through the session action endpoint.

typescript
const result = await client.createOrder("fFUEL/fUSDC", "buy", "1", "5");
console.log("Order tx:", result.txId);
python
from o2_sdk import OrderSide, OrderType

result = await client.create_order(
    market="fFUEL/fUSDC",
    side=OrderSide.BUY,
    price="1",
    quantity="5",
    order_type=OrderType.SPOT,
)
print(f"Order tx: {result.tx_id}")
rust
use o2_sdk::{OrderType, Side};

let result = client
    .create_order(
        &mut session,
        "fFUEL/fUSDC",
        Side::Buy,
        "1",
        "5",
        OrderType::Spot,
        true,
        true,
    )
    .await?;

println!("Order tx: {}", result.tx_id.as_deref().unwrap_or("?"));

For more order examples, see Orders.

Batch Actions

Batch actions are useful when you need to settle, create orders, and cancel orders in one request. The total action count, including SettleBalance, must not exceed 5.

typescript
import {
  createOrderAction,
  settleBalanceAction,
} from "@o2exchange/sdk";

await client.refreshNonce();

const response = await client.batchActions([
  {
    market: "fFUEL/fUSDC",
    actions: [
      settleBalanceAction(),
      createOrderAction("buy", "1", "5"),
    ],
  },
], true);

console.log("Batch tx:", response.txId);
python
from o2_sdk import OrderSide

await client.refresh_nonce()

batch = (
    client.actions_for("fFUEL/fUSDC")
    .settle_balance()
    .create_order(OrderSide.BUY, "1", "5")
    .build()
)

result = await client.batch_actions([batch], collect_orders=True)
print(f"Batch tx: {result.tx_id}")
rust
use o2_sdk::{OrderType, Side};

client.refresh_nonce(&mut session).await?;

let actions = client
    .actions_for("fFUEL/fUSDC")
    .await?
    .settle_balance()
    .create_order(Side::Buy, "1", "5", OrderType::Spot)
    .build()?;

let result = client
    .batch_actions(&mut session, "fFUEL/fUSDC", actions, true)
    .await?;

println!("Batch tx: {}", result.tx_id.as_deref().unwrap_or("?"));

When To Use The Raw API

Use the raw API only when you are building your own client, need custom signing, or are debugging the exact session payload.

Security Notes

  • Session keys can place and cancel orders only on authorized markets.
  • Session keys cannot withdraw funds.
  • Owner keys create sessions and perform privileged account operations.
  • Revoke or replace a session if the session key is exposed.