> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chipipay.com/llms.txt
> Use this file to discover all available pages before exploring further.

# useGetSessionData

> Query session key status from the CHIPI wallet smart contract. Returns whether the session is active, remaining calls, and allowed entrypoints.

## Usage

```typescript theme={null}
const { 
  data, 
  isLoading, 
  isError,
  error,
  isSuccess,
  refetch
} = useGetSessionData(params, options);
```

### Parameters

| Parameter                 | Type    | Required | Description                              |
| ------------------------- | ------- | -------- | ---------------------------------------- |
| `params.walletAddress`    | string  | Yes      | Wallet address to query                  |
| `params.sessionPublicKey` | string  | Yes      | Session public key to query              |
| `options.enabled`         | boolean | No       | Enable/disable the query (default: true) |

Pass `null` as params to disable the query.

### Return Value

| Property    | Type                | Description                   |
| ----------- | ------------------- | ----------------------------- |
| `data`      | SessionDataResponse | Session status from contract  |
| `isLoading` | boolean             | Whether query is in progress  |
| `isError`   | boolean             | Whether an error occurred     |
| `error`     | Error \| null       | Error details if any          |
| `isSuccess` | boolean             | Whether query succeeded       |
| `refetch`   | function            | Manually refetch session data |

### SessionDataResponse Structure

```typescript theme={null}
{
  isActive: boolean;           // Whether session is currently valid
  validUntil: number;          // Expiration timestamp (Unix seconds)
  remainingCalls: number;      // Transactions remaining before exhausted
  allowedEntrypoints: string[]; // Whitelisted function selectors
}
```

## Example Implementation

```typescript theme={null}
import { useGetSessionData } from "@chipi-stack/chipi-react";

export function SessionStatus() {
  // Get session from local storage
  const session = JSON.parse(localStorage.getItem('chipiSession') || 'null');
  
  const { data: sessionData, isLoading, refetch } = useGetSessionData(
    session ? {
      walletAddress: wallet.publicKey,
      sessionPublicKey: session.publicKey,
    } : null
  );

  if (!session) {
    return <div className="p-4 bg-gray-100 rounded">No active session</div>;
  }

  if (isLoading) {
    return <div className="p-4">Loading session status...</div>;
  }

  return (
    <div className="p-6 bg-white rounded-lg shadow">
      <div className="flex justify-between items-center mb-4">
        <h2 className="text-xl font-semibold">Session Status</h2>
        <button 
          onClick={() => refetch()}
          className="text-blue-600 hover:text-blue-800"
        >
          Refresh
        </button>
      </div>
      
      <div className="space-y-3">
        <div className="flex justify-between">
          <span className="text-gray-600">Status:</span>
          <span className={sessionData?.isActive ? "text-green-600" : "text-red-600"}>
            {sessionData?.isActive ? "Active" : "Inactive"}
          </span>
        </div>
        
        <div className="flex justify-between">
          <span className="text-gray-600">Expires:</span>
          <span>
            {sessionData?.validUntil 
              ? new Date(sessionData.validUntil * 1000).toLocaleString()
              : "N/A"}
          </span>
        </div>
        
        <div className="flex justify-between">
          <span className="text-gray-600">Remaining Calls:</span>
          <span className="font-mono">{sessionData?.remainingCalls ?? 0}</span>
        </div>
        
        <div className="flex justify-between">
          <span className="text-gray-600">Restrictions:</span>
          <span>
            {sessionData?.allowedEntrypoints?.length 
              ? `${sessionData.allowedEntrypoints.length} functions` 
              : "All functions"}
          </span>
        </div>
      </div>
      
      {!sessionData?.isActive && (
        <div className="mt-4 p-3 bg-yellow-50 text-yellow-800 rounded">
          Session is inactive. Create a new session to continue.
        </div>
      )}
    </div>
  );
}
```

## Conditional Rendering Based on Session

```typescript theme={null}
import { useGetSessionData } from "@chipi-stack/chipi-react";

export function GameInterface() {
  const session = JSON.parse(localStorage.getItem('chipiSession') || 'null');
  
  const { data: sessionData } = useGetSessionData(
    session ? {
      walletAddress: wallet.publicKey,
      sessionPublicKey: session.publicKey,
    } : null
  );

  // Check if session is valid before allowing actions
  const canPlay = sessionData?.isActive && sessionData?.remainingCalls > 0;

  return (
    <div>
      {canPlay ? (
        <GameBoard />
      ) : (
        <div className="text-center p-8">
          <p className="mb-4">Session expired or no calls remaining</p>
          <CreateSessionButton />
        </div>
      )}
    </div>
  );
}
```

## Auto-Refresh Session Status

```typescript theme={null}
import { useGetSessionData } from "@chipi-stack/chipi-react";
import { useEffect } from "react";

export function useSessionMonitor(walletAddress: string, sessionPublicKey: string) {
  const { data, refetch } = useGetSessionData({
    walletAddress,
    sessionPublicKey,
  });

  // Refresh every 30 seconds
  useEffect(() => {
    const interval = setInterval(() => {
      refetch();
    }, 30000);

    return () => clearInterval(interval);
  }, [refetch]);

  return {
    isActive: data?.isActive ?? false,
    remainingCalls: data?.remainingCalls ?? 0,
    expiresAt: data?.validUntil ? new Date(data.validUntil * 1000) : null,
  };
}
```

<Info>
  This is a **read-only** query that doesn't require gas or signatures. It directly calls the smart contract's view function.
</Info>

<Tip>
  If the session doesn't exist on-chain, `isActive` will be `false` with `remainingCalls: 0`. This is normal for sessions that were never registered or have been revoked.
</Tip>
