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

# useSessionKey

> React hook for managing session keys for non-custodial payments.

`useSessionKey` provides programmatic access to session key lifecycle management: loading, setup, and revocation.

## Import

```ts theme={null}
import { useSessionKey } from '@taprails/tap-to-pay';
```

## Usage

```tsx theme={null}
const {
  sessionKey,
  setupSessionKey,
  revokeSessionKey,
  isLoading,
  error,
  refresh,
} = useSessionKey();
```

## Return Value

<ResponseField name="sessionKey" type="SessionKeyData | null">
  The current session key data loaded from the device Keychain, or `null` if no key exists. Loaded automatically on mount.
</ResponseField>

<ResponseField name="setupSessionKey" type="(walletAddress?: string) => Promise<SessionKeyData>">
  Triggers the full session key setup flow: generates a keypair, calls `onSignTransaction` for user approval, and registers the public key with the backend. Returns the created key.
</ResponseField>

<ResponseField name="revokeSessionKey" type="() => Promise<void>">
  Revokes the current session key on the backend and deletes it from the Keychain.
</ResponseField>

<ResponseField name="isLoading" type="boolean">
  `true` during `setupSessionKey` or `revokeSessionKey` operations.
</ResponseField>

<ResponseField name="error" type="Error | null">
  Error from the most recent operation.
</ResponseField>

<ResponseField name="refresh" type="() => Promise<void>">
  Re-reads the session key from the Keychain. Useful after external changes.
</ResponseField>

***

## SessionKeyData

```ts theme={null}
interface SessionKeyData {
  publicKey: string;   // ECDSA public key (PEM format)
  expiresAt: number;   // Unix timestamp (ms) when the key expires
  walletAddress: string;
}
```

***

## Example

```tsx theme={null}
import { useSessionKey } from '@taprails/tap-to-pay';

export function SessionKeySettings() {
  const { sessionKey, revokeSessionKey, isLoading } = useSessionKey();

  const expiresAt = sessionKey ? new Date(sessionKey.expiresAt) : null;
  const isExpired = expiresAt ? expiresAt < new Date() : false;

  return (
    <View>
      {sessionKey ? (
        <>
          <Text>
            Status: {isExpired ? '⚠️ Expired' : '✅ Active'}
          </Text>
          <Text>
            Expires: {expiresAt?.toLocaleDateString()}
          </Text>
          <Button
            title="Revoke Tap-to-Pay Access"
            onPress={revokeSessionKey}
            disabled={isLoading}
          />
        </>
      ) : (
        <Text>No session key — tap-to-pay not set up.</Text>
      )}
    </View>
  );
}
```
