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

# Session storage

# Session Storage

This guide covers session persistence so you don't need to re-authenticate on every run using the Privacy Boost CLI.

## Automatic Session Persistence

After `privacy-boost login`, the CLI automatically saves the session. Subsequent commands use the saved session:

```bash theme={null}
# First time — requires private key
privacy-boost login --private-key $PRIVATE_KEY

# Subsequent commands — no private key needed
privacy-boost balance --token $TOKEN
privacy-boost shield --token $TOKEN --amount 1.0 --human
```

## Programmatic Session Export/Import (Rust)

### Export Session

```rust theme={null}
if let Some(session) = sdk.export_session() {
    let json = serde_json::to_string(&session)?;
    std::fs::write("session.json", json)?;
    println!("Session saved");
}
```

### Import Session

```rust theme={null}
let json = std::fs::read_to_string("session.json")?;
let session: ExportedSession = serde_json::from_str(&json)?;

match sdk.import_session(&session) {
    Ok(true) => println!("Session restored"),
    Ok(false) => println!("Session invalid or expired"),
    Err(e) => println!("Import failed: {:?}", e),
}
```

### ExportedSession

```rust theme={null}
pub struct ExportedSession {
    pub wallet_public_key_x: String,
    pub wallet_public_key_y: String,
    pub viewing_key: String,
    pub viewing_public_key_x: String,
    pub viewing_public_key_y: String,
    pub nullifying_key: String,
    pub nullifying_public_key_x: String,
    pub nullifying_public_key_y: String,
    pub mpk: String,
    pub account_id: String,
    pub jwt: String,
    pub jwt_expiry: u64,
    pub wallet_address: String,
}
```

## Configuration File

The CLI stores configuration at the default path:

```rust theme={null}
let config_path = CliConfig::default_config_path()?;
println!("Config at: {:?}", config_path);

// Save config
config.save_to_default()?;

// Load config
let config = CliConfig::load_from_default()?;
```

## Logout

```bash theme={null}
# End session, clear saved state
privacy-boost logout
```

| Method            | What it does      | Next login                 |
| ----------------- | ----------------- | -------------------------- |
| `logout()`        | Clears everything | Full re-auth required      |
| `clear_session()` | Clears JWT only   | Quick re-auth (no signing) |

```rust theme={null}
// Full logout
sdk.logout();

// Quick session clear
sdk.clear_session();
```

<Warning>
  Session files contain private keys. Store them securely and restrict file permissions.
</Warning>

## Best Practices

### 1. Protect Session Files

```bash theme={null}
chmod 600 session.json
```

### 2. Handle Expired Sessions

```rust theme={null}
match sdk.import_session(&session) {
    Ok(true) => println!("Session restored"),
    Ok(false) | Err(_) => {
        // Re-authenticate
        sdk.authenticate(&private_key, None, None)?;
    }
}
```

### 3. Clean Up on Exit

```rust theme={null}
// Delete session file on logout
sdk.logout();
std::fs::remove_file("session.json").ok();
```

## Next Steps

* [Wallet Integration](./wallet-integration)
* [Error Handling](./error-handling)
* [Network Presets](./network-presets)
* [Commands Reference](./commands)
