Skip to main content

Quick Start Guide

Get up and running with the Growatt API in minutes. This guide walks you through creating a client, authenticating, and retrieving your plant data.

Basic Usage

1

Create a new Growatt client

Initialize the client with default settings:
use growatt::Growatt;

let mut client = Growatt::new();
The client is created with:
  • Default base URL: https://server.growatt.com
  • 30-minute session duration
  • Automatic cookie management
2

Login to authenticate

Authenticate with your Growatt credentials:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Growatt::new();
    
    if client.login("your_username", "your_password").await? {
        println!("Login successful!");
        // Continue with API calls
    }
    
    Ok(())
}
The login() method:
  • Returns Result<bool, GrowattError>
  • Automatically hashes the password with MD5
  • Stores session cookies for subsequent requests
  • Sets session expiry time
3

Retrieve your plants

Get a list of all plants associated with your account:
let plants = client.get_plants().await?;
println!("Plants: {:?}", plants);

// Iterate through plants
for plant in plants.0 {
    println!("Plant ID: {}", plant.plant_id);
    println!("Plant Name: {}", plant.plant_name);
    
    if let Some(address) = plant.plant_address {
        println!("Address: {}", address);
    }
    
    if let Some(power) = plant.plant_watts {
        println!("Power: {} W", power);
    }
}
The PlantList struct contains a vector of Plant objects. Access the inner vector with .0.
4

Logout when finished

Properly terminate your session:
client.logout().await?;
println!("Logged out successfully");
Always logout when you’re done to properly clean up the session on the server.

Complete Example

Here’s a complete working example:
src/main.rs
use growatt::Growatt;

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

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

        // Get plants
        let plants = client.get_plants().await?;
        println!("Found {} plants", plants.0.len());
        
        for plant in plants.0 {
            println!("\nPlant: {}", plant.plant_name);
            println!("ID: {}", plant.plant_id);
            
            // Get detailed plant information
            match client.get_plant(&plant.plant_id).await {
                Ok(plant_data) => {
                    if let Some(today_energy) = plant_data.today_energy {
                        println!("Today's Energy: {} kWh", today_energy);
                    }
                    if let Some(total_energy) = plant_data.total_energy {
                        println!("Total Energy: {} kWh", total_energy);
                    }
                    if let Some(current_power) = plant_data.current_power {
                        println!("Current Power: {} W", current_power);
                    }
                },
                Err(e) => println!("Error getting plant details: {}", e),
            }
        }

        // When you're done
        client.logout().await?;
    }

    Ok(())
}

Environment-Based Configuration

For production applications, use environment variables:
1

Create a .env file

.env
GROWATT_USERNAME=your_username
GROWATT_PASSWORD=your_password
GROWATT_SESSION_DURATION=60
2

Initialize from environment

use growatt::Growatt;
use dotenv::dotenv;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load from .env file and environment
    dotenv().ok();

    // Create client with env vars
    let mut client = Growatt::from_env();

    // Check login status and auto-login if needed
    if client.is_logged_in() {
        // Your code here
    }

    Ok(())
}
from_env() automatically loads credentials from environment variables but doesn’t log in. You still need to call login() or check_login().

Advanced Client Configuration

Custom Session Duration

let client = Growatt::new()
    .with_session_duration(60);  // 60 minutes

Alternative Server URL

let client = Growatt::new()
    .with_alternate_url();  // Uses https://openapi.growatt.com

Chain Configuration

let client = Growatt::new()
    .with_alternate_url()
    .with_session_duration(120);

Error Handling

Handle errors gracefully:
match client.login("username", "password").await {
    Ok(success) => {
        if success {
            println!("Login successful");
        } else {
            println!("Login failed");
        }
    },
    Err(err) => match err {
        growatt::GrowattError::AuthError(msg) => {
            println!("Authentication error: {}", msg);
        },
        growatt::GrowattError::RequestError(err) => {
            println!("Network error: {}", err);
        },
        growatt::GrowattError::JsonError(err) => {
            println!("JSON parsing error: {}", err);
        },
        growatt::GrowattError::InvalidResponse(msg) => {
            println!("Invalid API response: {}", msg);
        },
        growatt::GrowattError::NotLoggedIn => {
            println!("Not logged in");
        },
    }
}

Type Signatures

Key method signatures:
// Authentication
pub async fn login(&mut self, username: &str, password: &str) -> Result<bool>
pub async fn logout(&mut self) -> Result<bool>
pub fn is_logged_in(&self) -> bool
pub fn get_token(&self) -> Option<String>

// Plant operations
pub async fn get_plants(&mut self) -> Result<PlantList>
pub async fn get_plant(&mut self, plant_id: &str) -> Result<PlantData>
pub async fn get_weather(&mut self, plant_id: &str) -> Result<serde_json::Value>

// Device operations
pub async fn get_mix_ids(&mut self, plant_id: &str) -> Result<serde_json::Value>
pub async fn get_device_list(&mut self, plant_id: &str) -> Result<serde_json::Value>

// Error type
pub type Result<T> = std::result::Result<T, GrowattError>

What’s Next?

Now that you’ve completed the quick start, explore more advanced features:

Authentication

Learn about session management and token handling

Plant Management

Retrieve detailed plant information and statistics

Energy Statistics

Access daily, monthly, and yearly energy data

Error Handling

Build robust applications with proper error handling

Build docs developers (and LLMs) love