Skip to main content

Overview

The Growatt SDK provides login() and logout() methods for session-based authentication with the Growatt API server. All API requests require an active authenticated session.

Login Method

Signature

pub async fn login(&mut self, username: &str, password: &str) -> Result<bool>
Authenticates with the Growatt server and establishes a session. Returns true on successful authentication.

Parameters

username
&str
required
Your Growatt account username
password
&str
required
Your Growatt account password (will be MD5 hashed automatically)

Returns

Result<bool>
Result<bool>
Returns Ok(true) on successful login, Ok(false) if already logged in with valid session, or Err(GrowattError) on failure

Behavior

  • If already logged in with a valid session, returns true immediately without making a new request
  • Stores credentials internally for automatic session renewal
  • Sets session expiry time based on configured session duration (default 30 minutes)
  • Extracts and stores authentication token from the response
  • Password is automatically hashed using MD5 before transmission

Example

use growatt::Growatt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Growatt::new();

    // Login with username and password
    if client.login("your_username", "your_password").await? {
        println!("Login successful!");

        // Your API calls here
        let plants = client.get_plants().await?;
        println!("Plants: {:?}", plants);

        // Logout when done
        client.logout().await?;
    }

    Ok(())
}

Error Handling

match client.login("username", "password").await {
    Ok(success) => {
        if success {
            println!("Login successful");
        } else {
            println!("Login failed");
        }
    },
    Err(err) => match err {
        GrowattError::AuthError(msg) => println!("Authentication error: {}", msg),
        GrowattError::RequestError(err) => println!("Network error: {}", err),
        GrowattError::InvalidResponse(msg) => println!("Invalid API response: {}", msg),
        _ => println!("Unexpected error: {}", err),
    }
}
The login() method stores your credentials internally to enable automatic session renewal when the session expires.

Logout Method

Signature

pub async fn logout(&mut self) -> Result<bool>
Terminates the current session with the Growatt server.

Returns

Result<bool>
Result<bool>
Returns Ok(true) on successful logout, Ok(false) if no active session exists, or Err(GrowattError) on failure

Behavior

  • Clears session state and credentials
  • Sets is_logged_in to false
  • Clears session expiry time
  • Returns false if no active session exists (no-op)
  • Growatt API returns 302 redirect on successful logout

Example

// Properly terminate the session
let success = client.logout().await?;
if success {
    println!("Successfully logged out");
} else {
    println!("No active session to log out from");
}
Always call logout() when you’re done with the API to properly terminate your session and free server resources.

Complete Authentication Workflow

use growatt::Growatt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Growatt::new();

    // Login
    match client.login("username", "password").await {
        Ok(true) => {
            println!("Authentication successful");
            
            // Perform API operations
            let plants = client.get_plants().await?;
            for plant in plants.0 {
                println!("Plant: {} (ID: {})", plant.plant_name, plant.plant_id);
            }
            
            // Logout when complete
            client.logout().await?;
        },
        Ok(false) => println!("Login returned false"),
        Err(e) => eprintln!("Login failed: {}", e),
    }

    Ok(())
}

Source Reference

  • login() implementation: src/lib.rs:161
  • logout() implementation: src/lib.rs:248
  • Password hashing: src/lib.rs:151

Build docs developers (and LLMs) love