The AbstractClient includes a createSession method that can be used to create a session key for the connected Abstract Global Wallet.

Usage

// This example demonstrates how to create a session key for NFT minting on a specific contract.
// The session key:
// - Can only call the mint function on the specified NFT contract
// - Has a lifetime gas fee limit of 1 ETH
// - Expires after 24 hours

import { useAbstractClient } from "@abstract-foundation/agw-react";
import { LimitType } from "@abstract-foundation/agw-client/sessions";
import { toFunctionSelector, parseEther } from "viem";
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";

// Generate a new session key pair
const sessionPrivateKey = generatePrivateKey();
const sessionSigner = privateKeyToAccount(sessionPrivateKey);

export default function CreateSession() {
  const { data: agwClient } = useAbstractClient();

  async function createSession() {
    if (!agwClient) return;

    const { session } = await agwClient.createSession({
      session: {
        signer: sessionSigner.address,
        expiresAt: BigInt(Math.floor(Date.now() / 1000) + 60 * 60 * 24),
        feeLimit: {
          limitType: LimitType.Lifetime,
          limit: parseEther("1"),
          period: BigInt(0),
        },
        callPolicies: [
          {
            target: "0xC4822AbB9F05646A9Ce44EFa6dDcda0Bf45595AA", // NFT contract
            selector: toFunctionSelector("mint(address,uint256)"),
            valueLimit: {
              limitType: LimitType.Unlimited,
              limit: BigInt(0),
              period: BigInt(0),
            },
            maxValuePerUse: BigInt(0),
            constraints: [],
          }
        ],
        transferPolicies: [],
      },
    });
  }
}

Parameters

session
SessionConfig
required

Configuration for the session key, including:

Returns

transactionHash
Hash | undefined

The transaction hash if a transaction was needed to enable sessions.

session
SessionConfig

The created session configuration.